Class: Benelux::Stats::Calculator
Overview
Based on Mongrel::Stats, Copyright © 2005 Zed A. Shaw
Instance Attribute Summary
#tags
Instance Method Summary
collapse
#add_tags, #add_tags_quick, #init_tags!, #remove_tags, #tag_values
Constructor Details
Returns a new instance of Calculator.
136
137
138
|
# File 'lib/benelux/stats.rb', line 136
def initialize
reset
end
|
Instance Method Details
140
141
142
143
144
145
|
# File 'lib/benelux/stats.rb', line 140
def +(other)
c = Calculator.new
c.merge! self
c.merge! other
c
end
|
#==(other) ⇒ Object
230
231
232
233
234
235
|
# File 'lib/benelux/stats.rb', line 230
def ==(other)
return false unless self.class == other.class
a=([@sum, @min, @max, @n, @sumsq] -
[other.sum, other.min, other.max, other.n, other.sumsq])
a.empty?
end
|
215
|
# File 'lib/benelux/stats.rb', line 215
def average; avg() end
|
Calculates and returns the mean for the data passed so far.
217
|
# File 'lib/benelux/stats.rb', line 217
def avg; return 0.0 unless @n > 0; @sum / @n; end
|
#dump(msg = "", out = STDERR) ⇒ Object
Dump this Stats object with an optional additional message.
192
193
194
|
# File 'lib/benelux/stats.rb', line 192
def dump(msg = "", out=STDERR)
out.puts "#{msg}: #{self.report}"
end
|
#first_tick ⇒ Object
184
|
# File 'lib/benelux/stats.rb', line 184
def first_tick() @last_time = Time.now end
|
203
204
205
206
|
# File 'lib/benelux/stats.rb', line 203
def inspect
v = [ mean, @n, @sum, @sumsq, sd, @min, @max, tags]
"%.4f: n=%.4f sum=%.4f sumsq=%.4f sd=%.4f min=%.4f max=%.4f %s" % v
end
|
NOTE: This is an alias for average. We don’t store values so we can’t return the actual mean
214
|
# File 'lib/benelux/stats.rb', line 214
def mean; avg() end
|
#merge!(other) ⇒ Object
157
158
159
160
161
162
163
164
165
166
167
168
169
|
# File 'lib/benelux/stats.rb', line 157
def merge!(other)
return self if other.n == 0
if @n == 0
@min, @max = other.min, other.max
else
@min = other.min if other.min < @min
@max = other.max if other.max > @max
end
@sum += other.sum
@sumsq += other.sumsq
@n += other.n
self
end
|
Returns a common display (used by dump)
197
198
199
200
201
|
# File 'lib/benelux/stats.rb', line 197
def report
v = [mean, @n, @sum, @sumsq, sd, @min, @max]
t = %q'%8d(N) %10.4f(SUM) %8.4f(SUMSQ) %8.4f(SD) %8.4f(MIN) %8.4f(MAX)'
('%0.4f: ' << t) % v
end
|
Resets the internal counters so you can start sampling again.
148
149
150
151
|
# File 'lib/benelux/stats.rb', line 148
def reset
@n, @sum, @sumsq = 0.0, 0.0, 0.0
@min, @max = 0.0, 0.0
end
|
#sample(s) ⇒ Object
Adds a sampling to the calculations.
172
173
174
175
176
177
178
179
180
181
182
|
# File 'lib/benelux/stats.rb', line 172
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
|
#samples(*args) ⇒ Object
153
154
155
|
# File 'lib/benelux/stats.rb', line 153
def samples(*args)
args.flatten.each { |s| sample(s) }
end
|
Calculates the standard deviation of the data so far.
220
221
222
223
224
225
226
227
228
|
# File 'lib/benelux/stats.rb', line 220
def sd
return 0.0 if @n <= 1
begin
return Math.sqrt( (@sumsq - (@sum * @sum / @n)) / (@n-1) )
rescue Errno::EDOM
return 0.0
end
end
|
185
186
187
188
189
|
# File 'lib/benelux/stats.rb', line 185
def tick
tick_time = Time.now
sample(tick_time - @last_time)
@last_time = tick_time
end
|
209
|
# File 'lib/benelux/stats.rb', line 209
def to_f; mean.to_f; end
|
210
|
# File 'lib/benelux/stats.rb', line 210
def to_i; mean.to_i; end
|
208
|
# File 'lib/benelux/stats.rb', line 208
def to_s; mean.to_s; end
|