class GroongaQueryLog::Command::CheckPerformanceRegression

Public Class Methods

new(options={}) click to toggle source
# File lib/groonga-query-log/command/check-performance-regression.rb, line 32
def initialize(options={})
  setup_options(options)
end

Public Instance Methods

run(arguments) click to toggle source
# File lib/groonga-query-log/command/check-performance-regression.rb, line 36
def run(arguments)
  paths = []
  begin
    paths = @option_parser.parse!(arguments)
  rescue OptionParser::InvalidOption => error
    $stderr.puts(error)
    return false
  end

  if paths.size != 2
    $stderr.puts("old query log and new query log must be specified.")
    return false
  end

  old_query_paths = resolve_path(paths[0])
  return false if old_query_paths.nil?
  new_query_paths = resolve_path(paths[1])
  return false if new_query_paths.nil?

  old_statistics = analyze(old_query_paths)
  return false if old_statistics.nil?
  new_statistics = analyze(new_query_paths)
  return false if new_statistics.nil?

  open_output do |output|
    checker = Checker.new(old_statistics,
                          new_statistics,
                          output,
                          @threshold,
                          @target_queries)
    checker.check
  end
end

Private Instance Methods

analyze(log_paths) click to toggle source
# File lib/groonga-query-log/command/check-performance-regression.rb, line 172
def analyze(log_paths)
  full_statistics = []
  begin
    parser = Parser.new
    parse = parse_log(parser, log_paths)
    parse = parse.first(@n_entries) if @n_entries >= 0
    parse.each do |statistic|
      full_statistics << statistic
    end
  rescue Error
    $stderr.puts($!.message)
    return nil
  end
  full_statistics
end
open_output() { |$stdout| ... } click to toggle source
# File lib/groonga-query-log/command/check-performance-regression.rb, line 82
def open_output(&block)
  case @output
  when "-"
    yield($stdout)
  when String
    File.open(@output, "w", &block)
  else
    yield(@output)
  end
end
resolve_path(path) click to toggle source
# File lib/groonga-query-log/command/check-performance-regression.rb, line 71
def resolve_path(path)
  if File.directory?(path)
    Dir.glob("#{path}/*.log")
  elsif File.exist?(path)
    [path]
  else
    $stderr.puts("query log path doesn't exist: <#{path}>")
    nil
  end
end
setup_options(options) click to toggle source
# File lib/groonga-query-log/command/check-performance-regression.rb, line 93
def setup_options(options)
  @output = options[:output] || "-"
  @n_entries = -1
  @threshold = Threshold.new
  @target_queries = []

  @option_parser = OptionParser.new do |parser|
    parser.version = VERSION
    parser.banner += " OLD_QUERY_LOG NEW_QUERY_LOG"

    parser.on("-n", "--n-entries=N",
              Integer,
              "Analyze N query log entries",
              "You can use -1 to analyze all query log entries",
              "(#{@n_entries})") do |n|
      @n_entries = n
    end

    if @output == "-"
      default_output = "stdout"
    else
      default_output = @output
    end
    parser.on("--output=PATH",
              "Output to PATH.",
              "(#{default_output})") do |output|
      @output = output
    end

    parser.on("--target-query-file=TARGET_QUERY_FILE",
              "Analyze matched queries which are listed " +
              "in specified TARGET_QUERY_FILE.") do |path|
      if File.exist?(path)
        @target_queries = File.readlines(path, chomp: true)
      else
        message = "target query file doesn't exist: <#{path}>"
        raise OptionParser::InvalidOption.new(message)
      end
    end

    parser.on("--slow-query-ratio=RATIO",
              Float,
              "Use RATIO as threshold to detect slow queries.",
              "If MEAN_NEW_ELAPSED_TIME / MEAN_OLD_ELAPSED_TIME AVERAGE",
              "is larger than RATIO, the query is slow.",
              "(#{@threshold.query_ratio})") do |ratio|
      @threshold.query_ratio = ratio
    end

    parser.on("--slow-query-second=SECOND",
              Float,
              "Use SECOND as threshold to detect slow queries.",
              "If MEAN_NEW_ELAPSED_TIME - MEAN_OLD_ELAPSED_TIME AVERAGE",
              "is larger than SECOND, the query is slow.",
              "(#{@threshold.query_second})") do |second|
      @threshold.query_second = second
    end

    parser.on("--slow-operation-ratio=RATIO",
              Float,
              "Use RATIO as threshold to detect slow operations.",
              "If MEAN_NEW_ELAPSED_TIME / MEAN_OLD_ELAPSED_TIME AVERAGE",
              "is larger than RATIO, the operation is slow.",
              "(#{@threshold.operation_ratio})") do |ratio|
      @threshold.operation_ratio = ratio
    end

    parser.on("--slow-operation-second=SECOND",
              Float,
              "Use SECOND as threshold to detect slow operations.",
              "If MEAN_NEW_ELAPSED_TIME - MEAN_OLD_ELAPSED_TIME AVERAGE",
              "is larger than SECOND, the operation is slow.",
              "(#{@threshold.operation_second})") do |second|
      @threshold.operation_second = second
    end
  end
end