Based on Mongrel::Stats, Copyright © 2005 Zed A. Shaw
Methods
public class
public instance
Included modules
Public class methods
Public instance methods
# File lib/benelux/stats.rb, line 141 141: def +(other) 142: c = Calculator.new 143: c.merge! self 144: c.merge! other 145: c 146: end
# File lib/benelux/stats.rb, line 231 231: def ==(other) 232: return false unless self.class == other.class 233: a=([@sum, @min, @max, @n, @sumsq] - 234: [other.sum, other.min, other.max, other.n, other.sumsq]) 235: a.empty? 236: end
Calculates and returns the mean for the data passed so far.
# File lib/benelux/stats.rb, line 218 218: def avg; return 0.0 unless @n > 0; @sum / @n; end
Dump this Stats object with an optional additional message.
# File lib/benelux/stats.rb, line 193 193: def dump(msg = "", out=STDERR) 194: out.puts "#{msg}: #{self.report}" 195: end
# File lib/benelux/stats.rb, line 185 185: def first_tick() @last_time = Time.now end
# File lib/benelux/stats.rb, line 204 204: def inspect 205: v = [ mean, @n, @sum, @sumsq, sd, @min, @max, tags] 206: "%.4f: n=%.4f sum=%.4f sumsq=%.4f sd=%.4f min=%.4f max=%.4f %s" % v 207: end
NOTE: This is an alias for average. We don’t store values so we can’t return the actual mean
# File lib/benelux/stats.rb, line 215 215: def mean; avg() end
# File lib/benelux/stats.rb, line 158 158: def merge!(other) 159: return self if other.n == 0 160: if @n == 0 161: @min, @max = other.min, other.max 162: else 163: @min = other.min if other.min < @min 164: @max = other.max if other.max > @max 165: end 166: @sum += other.sum 167: @sumsq += other.sumsq 168: @n += other.n 169: self 170: end
Returns a common display (used by dump)
# File lib/benelux/stats.rb, line 198 198: def report 199: v = [mean, @n, @sum, @sumsq, sd, @min, @max] 200: t = %q'%8d(N) %10.4f(SUM) %8.4f(SUMSQ) %8.4f(SD) %8.4f(MIN) %8.4f(MAX)' 201: ('%0.4f: ' << t) % v 202: end
Resets the internal counters so you can start sampling again.
# File lib/benelux/stats.rb, line 149 149: def reset 150: @n, @sum, @sumsq = 0.0, 0.0, 0.0 151: @min, @max = 0.0, 0.0 152: end
Adds a sampling to the calculations.
# File lib/benelux/stats.rb, line 173 173: def sample(s) 174: @sum += s 175: @sumsq += s * s 176: if @n == 0 177: @min = @max = s 178: else 179: @min = s if @min > s 180: @max = s if @max < s 181: end 182: @n+=1 183: end
# File lib/benelux/stats.rb, line 154 154: def samples(*args) 155: args.flatten.each { |s| sample(s) } 156: end
Calculates the standard deviation of the data so far.
# File lib/benelux/stats.rb, line 221 221: def sd 222: return 0.0 if @n <= 1 223: # (sqrt( ((s).sumsq - ( (s).sum * (s).sum / (s).n)) / ((s).n-1) )) 224: begin 225: return Math.sqrt( (@sumsq - (@sum * @sum / @n)) / (@n-1) ) 226: rescue Errno::EDOM 227: return 0.0 228: end 229: end
# File lib/benelux/stats.rb, line 186 186: def tick 187: tick_time = Time.now 188: sample(tick_time - @last_time) 189: @last_time = tick_time 190: end