Class: Mongrel::Stats

Class | Mongrel::Stats
In: | lib/mongrel/stats.rb lib/mongrel/stats.rb
Parent: | Object

### Methods

dump dump mean mean new new reset reset sample sample sd sd tick tick to_s to_s

### Attributes

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

### Public Class methods

new(name)

[Source]

        # File lib/mongrel/stats.rb, line 19
    19:     def initialize(name)
    20:       @name = name
    21:       reset
    22:     end

new(name)

[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)

Dump this Stats object with an optional additional message.

[Source]

        # File lib/mongrel/stats.rb, line 48
    48:     def dump(msg = "", out=STDERR)
    49:       out.puts "#{msg}: #{self.to_s}"
    50:     end

dump(msg = "", out=STDERR)

Dump this Stats object with an optional additional message.

[Source]

        # File lib/mongrel/stats.rb, line 48
    48:     def dump(msg = "", out=STDERR)
    49:       out.puts "#{msg}: #{self.to_s}"
    50:     end

mean()

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

[Source]

        # File lib/mongrel/stats.rb, line 59
    59:     def mean
    60:       @sum / @n
    61:     end

mean()

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

[Source]

        # File lib/mongrel/stats.rb, line 59
    59:     def mean
    60:       @sum / @n
    61:     end

reset()

Resets the internal counters so you can start sampling again.

[Source]

        # 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

reset()

Resets the internal counters so you can start sampling again.

[Source]

        # 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)

Adds a sampling to the calculations.

[Source]

        # 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

sample(s)

Adds a sampling to the calculations.

[Source]

        # 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()

Calculates the standard deviation of the data so far.

[Source]

        # 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

sd()

Calculates the standard deviation of the data so far.

[Source]

        # 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()

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")

[Source]

        # 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

tick()

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")

[Source]

        # 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()

Returns a common display (used by dump)

[Source]

        # 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

to_s()

Returns a common display (used by dump)

[Source]

        # 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

[Validate]