Class: Omnizip::Profiler::MethodProfiler

Inherits:
Object
  • Object
show all
Defined in:
lib/omnizip/profiler/method_profiler.rb

Overview

Profiles method execution time and call counts

Instance Method Summary collapse

Constructor Details

#initializeMethodProfiler

Returns a new instance of MethodProfiler.



9
10
11
# File 'lib/omnizip/profiler/method_profiler.rb', line 9

def initialize
  @call_counts = Hash.new(0)
end

Instance Method Details

#call_count(operation_name) ⇒ Object



40
41
42
# File 'lib/omnizip/profiler/method_profiler.rb', line 40

def call_count(operation_name)
  @call_counts[operation_name]
end

#profile(operation_name) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/omnizip/profiler/method_profiler.rb', line 13

def profile(operation_name)
  start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  gc_stat_before = GC.stat

  yield

  end_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  gc_stat_after = GC.stat

  @call_counts[operation_name] += 1

  wall_time = end_time - start_time
  gc_runs = gc_stat_after[:count] - gc_stat_before[:count]

  Models::PerformanceResult.new(
    operation_name: operation_name,
    total_time: wall_time,
    wall_time: wall_time,
    gc_runs: gc_runs,
    call_count: @call_counts[operation_name],
  )
end

#reset!Object



36
37
38
# File 'lib/omnizip/profiler/method_profiler.rb', line 36

def reset!
  @call_counts.clear
end

#total_callsObject



44
45
46
# File 'lib/omnizip/profiler/method_profiler.rb', line 44

def total_calls
  @call_counts.values.sum
end