Hitimes::TimedMetric

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.

Attributes

stats[R]
holds all the statistics

Public Class Methods

new( 'name') → TimedMetric
new( 'name', 'other' => 'data') → TimedMetric
click to toggle source

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
now → TimedMetric
click to toggle source

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

Public Instance Methods

measure { ... } → Object
click to toggle source

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
running? → true or false
click to toggle source

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 → Float
click to toggle source

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 → nil
click to toggle source

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 → Float or nil
click to toggle source

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
to_hash → Hash
click to toggle source

Convert the metric to a hash

     # File lib/hitimes/timed_metric.rb, line 163
163:     def to_hash
164:       h = super
165:       Stats::STATS.each do |s|
166:         h[s] = self.send( s ) 
167:       end
168:       return h
169:     end

Disabled; run with $DEBUG to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.