Class: Groonga::QueryLog::Analyzer::SizedGroupedOperations

Inherits:
Array
  • Object
show all
Defined in:
lib/groonga/query-log/analyzer/sized-grouped-operations.rb

Instance Method Summary collapse

Constructor Details

#initializeSizedGroupedOperations

Returns a new instance of SizedGroupedOperations.



24
25
26
27
# File 'lib/groonga/query-log/analyzer/sized-grouped-operations.rb', line 24

def initialize
  @max_size = 10
  @sorter = create_sorter
end

Instance Method Details

#<<(operation) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/groonga/query-log/analyzer/sized-grouped-operations.rb', line 42

def <<(operation)
  each do |grouped_operation|
    if grouped_operation[:name] == operation[:name] and
        grouped_operation[:context] == operation[:context]
      elapsed = operation[:relative_elapsed_in_seconds]
      grouped_operation[:total_elapsed] += elapsed
      grouped_operation[:n_operations] += 1
      replace(sort_by(&@sorter))
      return self
    end
  end

  grouped_operation = {
    :name => operation[:name],
    :context => operation[:context],
    :n_operations => 1,
    :total_elapsed => operation[:relative_elapsed_in_seconds],
  }
  buffer_size = @max_size * 100
  if size < buffer_size
    super(grouped_operation)
    replace(sort_by(&@sorter))
  else
    if @sorter.call(grouped_operation) < @sorter.call(last)
      super(grouped_operation)
      sorted_operations = sort_by(&@sorter)
      sorted_operations.pop
      replace(sorted_operations)
    end
  end
  self
end

#apply_options(options) ⇒ Object



29
30
31
# File 'lib/groonga/query-log/analyzer/sized-grouped-operations.rb', line 29

def apply_options(options)
  @max_size = options[:n_entries]
end

#eachObject



33
34
35
36
37
38
39
40
# File 'lib/groonga/query-log/analyzer/sized-grouped-operations.rb', line 33

def each
  i = 0
  super do |grouped_operation|
    break if i >= @max_size
    i += 1
    yield(grouped_operation)
  end
end