Class: Necropsy::PerformanceProfiler

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

Overview

Small, dependency-free phase profiler used by benchmark and local analysis. It is opt-in so ordinary reports remain byte-for-byte compatible.

Constant Summary collapse

SCHEMA_VERSION =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(clock: Process.method(:clock_gettime), rss_reader: nil, allocation_reader: nil) ⇒ PerformanceProfiler

Returns a new instance of PerformanceProfiler.



11
12
13
14
15
16
17
# File 'lib/necropsy/performance_profiler.rb', line 11

def initialize(clock: Process.method(:clock_gettime), rss_reader: nil, allocation_reader: nil)
  @clock = clock
  @rss_reader = rss_reader || method(:process_rss_kb)
  @allocation_reader = allocation_reader || method(:allocated_objects)
  @phases = []
  @peak_rss_kb = nil
end

Instance Attribute Details

#phasesObject (readonly)

Returns the value of attribute phases.



19
20
21
# File 'lib/necropsy/performance_profiler.rb', line 19

def phases
  @phases
end

Instance Method Details

#measure(name) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/necropsy/performance_profiler.rb', line 21

def measure(name)
  started = snapshot
  value = yield
  finished = snapshot
  @phases << phase_payload(name, started, finished)
  value
end

#report(counts: {}, report_index_size_bytes: nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/necropsy/performance_profiler.rb', line 29

def report(counts: {}, report_index_size_bytes: nil)
  total_time = @phases.sum { |phase| phase.fetch('wall_time_seconds') }
  total_allocations = @phases.sum { |phase| phase.fetch('allocated_objects') }
  {
    'schema_version' => SCHEMA_VERSION,
    'phases' => @phases.sort_by { |phase| phase.fetch('name') },
    'totals' => {
      'wall_time_seconds' => total_time.round(6),
      'allocated_objects' => total_allocations
    },
    'memory' => {
      'peak_rss_kb' => @peak_rss_kb,
      'rss_status' => @peak_rss_kb ? 'available' : 'unavailable'
    },
    'counts' => normalize_counts(counts),
    'report_index_size_bytes' => report_index_size_bytes
  }.compact
end