diff options
author | zedshaw <zedshaw@19e92222-5c0b-0410-8929-a290d50e31e9> | 2006-09-22 08:16:54 +0000 |
---|---|---|
committer | zedshaw <zedshaw@19e92222-5c0b-0410-8929-a290d50e31e9> | 2006-09-22 08:16:54 +0000 |
commit | e1d423ab4d835b8eea4e42444cc189bcdf79ec95 (patch) | |
tree | aaf05aed8a91fe5b6a3bb23b00e03c97bd281bcd /lib/mongrel/stats.rb | |
parent | 5f0c661e6a08793180e9b4414da4b197b33b0c78 (diff) | |
download | unicorn-e1d423ab4d835b8eea4e42444cc189bcdf79ec95.tar.gz |
git-svn-id: svn+ssh://rubyforge.org/var/svn/mongrel/trunk@349 19e92222-5c0b-0410-8929-a290d50e31e9
Diffstat (limited to 'lib/mongrel/stats.rb')
-rw-r--r-- | lib/mongrel/stats.rb | 118 |
1 files changed, 60 insertions, 58 deletions
diff --git a/lib/mongrel/stats.rb b/lib/mongrel/stats.rb index eec6ff9..f6cf5ab 100644 --- a/lib/mongrel/stats.rb +++ b/lib/mongrel/stats.rb @@ -12,76 +12,78 @@ # # 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. -class Stats - attr_reader :sum, :sumsq, :n, :min, :max +module Mongrel + class Stats + attr_reader :sum, :sumsq, :n, :min, :max - def initialize(name) - @name = name - reset - end + def initialize(name) + @name = name + reset + end - # Resets the internal counters so you can start sampling again. - def reset - @sum = 0.0 - @sumsq = 0.0 - @last_time = Time.new - @n = 0.0 - @min = 0.0 - @max = 0.0 - end + # Resets the internal counters so you can start sampling again. + def reset + @sum = 0.0 + @sumsq = 0.0 + @last_time = Time.new + @n = 0.0 + @min = 0.0 + @max = 0.0 + end - # Adds a sampling to the calculations. - 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 + # Adds a sampling to the calculations. + 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 - @n+=1 - end - # Dump this Stats object with an optional additional message. - def dump(msg = "", out=STDERR) - out.puts "#{msg}: #{self.to_s}" - end + # Dump this Stats object with an optional additional message. + def dump(msg = "", out=STDERR) + out.puts "#{msg}: #{self.to_s}" + end - # Returns a common display (used by dump) - def to_s + # Returns a common display (used by dump) + 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 + end - # Calculates and returns the mean for the data passed so far. - def mean - @sum / @n - end + # Calculates and returns the mean for the data passed so far. + def mean + @sum / @n + end - # Calculates the standard deviation of the data so far. - 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 + # Calculates the standard deviation of the data so far. + 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 - 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 = Stats.new("do_stuff") - # 10000.times { do_stuff(); t.tick } - # t.dump("time") - # - def tick - now = Time.now - sample(now - @last_time) - @last_time = now + # 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") + # + def tick + now = Time.now + sample(now - @last_time) + @last_time = now + end end end |