lib/drone/metrics/timer.rb in drone-0.0.3 vs lib/drone/metrics/timer.rb in drone-1.0.1
- old
+ new
@@ -1,49 +1,56 @@
-
+require 'forwardable'
require File.expand_path('../histogram', __FILE__)
-require File.expand_path('..//meter', __FILE__)
+require File.expand_path('../meter', __FILE__)
+require File.expand_path('../metric', __FILE__)
module Drone
module Metrics
- class Timer
- attr_reader :name
+ ##
+ # The timer metric will record the time spent in a given method
+ # or any block of code.
+ #
+ # All the times are in milliseconds.
+ #
+ class Timer < Metric
+ extend Forwardable
- def initialize(name = 'calls')
- @name = name
- @histogram = Histogram.new(Histogram::TYPE_BIASED)
- clear()
+ def_delegators :@histogram, :count, :min, :max, :mean, :stdDev, :percentiles, :values
+
+ def initialize(name)
+ super(name)
+
+ @histogram = Histogram.new("#{name}:histogram", :biased)
end
def count
@histogram.count
end
- # may requires a conversion... or not
- [:count, :min, :max, :mean, :stdDev, :percentiles, :values].each do |attr_name|
- define_method(attr_name) do |*args|
- @histogram.send(attr_name, *args)
- end
- end
-
def clear
@histogram.clear()
end
- #
- # duration: milliseconds
+ ##
+ # Method used to record a new duration
+ #
+ # @param [Float] duration A duration in milliseconds
+ #
def update(duration)
if duration >= 0
@histogram.update(duration)
end
end
- #
+ ##
# time and record the duration of the block
+ # @yield [] The block to time
+ #
def time
started_at = Time.now.to_f
yield()
ensure
- update(Time.now.to_f - started_at.to_f)
+ update((Time.now.to_f - started_at.to_f) * 1000)
end
end
end
end