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.
250 251 252 |
# File 'lib/steep.rb', line 250 def initialize() @samples = [] end |
Instance Method Details
#average ⇒ Object
276 277 278 279 280 281 282 |
# File 'lib/steep.rb', line 276 def average if count > 0 total/count else 0.to_f end end |
#count ⇒ Object
264 265 266 |
# File 'lib/steep.rb', line 264 def count @samples.count end |
#percentile(p) ⇒ Object
284 285 286 287 |
# File 'lib/steep.rb', line 284 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
254 255 256 257 258 259 260 261 262 |
# File 'lib/steep.rb', line 254 def sample() start = Time.now begin yield ensure time = Time.now - start @samples << [, time] end end |
#slowests(num) ⇒ Object
272 273 274 |
# File 'lib/steep.rb', line 272 def slowests(num) @samples.sort_by(&:last).reverse.take(num) end |
#total ⇒ Object
268 269 270 |
# File 'lib/steep.rb', line 268 def total @samples.sum(&:last) end |