Parent

Mongrel::Stats

Attributes

sum[R]
sumsq[R]
n[R]
min[R]
max[R]

Public Class Methods

new(name) click to toggle source
    # File lib/mongrel/stats.rb, line 19
19:     def initialize(name)
20:       @name = name
21:       reset
22:     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
48:     def dump(msg = "", out=STDERR)
49:       out.puts "#{msg}: #{self.to_s}"
50:     end
mean() click to toggle source

Calculates and returns the mean for the data passed so far.

    # File lib/mongrel/stats.rb, line 59
59:     def mean
60:       @sum / @n
61:     end
reset() click to toggle source

Resets the internal counters so you can start sampling again.

    # File lib/mongrel/stats.rb, line 25
25:     def reset
26:       @sum = 0.0
27:       @sumsq = 0.0
28:       @last_time = Time.new
29:       @n = 0.0
30:       @min = 0.0
31:       @max = 0.0
32:     end
sample(s) click to toggle source

Adds a sampling to the calculations.

    # File lib/mongrel/stats.rb, line 35
35:     def sample(s)
36:       @sum += s
37:       @sumsq += s * s
38:       if @n == 0
39:         @min = @max = s
40:       else
41:         @min = s if @min > s
42:         @max = s if @max < s
43:       end
44:       @n+=1
45:     end
sd() click to toggle source

Calculates the standard deviation of the data so far.

    # File lib/mongrel/stats.rb, line 64
64:     def sd
65:       # (sqrt( ((s).sumsq - ( (s).sum * (s).sum / (s).n)) / ((s).n-1) ))
66:       begin
67:         return Math.sqrt( (@sumsq - ( @sum * @sum / @n)) / (@n-1) )
68:       rescue Errno::EDOM
69:         return 0.0
70:       end
71:     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
83:     def tick
84:       now = Time.now
85:       sample(now - @last_time)
86:       @last_time = now
87:     end
to_s() click to toggle source

Returns a common display (used by dump)

    # File lib/mongrel/stats.rb, line 53
53:     def to_s  
54:     "[#{@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]
55:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.