class ProfileTools::Collector

Collects stats around method calls

Attributes

methods[R]
total_collection_calls[R]

Public Class Methods

new() click to toggle source
# File lib/profile_tools/collector.rb, line 11
def initialize
  @methods = {}
  @total_collection_calls = 0
  @sort_order = 0
end

Public Instance Methods

called_methods() click to toggle source
# File lib/profile_tools/collector.rb, line 28
def called_methods
  @methods
    .values
    .reject { |info| info[:calls].zero? }
    .sort { |a, b| a[:sort_order] <=> b[:sort_order] }
end
init_method(method) click to toggle source
# File lib/profile_tools/collector.rb, line 17
def init_method(method)
  @methods[method] = {
    method: method,
    duration: 0.0,
    calls: 0,
    count_objects: Hash.new(0),
    num_collection_calls: 0,
    sort_order: nil
  }
end
instrument(method) { || ... } click to toggle source
# File lib/profile_tools/collector.rb, line 35
def instrument(method)
  current_collection_calls = @total_collection_calls
  result = nil
  duration = nil
  @methods[method][:sort_order] ||= (@sort_order += 1)
  count_objects = count_objects_around do
    started_at = now
    result = yield
    duration = now - started_at
  end
  add(
    method,
    duration * 1000.0,
    count_objects,
    @total_collection_calls - current_collection_calls
  )
  result
end

Private Instance Methods

add(method, duration, count_object_changes, num_collection_calls) click to toggle source
# File lib/profile_tools/collector.rb, line 56
def add(method, duration, count_object_changes, num_collection_calls)
  @total_collection_calls += 1
  @methods[method][:calls] += 1
  @methods[method][:duration] += duration
  @methods[method][:num_collection_calls] = num_collection_calls
  add_object_changes(@methods[method][:count_objects], count_object_changes)
  adjust_count_objects(@methods[method][:count_objects], num_collection_calls)
end
add_object_changes(current_objects, new_objects) click to toggle source
# File lib/profile_tools/collector.rb, line 65
def add_object_changes(current_objects, new_objects)
  new_objects.each do |name, cnt|
    current_objects[name] += cnt
  end
  current_objects
end
adjust_count_objects(count_objects, num_collection_calls) click to toggle source
# File lib/profile_tools/collector.rb, line 72
def adjust_count_objects(count_objects, num_collection_calls)
  return if num_collection_calls.zero?

  count_objects[:T_STRING] -= (1 * num_collection_calls)
  count_objects[:T_ARRAY] -= (1 * num_collection_calls)
  count_objects[:T_HASH] -= (2 * num_collection_calls)
end
count_objects_around() { || ... } click to toggle source
# File lib/profile_tools/collector.rb, line 91
def count_objects_around
  starting_objects = ObjectSpace.count_objects
  yield
  count_objects_changes(starting_objects, ObjectSpace.count_objects)
end
count_objects_changes(starting_objects, new_objects) click to toggle source
# File lib/profile_tools/collector.rb, line 84
def count_objects_changes(starting_objects, new_objects)
  new_objects.each do |name, _|
    new_objects[name] -= starting_objects[name]
    new_objects[name] -= 1 if name == :T_HASH
  end
end
now() click to toggle source
# File lib/profile_tools/collector.rb, line 80
def now
  Concurrent.monotonic_time
end