class MicroProfiler

Constants

TIME_PRECISION
VERSION

Attributes

garbage_collection[R]
time[RW]

Public Class Methods

measure(garbage_collection: false, block: Proc.new) click to toggle source
# File lib/micro_profiler.rb, line 7
def self.measure(garbage_collection: false, block: Proc.new)
  new(garbage_collection: garbage_collection).measure(block: block)
end
new(garbage_collection: false) click to toggle source
# File lib/micro_profiler.rb, line 11
def initialize(garbage_collection: false)
  @garbage_collection = garbage_collection
end

Public Instance Methods

measure(result: nil, block: Proc.new) click to toggle source
# File lib/micro_profiler.rb, line 15
def measure(result: nil, block: Proc.new)
  calling_method
  before
  self.time = Benchmark.realtime { result = block.call }
  after

  print_measurements
  result
end

Private Instance Methods

after() click to toggle source
# File lib/micro_profiler.rb, line 30
def after
  ending_memory_usage
  ending_garbage_collection_count
end
before() click to toggle source
# File lib/micro_profiler.rb, line 35
def before
  starting_garbage_collection_count
  starting_memory_usage
end
calling_method() click to toggle source
# File lib/micro_profiler.rb, line 40
def calling_method
  @calling_method ||= "##{caller_locations[2].label}:#{caller_locations[2].lineno}"
end
current_garbage_collection_count() click to toggle source
# File lib/micro_profiler.rb, line 44
def current_garbage_collection_count
  GC.stat[:count].to_i
end
current_memory_usage() click to toggle source
# File lib/micro_profiler.rb, line 48
def current_memory_usage
  `ps -o rss= -p #{Process.pid}`.to_i / 1024
end
ending_garbage_collection_count() click to toggle source
# File lib/micro_profiler.rb, line 52
def ending_garbage_collection_count
  @ending_garbage_collection_count ||= current_garbage_collection_count
end
ending_memory_usage() click to toggle source
# File lib/micro_profiler.rb, line 56
def ending_memory_usage
  @ending_memory_usage ||= current_memory_usage
end
formatted_memory_usage() click to toggle source
# File lib/micro_profiler.rb, line 60
def formatted_memory_usage
  format('%d MB', (ending_memory_usage - starting_memory_usage))
end
garbage_collection_runs() click to toggle source
# File lib/micro_profiler.rb, line 64
def garbage_collection_runs
  ending_garbage_collection_count - starting_garbage_collection_count
end
handle_garbage_collection() click to toggle source
# File lib/micro_profiler.rb, line 68
def handle_garbage_collection
  if garbage_collection
    GC.start
  else
    GC.disable
  end
end
print_measurements() click to toggle source
starting_garbage_collection_count() click to toggle source
# File lib/micro_profiler.rb, line 85
def starting_garbage_collection_count
  @starting_garbage_collection_count ||= current_garbage_collection_count
end
starting_memory_usage() click to toggle source
# File lib/micro_profiler.rb, line 89
def starting_memory_usage
  @starting_memory_usage ||= current_memory_usage
end