Class: Hitimes::Stats

Inherits:
Object
  • Object
show all
Defined in:
ext/hitimes/c/hitimes_stats.c,
lib/hitimes/stats.rb,
ext/hitimes/c/hitimes_stats.c

Overview

The Stats class encapulsates capturing and reporting statistics. It is modeled after the RFuzz::Sampler class, but implemented in C. For general use you allocate a new Stats object, and then update it with new values. The Stats object will keep track of the min, max, count, sum and sumsq and when you want you may also retrieve the mean, stddev and rate.

this contrived example shows getting a list of all the files in a directory and running stats on file sizes.

s = Hitimes::Stats.new
dir = ARGV.shift || Dir.pwd
Dir.entries( dir ).each do |entry|
  fs = File.stat( entry )
  if fs.file? then
    s.update( fs.size )
   end
end

%w[ count min max mean sum stddev rate ].each do |m|
  puts "#{m.rjust(6)} : #{s.send( m ) }"
end

Direct Known Subclasses

MutexedStats

Constant Summary collapse

STATS =

A list of the available stats

%w[ count max mean min rate stddev sum sumsq ]

Instance Method Summary collapse

Instance Method Details

#countInteger

Return the number of values that have passed through the Stats object.

Returns:

  • (Integer)


161
162
163
164
165
166
167
168
# File 'ext/hitimes/c/hitimes_stats.c', line 161

VALUE hitimes_stats_count( VALUE self )
{
    hitimes_stats_t *stats;

    Data_Get_Struct( self, hitimes_stats_t, stats );

    return LONG2NUM( stats->count );
}

#maxFloat

Return the maximum value that has passed through the Stats object

Returns:

  • (Float)


128
129
130
131
132
133
134
135
# File 'ext/hitimes/c/hitimes_stats.c', line 128

VALUE hitimes_stats_max( VALUE self )
{
    hitimes_stats_t *stats;

    Data_Get_Struct( self, hitimes_stats_t, stats );

    return rb_float_new( stats->max );
}

#meanFloat

Return the arithmetic mean of the values put into the Stats object. If no values have passed through the stats object then 0.0 is returned;

Returns:

  • (Float)


77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'ext/hitimes/c/hitimes_stats.c', line 77

VALUE hitimes_stats_mean( VALUE self )
{
    hitimes_stats_t *stats;
    long double      mean = 0.0;

    Data_Get_Struct( self, hitimes_stats_t, stats );

    if ( stats->count > 0 ) {
      mean = stats->sum / stats->count ;
    }

    return rb_float_new( mean );
}

#minFloat

Return the minimum value that has passed through the Stats object

Returns:

  • (Float)


145
146
147
148
149
150
151
152
# File 'ext/hitimes/c/hitimes_stats.c', line 145

VALUE hitimes_stats_min( VALUE self )
{
    hitimes_stats_t *stats;

    Data_Get_Struct( self, hitimes_stats_t, stats );

    return rb_float_new( stats->min );
}

#rateFloat

Return the count divided by sum.

In many cases when Stats#update( value ) is called, the value is a unit of time, typically seconds or microseconds. #rate is a convenience for those times. In this case, where value is a unit if time, then count divided by sum is a useful value, i.e. something per unit of time.

In the case where value is a non-time related value, then the value returned by rate is not really useful.

Returns:

  • (Float)


107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'ext/hitimes/c/hitimes_stats.c', line 107

VALUE hitimes_stats_rate( VALUE self )
{
    hitimes_stats_t *stats;
    long double      rate = 0.0;

    Data_Get_Struct( self, hitimes_stats_t, stats );

    if ( stats->sum > 0.0 ) {
      rate = stats->count / stats->sum;
    }

    return rb_float_new( rate );
}

#stddevFloat

Return the standard deviation of all the values that have passed through the Stats object. The standard deviation has no meaning unless the count is > 1, therefore if the current stat.count is < 1 then 0.0 will be returned;

Returns:

  • (Float)


211
212
213
214
215
216
217
218
219
220
221
222
# File 'ext/hitimes/c/hitimes_stats.c', line 211

VALUE hitimes_stats_stddev ( VALUE self )
{
    hitimes_stats_t *stats;
    long double     stddev = 0.0;

    Data_Get_Struct( self, hitimes_stats_t, stats );
    if ( stats->count > 1 ) {
      stddev = sqrt( ( stats->sumsq - ( stats->sum * stats->sum  / stats->count ) ) / ( stats->count - 1 ) );
    }

    return rb_float_new( stddev );
}

#sumFloat

Return the sum of all the values that have passed through the Stats object.

Returns:

  • (Float)


177
178
179
180
181
182
183
184
# File 'ext/hitimes/c/hitimes_stats.c', line 177

VALUE hitimes_stats_sum( VALUE self )
{
    hitimes_stats_t *stats;

    Data_Get_Struct( self, hitimes_stats_t, stats );

    return rb_float_new( stats->sum );
}

#sumsqFloat

Return the sum of the squars of all the values that passed through the Stats object.

Returns:

  • (Float)


193
194
195
196
197
198
199
200
# File 'ext/hitimes/c/hitimes_stats.c', line 193

VALUE hitimes_stats_sumsq( VALUE self )
{
    hitimes_stats_t *stats;

    Data_Get_Struct( self, hitimes_stats_t, stats );

    return rb_float_new( stats->sumsq );
}

#to_hash(*args) ⇒ Object

call-seq:

stat.to_hash   -> Hash
stat.to_hash( %w[ count max mean ]) -> Hash

return a hash of the stats. By default this returns a hash of all stats but passing in an array of items will limit the stats returned to only those in the Array.

If passed in an empty array or nil to to_hash then STATS is assumed to be the list of stats to return in the hash.



24
25
26
27
28
29
30
31
32
# File 'lib/hitimes/stats.rb', line 24

def to_hash( *args )
  h = {}
  args = [ args ].flatten
  args = STATS if args.empty?
  args.each do |meth|
    h[meth] = self.send( meth )
  end
  return h
end

#to_json(*args) ⇒ Object

call-seq:

stat.to_json  -> String
stat.to_json( *args ) -> String

return a json string of the stats. By default this returns a json string of all the stats. If an array of items is passed in, those that match the known stats will be all that is included in the json output.



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/hitimes/stats.rb', line 43

def to_json( *args )
  h = to_hash( *args )
  a = []
  s = StringIO.new

  s.print "{ "
  h.each_pair do |k,v|
    a << "\"#{k}\": #{v}"
  end
  s.print a.join(", ")
  s.print "}"
  return s.string
end

#update(val) ⇒ Object

Update the running stats with the new value. Return the input value.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'ext/hitimes/c/hitimes_stats.c', line 47

VALUE hitimes_stats_update( VALUE self, VALUE v )
{
    long double      new_v;
    hitimes_stats_t *stats;

    Data_Get_Struct( self, hitimes_stats_t, stats );
    new_v = NUM2DBL( v );

    if ( 0 == stats->count ) {
      stats->min = new_v;
      stats->max = new_v;
    } else {
      stats->min = ( new_v < stats->min) ? ( new_v ) : ( stats->min );
      stats->max = ( new_v > stats->max) ? ( new_v ) : ( stats->max );
    }

    stats->count += 1;
    stats->sum   += new_v;
    stats->sumsq += ( new_v * new_v );

    return v;
}