Class: Hitimes::TimedMetric

Inherits:
Metric
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/hitimes/timed_metric.rb

Overview

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.

Instance Attribute Summary collapse

Attributes inherited from Metric

#additional_data, #name, #sampling_delta

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Metric

#sampling_start_time, #sampling_stop_time, #utc_microseconds

Constructor Details

#initialize(name, additional_data = {}) ⇒ TimedMetric

:call-seq:

TimedMetric.new( 'name') -> TimedMetric
TimedMetric.new( 'name', 'other' => 'data') -> TimedMetric

Create a new TimedMetric giving it a name and additional data. additional_data may be anything that follows the to_hash protocol



61
62
63
64
65
# File 'lib/hitimes/timed_metric.rb', line 61

def initialize(name, additional_data = {})
  super(name, additional_data)
  @stats            = Stats.new
  @current_interval = Interval.new
end

Instance Attribute Details

#statsObject (readonly)

holds all the statistics



37
38
39
# File 'lib/hitimes/timed_metric.rb', line 37

def stats
  @stats
end

Class Method Details

.now(name, additional_data = {}) ⇒ Object

:call-seq:

TimedMetric.now -> TimedMetric

Return a TimedMetric that has been started



46
47
48
49
50
# File 'lib/hitimes/timed_metric.rb', line 46

def now(name, additional_data = {})
  tm = TimedMetric.new(name, additional_data)
  tm.start
  tm
end

Instance Method Details

#measureObject

:call-seq:

timed_metric.measure {  ... } -> Object

Measure the execution of a block and add those stats to the running stats. The return value is the return value of the block



123
124
125
126
127
128
129
130
131
132
# File 'lib/hitimes/timed_metric.rb', line 123

def measure
  return_value = nil
  begin
    start
    return_value = yield
  ensure
    stop
  end
  return_value
end

#running?Boolean

:call-seq:

timed_metric.running? -> true or false

return whether or not the timer is currently running.

Returns:

  • (Boolean)


73
74
75
# File 'lib/hitimes/timed_metric.rb', line 73

def running?
  @current_interval.running?
end

#splitObject

:call-seq:

timed_metric.split -> Float

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.



147
148
149
150
151
152
153
154
155
156
# File 'lib/hitimes/timed_metric.rb', line 147

def split
  if @current_interval.running?
    next_interval = @current_interval.split
    duration = @current_interval.duration
    @stats.update(duration)
    @current_interval = next_interval
    return duration
  end
  false
end

#startObject

:call-seq:

timed_metric.start -> nil

Start the current metric, if the current metric is already started, then this is a noop.



84
85
86
87
88
89
90
91
# File 'lib/hitimes/timed_metric.rb', line 84

def start
  unless @current_interval.running?
    @current_interval.start
    @sampling_start_time ||= utc_microseconds
    @sampling_start_interval ||= Interval.now
  end
  nil
end

#stopObject

:call-seq:

timed_metric.stop -> Float or nil

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.



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/hitimes/timed_metric.rb', line 102

def stop
  if @current_interval.running?
    duration = @current_interval.stop
    @stats.update(duration)
    @current_interval = Interval.new

    # update the length of time we have been sampling
    @sampling_delta = @sampling_start_interval.duration_so_far

    return duration
  end
  false
end

#to_hashObject

:call-seq:

metric.to_hash -> Hash

Convert the metric to a hash



164
165
166
167
168
169
170
# File 'lib/hitimes/timed_metric.rb', line 164

def to_hash
  result = super
  Stats::STATS.each do |stat|
    result[stat] = send(stat)
  end
  result
end