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.
137
138
139
|
# File 'lib/benelux/stats.rb', line 137
def initialize
reset
end
|
Instance Method Details
141
142
143
144
145
146
|
# File 'lib/benelux/stats.rb', line 141
def +(other)
c = Calculator.new
c.merge! self
c.merge! other
c
end
|
#==(other) ⇒ Object
231
232
233
234
235
236
|
# File 'lib/benelux/stats.rb', line 231
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
|
216
|
# File 'lib/benelux/stats.rb', line 216
def average; avg() end
|
Calculates and returns the mean for the data passed so far.
218
|
# File 'lib/benelux/stats.rb', line 218
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.
193
194
195
|
# File 'lib/benelux/stats.rb', line 193
def dump(msg = "", out=STDERR)
out.puts "#{msg}: #{self.report}"
end
|
#first_tick ⇒ Object
185
|
# File 'lib/benelux/stats.rb', line 185
def first_tick() @last_time = Time.now end
|
204
205
206
207
|
# File 'lib/benelux/stats.rb', line 204
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
215
|
# File 'lib/benelux/stats.rb', line 215
def mean; avg() end
|
#merge!(other) ⇒ Object
158
159
160
161
162
163
164
165
166
167
168
169
170
|
# File 'lib/benelux/stats.rb', line 158
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)
198
199
200
201
202
|
# File 'lib/benelux/stats.rb', line 198
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.
149
150
151
152
|
# File 'lib/benelux/stats.rb', line 149
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.
173
174
175
176
177
178
179
180
181
182
183
|
# File 'lib/benelux/stats.rb', line 173
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
154
155
156
|
# File 'lib/benelux/stats.rb', line 154
def samples(*args)
args.flatten.each { |s| sample(s) }
end
|
Calculates the standard deviation of the data so far.
221
222
223
224
225
226
227
228
229
|
# File 'lib/benelux/stats.rb', line 221
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
|
186
187
188
189
190
|
# File 'lib/benelux/stats.rb', line 186
def tick
tick_time = Time.now
sample(tick_time - @last_time)
@last_time = tick_time
end
|
210
|
# File 'lib/benelux/stats.rb', line 210
def to_f; mean.to_f; end
|
211
|
# File 'lib/benelux/stats.rb', line 211
def to_i; mean.to_i; end
|
209
|
# File 'lib/benelux/stats.rb', line 209
def to_s; mean.to_s; end
|