Class: Hitimes::TimedMetric

### Home Classes Methods

### In Files

- lib/hitimes/timed_metric.rb

### Parent

Hitimes::Metric

### Methods

- ::new
- ::now
- #measure
- #running?
- #split
- #start
- #stop
- #to_hash

### Files

- Manifest.txt

### Class/Module Index

Quicksearch

- Hitimes
- Hitimes::Error
- Hitimes::Metric
- Hitimes::MutexedStats
- Hitimes::Paths
- Hitimes::Stats
- Hitimes::TimedMetric
- Hitimes::TimedValueMetric
- Hitimes::ValueMetric

No matching classes.

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

    # File lib/hitimes/timed_metric.rb, line 59
    def initialize( name, additional_data = {} )
      super( name, additional_data )
      @stats            = Stats.new
      @current_interval = Interval.new
    end

now → TimedMetric click to toggle source

Return a TimedMetric that has been started

    # File lib/hitimes/timed_metric.rb, line 44
    def now( name, additional_data = {} )
      t = TimedMetric.new( name, additional_data )
      t.start
      return t
    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 121
    def measure( &block )
      return_value = nil
      begin
        start
        return_value = yield
      ensure
        stop
      end
      return return_value
    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 71
    def running?
      @current_interval.running?
    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 145
    def split
      if @current_interval.running? then
        next_interval = @current_interval.split
        d = @current_interval.duration
        @stats.update( d )
        @current_interval = next_interval
        return d
      end
      return false
    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 82
    def start
      if not @current_interval.running? then
        @current_interval.start
        @sampling_start_time ||= self.utc_microseconds()
        @sampling_start_interval ||= Interval.now
      end
      nil
    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 100
    def stop
      if @current_interval.running? then
        d = @current_interval.stop
        @stats.update( d )
        @current_interval = Interval.new

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

        return d
      end
      return false
    end

to_hash → Hash click to toggle source

Convert the metric to a hash

    # File lib/hitimes/timed_metric.rb, line 162
    def to_hash
      h = super
      Stats::STATS.each do |s|
        h[s] = self.send( s )
      end
      return h
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.