Class: Barnes::Instruments::Stopwatch

Inherits:
Object
  • Object
show all
Defined in:
lib/barnes/instruments/stopwatch.rb

Instance Method Summary collapse

Constructor Details

#initialize(timepiece = Timepiece) ⇒ Stopwatch

Returns a new instance of Stopwatch.



27
28
29
30
# File 'lib/barnes/instruments/stopwatch.rb', line 27

def initialize(timepiece = Timepiece)
  @timepiece = timepiece
  @has_cpu_time = timepiece.respond_to?(:cpu)
end

Instance Method Details

#instrument!(state, counters, gauges) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/barnes/instruments/stopwatch.rb', line 36

def instrument!(state, counters, gauges)
  last = state[:stopwatch]
  wall_elapsed = @timepiece.wall - last[:wall]
  counters[:'Time.wall'] = wall_elapsed

  if @has_cpu_time
    cpu_elapsed = @timepiece.cpu - last[:cpu]
    idle_elapsed = wall_elapsed - cpu_elapsed

    counters[:'Time.cpu']      = cpu_elapsed
    counters[:'Time.idle']     = idle_elapsed

    if wall_elapsed == 0
      counters[:'Time.pct.cpu']  = 0
      counters[:'Time.pct.idle'] = 0
    else
      counters[:'Time.pct.cpu']  = 100.0 * cpu_elapsed  / wall_elapsed
      counters[:'Time.pct.idle'] = 100.0 * idle_elapsed / wall_elapsed
    end
  end

  state[:stopwatch] = current
end

#start!(state) ⇒ Object



32
33
34
# File 'lib/barnes/instruments/stopwatch.rb', line 32

def start!(state)
  state[:stopwatch] = current
end