Class: Steep::Sampler
Instance Method Summary collapse
- #average ⇒ Object
- #count ⇒ Object
-
#initialize ⇒ Sampler
constructor
A new instance of Sampler.
- #percentile(p) ⇒ Object
- #sample(message) ⇒ Object
- #slowests(num) ⇒ Object
- #total ⇒ Object
Constructor Details
#initialize ⇒ Sampler
Returns a new instance of Sampler.
230 231 232 |
# File 'lib/steep.rb', line 230 def initialize() @samples = [] end |
Instance Method Details
#average ⇒ Object
256 257 258 259 260 261 262 |
# File 'lib/steep.rb', line 256 def average if count > 0 total/count else 0.to_f end end |
#count ⇒ Object
244 245 246 |
# File 'lib/steep.rb', line 244 def count @samples.count end |
#percentile(p) ⇒ Object
264 265 266 267 |
# File 'lib/steep.rb', line 264 def percentile(p) c = [count * p / 100.to_r, 1].max or raise slowests(c.to_i).last&.last || 0.to_f end |
#sample(message) ⇒ Object
234 235 236 237 238 239 240 241 242 |
# File 'lib/steep.rb', line 234 def sample() start = Time.now begin yield ensure time = Time.now - start @samples << [, time] end end |
#slowests(num) ⇒ Object
252 253 254 |
# File 'lib/steep.rb', line 252 def slowests(num) @samples.sort_by(&:last).reverse.take(num) end |
#total ⇒ Object
248 249 250 |
# File 'lib/steep.rb', line 248 def total @samples.sum(&:last) end |