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
# File 'lib/barnes/instruments/stopwatch.rb', line 27

def initialize(timepiece = Timepiece)
  @timepiece = timepiece
end

Instance Method Details

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



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

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

  counters[:'Time.wall'] = wall_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

  state[:stopwatch] = current
end

#start!(state) ⇒ Object



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

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