Parent

Logging::Stats::Sampler

A very simple little class for doing some basic fast statistics sampling. You feed it either samples of numeric data you want measured or you call Sampler#tick to get it to add a time delta between the last time you called it. When you’re done either call sum, sumsq, num, min, max, mean or sd to get the information. The other option is to just call to_s and see everything.

It does all of this very fast and doesn’t take up any memory since the samples are not stored but instead all the values are calculated on the fly.

Attributes

name[R]
sum[R]
sumsq[R]
num[R]
min[R]
max[R]
last[R]

Public Class Methods

keys() click to toggle source

Class method that returns the headers that a CSV file would have for the values that this stats object is using.

    # File lib/logging/stats.rb, line 88
88:     def self.keys
89:       ]name sum sumsq num mean sd min max]
90:     end
new( name ) click to toggle source

Create a new sampler.

    # File lib/logging/stats.rb, line 22
22:     def initialize( name )
23:       @name = name
24:       reset
25:     end

Public Instance Methods

coalesce( other ) click to toggle source

Coalesce the statistics from the other sampler into this one. The other sampler is not modified by this method.

Coalescing the same two samplers multiple times should only be done if one of the samplers is reset between calls to this method. Otherwise statistics will be counted multiple times.

    # File lib/logging/stats.rb, line 47
47:     def coalesce( other )
48:       @sum += other.sum
49:       @sumsq += other.sumsq
50:       if other.num > 0
51:         @min = other.min if @min > other.min
52:         @max = other.max if @max < other.max
53:         @last = other.last
54:       end
55:       @num += other.num
56:     end
mark() click to toggle source

You can just call tick repeatedly if you need the delta times between a set of sample periods, but many times you actually want to sample how long something takes between a start/end period. Call mark at the beginning and then tick at the end you’ll get this kind of measurement. Don’t mix mark/tick and tick sampling together or the measurement will be meaningless.

     # File lib/logging/stats.rb, line 124
124:     def mark
125:       @last_time = Time.now.to_f
126:     end
mean() click to toggle source

Calculates and returns the mean for the data passed so far.

     # File lib/logging/stats.rb, line 99
 99:     def mean
100:       return 0.0 if num < 1
101:       sum / num
102:     end
reset() click to toggle source

Resets the internal counters so you can start sampling again.

    # File lib/logging/stats.rb, line 29
29:     def reset
30:       @sum = 0.0
31:       @sumsq = 0.0
32:       @num = 0
33:       @min = 0.0
34:       @max = 0.0
35:       @last = nil
36:       @last_time = Time.now.to_f
37:       self
38:     end
sample( s ) click to toggle source

Adds a sampling to the calculations.

    # File lib/logging/stats.rb, line 60
60:     def sample( s )
61:       @sum += s
62:       @sumsq += s * s
63:       if @num == 0
64:         @min = @max = s
65:       else
66:         @min = s if @min > s
67:         @max = s if @max < s
68:       end
69:       @num += 1
70:       @last = s
71:     end
sd() click to toggle source

Calculates the standard deviation of the data so far.

     # File lib/logging/stats.rb, line 106
106:     def sd
107:       return 0.0 if num < 2
108: 
109:       # (sqrt( ((s).sumsq - ( (s).sum * (s).sum / (s).num)) / ((s).num-1) ))
110:       begin
111:         return Math.sqrt( (sumsq - ( sum * sum / num)) / (num-1) )
112:       rescue Errno::EDOM
113:         return 0.0
114:       end
115:     end
tick() click to toggle source

Adds a time delta between now and the last time you called this. This will give you the average time between two activities.

An example is:

 t = Sampler.new("do_stuff")
 10000.times { do_stuff(); t.tick }
 t.dump("time")
     # File lib/logging/stats.rb, line 137
137:     def tick
138:       now = Time.now.to_f
139:       sample(now - @last_time)
140:       @last_time = now
141:     end
to_a() click to toggle source

An array of the values: [name,sum,sumsq,num,mean,sd,min,max]

    # File lib/logging/stats.rb, line 81
81:     def to_a
82:       [name, sum, sumsq, num, mean, sd, min, max]
83:     end
to_hash() click to toggle source
    # File lib/logging/stats.rb, line 92
92:     def to_hash
93:       {:name => name, :sum => sum, :sumsq => sumsq, :num => num,
94:        :mean => mean, :sd => sd, :min => min, :max => max}
95:     end
to_s() click to toggle source

Returns statistics in a common format.

    # File lib/logging/stats.rb, line 75
75:     def to_s
76:       "[%s]: SUM=%0.6f, SUMSQ=%0.6f, NUM=%d, MEAN=%0.6f, SD=%0.6f, MIN=%0.6f, MAX=%0.6f" % to_a
77:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.