class Leafy::Core::Timer

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

Public Class Methods

new(reservoir = SlidingWindowReservoir, clock = Clock.default_clock) click to toggle source

Creates a new {@link Timer} that uses the given {@link Reservoir} and {@link Clock}.

@param reservoir the {@link Reservoir} implementation the timer should use @param clock the {@link Clock} implementation the timer should use

# 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

Public Instance Methods

context(&block) click to toggle source

Returns a new {@link Context}.

@return a new {@link Context} @see Context

# 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() click to toggle source
# File lib/leafy/core/timer.rb, line 80
def count
  @histogram.count
end
fifteen_minute_rate() click to toggle source
# File lib/leafy/core/timer.rb, line 84
def fifteen_minute_rate
  @meter.fifteen_minute_rate
end
five_minute_rate() click to toggle source
# File lib/leafy/core/timer.rb, line 88
def five_minute_rate
  @meter.five_minute_rate
end
mean_rate() click to toggle source
# File lib/leafy/core/timer.rb, line 96
def mean_rate
  @meter.mean_rate
end
one_minute_rate() click to toggle source
# File lib/leafy/core/timer.rb, line 92
def one_minute_rate
  @meter.one_minute_rate
end
snapshot() click to toggle source
# File lib/leafy/core/timer.rb, line 100
def snapshot
  @histogram.snapshot
end
time(&block) click to toggle source

Times and records the duration of event.

@param event a {@link Runnable} whose {@link Runnable#run()} method implements a process

whose duration should be timed
# 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) click to toggle source

Adds a recorded duration.

@param duration the length of the duration in seconds

# File lib/leafy/core/timer.rb, line 46
def update(duration)
  if duration >= 0
    @histogram.update(duration * 1000000000.0)
    @meter.mark
  end
end