Object
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.
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
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
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
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
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
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
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
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
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.