Class: Leafy::Core::Timer
- Inherits:
-
Object
- Object
- Leafy::Core::Timer
- 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
-
#context(&block) ⇒ Object
Returns a new Context.
- #count ⇒ Object
- #fifteen_minute_rate ⇒ Object
- #five_minute_rate ⇒ Object
-
#initialize(reservoir = SlidingWindowReservoir, clock = Clock.default_clock) ⇒ Timer
constructor
Creates a new Timer that uses the given Reservoir and Clock.
- #mean_rate ⇒ Object
- #one_minute_rate ⇒ Object
- #snapshot ⇒ Object
-
#time(&block) ⇒ Object
Times and records the duration of event.
-
#update(duration) ⇒ Object
Adds a recorded duration.
Constructor Details
#initialize(reservoir = SlidingWindowReservoir, clock = Clock.default_clock) ⇒ Timer
Creates a new Timer that uses the given Reservoir and Clock.
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.
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 |
#count ⇒ Object
80 81 82 |
# File 'lib/leafy/core/timer.rb', line 80 def count @histogram.count end |
#fifteen_minute_rate ⇒ Object
84 85 86 |
# File 'lib/leafy/core/timer.rb', line 84 def fifteen_minute_rate @meter.fifteen_minute_rate end |
#five_minute_rate ⇒ Object
88 89 90 |
# File 'lib/leafy/core/timer.rb', line 88 def five_minute_rate @meter.five_minute_rate end |
#mean_rate ⇒ Object
96 97 98 |
# File 'lib/leafy/core/timer.rb', line 96 def mean_rate @meter.mean_rate end |
#one_minute_rate ⇒ Object
92 93 94 |
# File 'lib/leafy/core/timer.rb', line 92 def one_minute_rate @meter.one_minute_rate end |
#snapshot ⇒ Object
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.
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.
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 |