Class: Stats
- Inherits:
-
Object
- Object
- Stats
- Defined in:
- lib/mongrel/stats.rb
Overview
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 Stats.tick to get it to add a time delta between the last time you called it. When you’re done either call sum, sumsq, n, min, max, mean or sd to get the information. The other option is to just call dump 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.
Instance Attribute Summary collapse
-
#max ⇒ Object
readonly
Returns the value of attribute max.
-
#min ⇒ Object
readonly
Returns the value of attribute min.
-
#n ⇒ Object
readonly
Returns the value of attribute n.
-
#sum ⇒ Object
readonly
Returns the value of attribute sum.
-
#sumsq ⇒ Object
readonly
Returns the value of attribute sumsq.
Instance Method Summary collapse
-
#dump(msg = "", out = STDERR) ⇒ Object
Dump this Stats object with an optional additional message.
-
#initialize(name) ⇒ Stats
constructor
A new instance of Stats.
-
#mean ⇒ Object
Calculates and returns the mean for the data passed so far.
-
#reset ⇒ Object
Resets the internal counters so you can start sampling again.
-
#sample(s) ⇒ Object
Adds a sampling to the calculations.
-
#sd ⇒ Object
Calculates the standard deviation of the data so far.
-
#tick ⇒ Object
Adds a time delta between now and the last time you called this.
Constructor Details
#initialize(name) ⇒ Stats
Returns a new instance of Stats.
12 13 14 15 |
# File 'lib/mongrel/stats.rb', line 12 def initialize(name) @name = name reset end |
Instance Attribute Details
#max ⇒ Object (readonly)
Returns the value of attribute max.
10 11 12 |
# File 'lib/mongrel/stats.rb', line 10 def max @max end |
#min ⇒ Object (readonly)
Returns the value of attribute min.
10 11 12 |
# File 'lib/mongrel/stats.rb', line 10 def min @min end |
#n ⇒ Object (readonly)
Returns the value of attribute n.
10 11 12 |
# File 'lib/mongrel/stats.rb', line 10 def n @n end |
#sum ⇒ Object (readonly)
Returns the value of attribute sum.
10 11 12 |
# File 'lib/mongrel/stats.rb', line 10 def sum @sum end |
#sumsq ⇒ Object (readonly)
Returns the value of attribute sumsq.
10 11 12 |
# File 'lib/mongrel/stats.rb', line 10 def sumsq @sumsq end |
Instance Method Details
#dump(msg = "", out = STDERR) ⇒ Object
Dump this Stats object with an optional additional message.
41 42 43 |
# File 'lib/mongrel/stats.rb', line 41 def dump(msg = "", out=STDERR) out.puts "[#{@name}] #{msg} : SUM=#@sum, SUMSQ=#@sumsq, N=#@n, MEAN=#{mean}, SD=#{sd}, MIN=#@min, MAX=#@max" end |
#mean ⇒ Object
Calculates and returns the mean for the data passed so far.
46 47 48 |
# File 'lib/mongrel/stats.rb', line 46 def mean @sum / @n end |
#reset ⇒ Object
Resets the internal counters so you can start sampling again.
18 19 20 21 22 23 24 25 |
# File 'lib/mongrel/stats.rb', line 18 def reset @sum = 0.0 @sumsq = 0.0 @last_time = Time.new @n = 0.0 @min = 0.0 @max = 0.0 end |
#sample(s) ⇒ Object
Adds a sampling to the calculations.
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/mongrel/stats.rb', line 28 def sample(s) @sum += s @sumsq += s * s if @n == 0 @min = @max = s else @min = s if @min > s @max = s if @max < s end @n+=1 end |
#sd ⇒ Object
Calculates the standard deviation of the data so far.
51 52 53 54 |
# File 'lib/mongrel/stats.rb', line 51 def sd # (sqrt( ((s).sumsq - ( (s).sum * (s).sum / (s).n)) / ((s).n-1) )) Math.sqrt( (@sumsq - ( @sum * @sum / @n)) / (@n-1) ) end |
#tick ⇒ Object
66 67 68 69 70 |
# File 'lib/mongrel/stats.rb', line 66 def tick now = Time.now sample(now - @last_time) @last_time = now end |