Class: Stats

Inherits:
Object
  • Object
show all
Defined in:
lib/mongrel/stats.rb

Overview

A very simple little class for doing some basic fast statistics sampling. You feed it either samples of numeric data you want measured or you call Stats.tick to get it to add a time delta between the last time you called it. When you’re done either call sum, sumsq, n, min, max, mean or sd to get the information. The other option is to just call dump and see everything.

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Stats

Returns a new instance of Stats.



18
19
20
21
# File 'lib/mongrel/stats.rb', line 18

def initialize(name)
  @name = name
  reset
end

Instance Attribute Details

#maxObject (readonly)

Returns the value of attribute max.



16
17
18
# File 'lib/mongrel/stats.rb', line 16

def max
  @max
end

#minObject (readonly)

Returns the value of attribute min.



16
17
18
# File 'lib/mongrel/stats.rb', line 16

def min
  @min
end

#nObject (readonly)

Returns the value of attribute n.



16
17
18
# File 'lib/mongrel/stats.rb', line 16

def n
  @n
end

#sumObject (readonly)

Returns the value of attribute sum.



16
17
18
# File 'lib/mongrel/stats.rb', line 16

def sum
  @sum
end

#sumsqObject (readonly)

Returns the value of attribute sumsq.



16
17
18
# File 'lib/mongrel/stats.rb', line 16

def sumsq
  @sumsq
end

Instance Method Details

#dump(msg = "", out = STDERR) ⇒ Object

Dump this Stats object with an optional additional message.



47
48
49
# File 'lib/mongrel/stats.rb', line 47

def dump(msg = "", out=STDERR)
  out.puts "#{msg}: #{self.to_s}"
end

#meanObject

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



58
59
60
# File 'lib/mongrel/stats.rb', line 58

def mean
  @sum / @n
end

#resetObject

Resets the internal counters so you can start sampling again.



24
25
26
27
28
29
30
31
# File 'lib/mongrel/stats.rb', line 24

def reset
  @sum = 0.0
  @sumsq = 0.0
  @last_time = Time.new
  @n = 0.0
  @min = 0.0
  @max = 0.0
end

#sample(s) ⇒ Object

Adds a sampling to the calculations.



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/mongrel/stats.rb', line 34

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

#sdObject

Calculates the standard deviation of the data so far.



63
64
65
66
67
68
69
70
# File 'lib/mongrel/stats.rb', line 63

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

#tickObject

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


82
83
84
85
86
# File 'lib/mongrel/stats.rb', line 82

def tick
  now = Time.now
  sample(now - @last_time)
  @last_time = now
end

#to_sObject

Returns a common display (used by dump)



52
53
54
# File 'lib/mongrel/stats.rb', line 52

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