Class: Leafy::Core::Timer

Inherits:
Object
  • Object
show all
Defined in:
lib/leafy/core/timer.rb

Overview

A timer metric which aggregates timing durations and provides duration statistics, plus throughput statistics via Meter.

Defined Under Namespace

Classes: Context

Instance Method Summary collapse

Constructor Details

#initialize(reservoir = SlidingWindowReservoir, clock = Clock.default_clock) ⇒ Timer

Creates a new Timer that uses the given Reservoir and Clock.

Parameters:

  • reservoir (defaults to: SlidingWindowReservoir)

    the Reservoir implementation the timer should use

  • clock (defaults to: Clock.default_clock)

    the Clock implementation the timer should use



37
38
39
40
41
# File 'lib/leafy/core/timer.rb', line 37

def initialize(reservoir = SlidingWindowReservoir, clock = Clock.default_clock)
  @meter = Meter.new(clock)
  @clock = clock
  @histogram = Histogram.new(reservoir)
end

Instance Method Details

#context(&block) ⇒ Object

Returns a new Context.

Returns:

  • a new Context

See Also:



70
71
72
73
74
75
76
77
78
# File 'lib/leafy/core/timer.rb', line 70

def context(&block)
  ctx = Context.new(self, @clock)
  if block_given?
    block.call ctx
    ctx.stop
  else
    ctx
  end
end

#countObject



80
81
82
# File 'lib/leafy/core/timer.rb', line 80

def count
  @histogram.count
end

#fifteen_minute_rateObject



84
85
86
# File 'lib/leafy/core/timer.rb', line 84

def fifteen_minute_rate
  @meter.fifteen_minute_rate
end

#five_minute_rateObject



88
89
90
# File 'lib/leafy/core/timer.rb', line 88

def five_minute_rate
  @meter.five_minute_rate
end

#mean_rateObject



96
97
98
# File 'lib/leafy/core/timer.rb', line 96

def mean_rate
  @meter.mean_rate
end

#one_minute_rateObject



92
93
94
# File 'lib/leafy/core/timer.rb', line 92

def one_minute_rate
  @meter.one_minute_rate
end

#snapshotObject



100
101
102
# File 'lib/leafy/core/timer.rb', line 100

def snapshot
  @histogram.snapshot
end

#time(&block) ⇒ Object

Times and records the duration of event.

Parameters:

  • event

    a Runnable whose Runnable#run() method implements a process whose duration should be timed



57
58
59
60
61
62
63
64
# File 'lib/leafy/core/timer.rb', line 57

def time(&block)
  startTime = @clock.tick
  begin 
    block.call
  ensure
    update((@clock.tick - startTime) / 1000000000.0)
  end
end

#update(duration) ⇒ Object

Adds a recorded duration.

Parameters:

  • duration

    the length of the duration in seconds



46
47
48
49
50
51
# File 'lib/leafy/core/timer.rb', line 46

def update(duration)
  if duration >= 0
    @histogram.update(duration * 1000000000.0)
    @meter.mark
  end
end