Class: Stella::Stats
- Inherits:
-
Object
- Object
- Stella::Stats
- Defined in:
- lib/stella/stats.rb
Overview
Based on Mongrel::Stats, Copyright © 2005 Zed A. Shaw
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
- #+(obj) ⇒ Object
-
#dump(msg = "", out = STDERR) ⇒ Object
Dump this Stats object with an optional additional message.
-
#initialize(name) ⇒ Stats
constructor
A new instance of Stats.
-
#inspect ⇒ Object
Returns a common display (used by dump).
-
#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.
- #to_f ⇒ Object
- #to_i ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(name) ⇒ Stats
Returns a new instance of Stats.
8 9 10 11 |
# File 'lib/stella/stats.rb', line 8 def initialize(name) @name = name reset end |
Instance Attribute Details
#max ⇒ Object (readonly)
Returns the value of attribute max.
6 7 8 |
# File 'lib/stella/stats.rb', line 6 def max @max end |
#min ⇒ Object (readonly)
Returns the value of attribute min.
6 7 8 |
# File 'lib/stella/stats.rb', line 6 def min @min end |
#n ⇒ Object (readonly)
Returns the value of attribute n.
6 7 8 |
# File 'lib/stella/stats.rb', line 6 def n @n end |
#sum ⇒ Object (readonly)
Returns the value of attribute sum.
6 7 8 |
# File 'lib/stella/stats.rb', line 6 def sum @sum end |
#sumsq ⇒ Object (readonly)
Returns the value of attribute sumsq.
6 7 8 |
# File 'lib/stella/stats.rb', line 6 def sumsq @sumsq end |
Instance Method Details
#+(obj) ⇒ Object
13 14 15 |
# File 'lib/stella/stats.rb', line 13 def +(obj) puts obj.class end |
#dump(msg = "", out = STDERR) ⇒ Object
Dump this Stats object with an optional additional message.
38 39 40 |
# File 'lib/stella/stats.rb', line 38 def dump(msg = "", out=STDERR) out.puts "#{msg}: #{self.inspect}" end |
#inspect ⇒ Object
Returns a common display (used by dump)
43 44 45 46 47 |
# File 'lib/stella/stats.rb', line 43 def inspect v = [mean, @n, @sum, @sumsq, sd, @min, @max] t = %w"N=%0.4f SUM=%0.4f SUMSQ=%0.4f SD=%0.4f MIN=%0.4f MAX=%0.4f" "%0.4f: " << t % v end |
#mean ⇒ Object
Calculates and returns the mean for the data passed so far.
62 63 64 |
# File 'lib/stella/stats.rb', line 62 def mean @sum / @n end |
#reset ⇒ Object
Resets the internal counters so you can start sampling again.
18 19 20 21 22 |
# File 'lib/stella/stats.rb', line 18 def reset @n, @sum, @sumsq = 0.0, 0.0, 0.0 @last_time = Time.new @min, @max = 0.0, 0.0 end |
#sample(s) ⇒ Object
Adds a sampling to the calculations.
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/stella/stats.rb', line 25 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.
67 68 69 70 71 72 73 74 75 |
# File 'lib/stella/stats.rb', line 67 def sd return 0.0 if @n <= 1 # (sqrt( ((s).sumsq - ( (s).sum * (s).sum / (s).n)) / ((s).n-1) )) begin return Math.sqrt( (@sumsq - ( @sum * @sum / @n)) / (@n-1) ) rescue Errno::EDOM return 0.0 end end |
#tick ⇒ Object
87 88 89 90 91 |
# File 'lib/stella/stats.rb', line 87 def tick now = Time.now sample(now - @last_time) @last_time = now end |
#to_f ⇒ Object
53 54 55 |
# File 'lib/stella/stats.rb', line 53 def to_f mean.to_f end |
#to_i ⇒ Object
57 58 59 |
# File 'lib/stella/stats.rb', line 57 def to_i mean.to_i end |
#to_s ⇒ Object
49 50 51 |
# File 'lib/stella/stats.rb', line 49 def to_s mean.to_s end |