class Mongrel::Stats
Attributes
max[R]
min[R]
n[R]
sum[R]
sumsq[R]
Public Class Methods
new(name)
click to toggle source
# File lib/mongrel/stats.rb, line 19 def initialize(name) @name = name reset end
Public Instance Methods
dump(msg = "", out=STDERR)
click to toggle source
Dump this Stats
object with an optional additional message.
# File lib/mongrel/stats.rb, line 48 def dump(msg = "", out=STDERR) out.puts "#{msg}: #{self.to_s}" end
mean()
click to toggle source
Calculates and returns the mean for the data passed so far.
# File lib/mongrel/stats.rb, line 59 def mean @sum / @n end
reset()
click to toggle source
Resets the internal counters so you can start sampling again.
# File lib/mongrel/stats.rb, line 25 def reset @sum = 0.0 @sumsq = 0.0 @last_time = Time.new @n = 0.0 @min = 0.0 @max = 0.0 end
sample(s)
click to toggle source
Adds a sampling to the calculations.
# File lib/mongrel/stats.rb, line 35 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()
click to toggle source
Calculates the standard deviation of the data so far.
# File lib/mongrel/stats.rb, line 64 def sd # (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()
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 = Stats.new("do_stuff") 10000.times { do_stuff(); t.tick } t.dump("time")
# File lib/mongrel/stats.rb, line 83 def tick now = Time.now sample(now - @last_time) @last_time = now end
to_s()
click to toggle source
Returns a common display (used by dump)
# File lib/mongrel/stats.rb, line 53 def to_s "[#{@name}]: SUM=%0.4f, SUMSQ=%0.4f, N=%0.4f, MEAN=%0.4f, SD=%0.4f, MIN=%0.4f, MAX=%0.4f" % [@sum, @sumsq, @n, mean, sd, @min, @max] end