Class: ExecutionTime::Measurer

Inherits:
Object
  • Object
show all
Defined in:
lib/execution_time.rb

Class Method Summary collapse

Class Method Details

.watchObject



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
# 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