Class: Trashed::Instruments::Stopwatch

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

Instance Method Summary collapse

Constructor Details

#initialize(timepiece = Timepiece) ⇒ Stopwatch

Returns a new instance of Stopwatch.



4
5
6
7
# File 'lib/trashed/instruments/stopwatch.rb', line 4

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

Instance Method Details

#measure(state, timings, gauges) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/trashed/instruments/stopwatch.rb', line 14

def measure(state, timings, gauges)
  wall_elapsed = @timepiece.wall - state.delete(:stopwatch_wall)
  timings[:'Time.wall'] = wall_elapsed
  if @has_cpu_time
    cpu_elapsed = @timepiece.cpu - state.delete(:stopwatch_cpu)
    idle_elapsed = wall_elapsed - cpu_elapsed

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

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

#start(state, timings, gauges) ⇒ Object



9
10
11
12
# File 'lib/trashed/instruments/stopwatch.rb', line 9

def start(state, timings, gauges)
  state[:stopwatch_wall] = @timepiece.wall
  state[:stopwatch_cpu]  = @timepiece.cpu if @has_cpu_time
end