class GroongaQueryLog::MemoryLeakDetector

Public Class Methods

new(options) click to toggle source
# File lib/groonga-query-log/memory-leak-detector.rb, line 25
def initialize(options)
  @options = options
end

Public Instance Methods

detect(input) click to toggle source
# File lib/groonga-query-log/memory-leak-detector.rb, line 29
def detect(input)
  each_command(input) do |command|
    @options.create_client do |client|
      begin
        check_command(client, command)
      rescue Groonga::Client::Connection::Error
        # TODO: add error log mechanism
        $stderr.puts(Time.now.iso8601(6))
        $stderr.puts(command.original_source)
        $stderr.puts($!.raw_error.message)
        $stderr.puts($!.raw_error.backtrace)
      end
    end
  end
end

Private Instance Methods

check_command(client, command) click to toggle source
# File lib/groonga-query-log/memory-leak-detector.rb, line 53
def check_command(client, command)
  command["cache"] = "no" if @options.force_disable_cache?
  current_n_allocations = nil
  @options.n_tries.times do |i|
    client.execute(command)
    previous_n_allocations = current_n_allocations
    current_n_allocations = n_allocations(client)
    next if previous_n_allocations.nil?
    if current_n_allocations > previous_n_allocations
      max_n_digits = [
        compute_n_digits(previous_n_allocations),
        compute_n_digits(current_n_allocations),
      ].max
      puts("detect a memory leak:")
      puts("Nth try: #{i}")
      puts("previous: %*d" % [max_n_digits, previous_n_allocations])
      puts(" current: %*d" % [max_n_digits, current_n_allocations])
      puts(command.original_source)
    end
  end
end
compute_n_digits(n) click to toggle source
# File lib/groonga-query-log/memory-leak-detector.rb, line 79
def compute_n_digits(n)
  (Math.log10(n) + 1).floor
end
each_command(input) { |command| ... } click to toggle source
# File lib/groonga-query-log/memory-leak-detector.rb, line 46
def each_command(input)
  parser = Parser.new
  parser.parse(input) do |statistic|
    yield(statistic.command)
  end
end
n_allocations(client) click to toggle source
# File lib/groonga-query-log/memory-leak-detector.rb, line 75
def n_allocations(client)
  client.status.n_allocations
end