Sha256: d861b9e337173b31515463fab08be89bd7f6f2a4b92c493559832091b218ae5e

Contents?: true

Size: 1.97 KB

Versions: 47

Compression:

Stored size: 1.97 KB

Contents

#--
# Copyright (c) 2008, 2009 Jeremy Hinegardner
# All rights reserved.  See LICENSE and/or COPYING for details.
#++

require 'forwardable'
module Hitimes
  #
  # A ValueMetric holds the data from measuring a single value over a period of
  # time.  In most cases this may be a single measurement at a single point in
  # time.
  #
  # A good example of a ValueMetric is measuring the number of items in a queue.
  #
  # A ValueMetric contains a Stats object, therefore ValueMetric has +count+, +max+, 
  # +mean+, +min+, +stddev+, +sum+, +sumsq+ methods that delegate to that Stats
  # object for convenience.
  #
  class ValueMetric < Metric

    # holds all the statistics
    attr_reader :stats
    
    #
    # :call-seq:
    #   ValueMetric.new( 'my_metric' ) -> ValueMetric
    #   ValueMetric.new( 'my_metric', 'foo' => 'bar', 'this' => 42 ) -> ValueMetric
    #
    # Create a new ValueMetric giving it a name and additional data.
    # +additional_data+ may be anything that follows the +to_hash+ protocol.
    #
    def initialize( name, additional_data = {} )
      super( name, additional_data )
      @stats = Stats.new
    end

    #
    # :call-seq:
    #   metric.measure( value ) -> Float
    #
    # Give the +value+ as the measurement to the metric.  The value is returned
    #
    def measure( value )
      @sampling_start_time ||= self.utc_microseconds()
      @sampling_start_interval ||= Interval.now

      @stats.update( value )
      
      # update the length of time we have been sampling
      @sampling_delta = @sampling_start_interval.duration_so_far
    end

    #
    # :call-seq:
    #   metric.to_hash -> Hash
    #   
    # Convert the metric to a hash
    #
    def to_hash
      h = super
      (Stats::STATS - %w[ rate ]).each do |s|
        h[s] = self.send( s ) 
      end
      return h
    end

    # forward appropriate calls directly to the stats object
    extend Forwardable
    def_delegators :@stats, :count, :max, :mean, :min, :stddev, :sum, :sumsq
  end
end

Version data entries

47 entries across 45 versions & 4 rubygems

Version Path
honeybadger-2.4.0 vendor/gems/ruby/1.9.1/gems/hitimes-1.2.2/lib/hitimes/value_metric.rb
hitimes-1.2.3-x86-mingw32 lib/hitimes/value_metric.rb
hitimes-1.2.3-java lib/hitimes/value_metric.rb
hitimes-1.2.3 lib/hitimes/value_metric.rb
scoot-0.0.4 .bundle/gems/ruby/2.2.0/gems/hitimes-1.2.2/lib/hitimes/value_metric.rb
vagrant-cloudstack-1.1.0 vendor/bundle/gems/hitimes-1.2.2/lib/hitimes/value_metric.rb
hitimes-1.2.2-x86-mingw32 lib/hitimes/value_metric.rb
hitimes-1.2.2-x86-mswin32-60 lib/hitimes/value_metric.rb
hitimes-1.2.2-java lib/hitimes/value_metric.rb
hitimes-1.2.2 lib/hitimes/value_metric.rb
hitimes-1.2.1-java lib/hitimes/value_metric.rb
hitimes-1.2.1-x86-mswin32 lib/hitimes/value_metric.rb
hitimes-1.2.1 lib/hitimes/value_metric.rb
hitimes-1.2.0-x86-mswin32 lib/hitimes/value_metric.rb
hitimes-1.2.0-java lib/hitimes/value_metric.rb
hitimes-1.2.0 lib/hitimes/value_metric.rb
hitimes-1.1.1-java lib/hitimes/value_metric.rb
hitimes-1.1.1-x86-mswin32 lib/hitimes/value_metric.rb
hitimes-1.1.1-x86-mingw32 lib/hitimes/value_metric.rb
hitimes-1.1.1 lib/hitimes/value_metric.rb