class ExecutionTime::Measurer

Public Class Methods

watch() { || ... } click to toggle source
# File lib/execution_time.rb, line 45
def Measurer.watch
  return yield unless ExecutionTime.enabled

  AppMetrics.reset

  start     = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  before    = GC.stat(:total_allocated_objects)
  ActiveRecord::LogSubscriber.reset_runtime

  result   = yield

  after    = GC.stat(:total_allocated_objects)
  duration = (Process.clock_gettime(Process::CLOCK_MONOTONIC) - start)
  db_after = ActiveRecord::LogSubscriber.reset_runtime

  info = "Completed in #{(duration * 1000).round(1)}ms | Allocations: #{after - before}"

  if AppMetrics.counter > 0
    info << " | ActiveRecord: #{db_after.round(1)}ms"
    info << " (queries: #{AppMetrics.counter})"
  end

  puts AppMetrics.with_color(info)
  result
end