A TimedMetric holds the metrics on how long it takes to do something. For example, measuring how long a method takes to operate.
tm = TimedMetric.new( 'my-method' )
200.times do
my_method_result = tm.measure do
my_method( ... )
end
end
puts "#{ tm.name } operated at a rate of #{ tm.rate } calls per second"
Since TimedMetric is a child class of Metric make sure to look at the Metric API also.
A TimedMetric measures the execution time of an option with the Interval class.
A TimedMetric contains a Stats object, therefore TimedMetric has count, max, mean, min, rate, stddev, sum, sumsq methods that delegate to that Stats object for convenience.
Create a new TimedMetric giving it a name and additional data. additional_data may be anything that follows the to_hash protocol
# File lib/hitimes/timed_metric.rb, line 60
60: def initialize( name, additional_data = {} )
61: super( name, additional_data )
62: @stats = Stats.new
63: @current_interval = Interval.new
64: end
Return a TimedMetric that has been started
# File lib/hitimes/timed_metric.rb, line 45
45: def now( name, additional_data = {} )
46: t = TimedMetric.new( name, additional_data )
47: t.start
48: return t
49: end
Measure the execution of a block and add those stats to the running stats. The return value is the return value of the block
# File lib/hitimes/timed_metric.rb, line 122
122: def measure( &block )
123: return_value = nil
124: begin
125: start
126: return_value = yield
127: ensure
128: stop
129: end
130: return return_value
131: end
return whether or not the timer is currently running.
# File lib/hitimes/timed_metric.rb, line 72
72: def running?
73: @current_interval.running?
74: end
Split the current TimedMetric. Essentially, mark a split time. This means stop the current interval and create a new interval, but make sure that the new interval lines up exactly, timewise, behind the previous interval.
If the timer is running, then split returns the duration of the previous interval, i.e. the split-time. If the timer is not running, nothing happens and false is returned.
# File lib/hitimes/timed_metric.rb, line 146
146: def split
147: if @current_interval.running? then
148: next_interval = @current_interval.split
149: d = @current_interval.duration
150: @stats.update( d )
151: @current_interval = next_interval
152: return d
153: end
154: return false
155: end
Start the current metric, if the current metric is already started, then this is a noop.
# File lib/hitimes/timed_metric.rb, line 83
83: def start
84: if not @current_interval.running? then
85: @current_interval.start
86: @sampling_start_time ||= self.utc_microseconds()
87: @sampling_start_interval ||= Interval.now
88: end
89: nil
90: end
Stop the current metric. This updates the stats and removes the current interval. If the timer was stopped then the duration of the last Interval is returned. If the timer was already stopped then false is returned and no stats are updated.
# File lib/hitimes/timed_metric.rb, line 101
101: def stop
102: if @current_interval.running? then
103: d = @current_interval.stop
104: @stats.update( d )
105: @current_interval = Interval.new
106:
107: # update the length of time we have been sampling
108: @sampling_delta = @sampling_start_interval.duration_so_far
109:
110: return d
111: end
112: return false
113: end
Disabled; run with $DEBUG to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.