class Benchmark::HTTP::Stopwatch
Attributes
concurrency[R]
The maximum number of executing measurements at any one time.
samples[R]
The individual samples' durations.
Public Class Methods
new(concurrency = 0)
click to toggle source
# File lib/benchmark/http/statistics.rb, line 26 def initialize(concurrency = 0) @samples = [] # The number of currently executing measurements: @count = 0 @concurrency = concurrency @start_time = nil end
Public Instance Methods
add(duration, result = nil)
click to toggle source
# File lib/benchmark/http/statistics.rb, line 100 def add(duration, result = nil) @samples << duration end
average()
click to toggle source
# File lib/benchmark/http/statistics.rb, line 68 def average if @samples.any? @samples.sum / @samples.size end end
count()
click to toggle source
# File lib/benchmark/http/statistics.rb, line 50 def count @samples.size end
duration()
click to toggle source
# File lib/benchmark/http/statistics.rb, line 42 def duration @samples.sum end
latency()
click to toggle source
# File lib/benchmark/http/statistics.rb, line 58 def latency duration.to_f / count.to_f end
measure() { || ... }
click to toggle source
# File lib/benchmark/http/statistics.rb, line 104 def measure @count += 1 if @count > @concurrency @concurrency = @count end start_time = Async::Clock.now unless @start_time @start_time = start_time end result = yield end_time = Async::Clock.now self.add(end_time - start_time, result) return result ensure @count -= 1 end
per_second(duration = self.sequential_duration)
click to toggle source
# File lib/benchmark/http/statistics.rb, line 54 def per_second(duration = self.sequential_duration) @samples.size.to_f / duration.to_f end
print(out = STDOUT)
click to toggle source
# File lib/benchmark/http/statistics.rb, line 136 def print(out = STDOUT) if self.valid? out.puts "#{@samples.size} samples. #{per_second} requests per second. S/D: #{Seconds[standard_deviation]}." else out.puts "Not enough samples." end end
sample(confidence_factor) { || ... }
click to toggle source
# File lib/benchmark/http/statistics.rb, line 128 def sample(confidence_factor, &block) yield begin measure(&block) end until confident?(confidence_factor) end
sequential_duration()
click to toggle source
# File lib/benchmark/http/statistics.rb, line 46 def sequential_duration duration / @concurrency end
similar?(other, difference = 2.0)
click to toggle source
# File lib/benchmark/http/statistics.rb, line 62 def similar?(other, difference = 2.0) ratio = other.latency / self.latency return ratio < difference end
standard_deviation()
click to toggle source
Population Standard Deviation, σ
# File lib/benchmark/http/statistics.rb, line 88 def standard_deviation if variance = self.variance Math.sqrt(variance.abs) end end
standard_error()
click to toggle source
# File lib/benchmark/http/statistics.rb, line 94 def standard_error if standard_deviation = self.standard_deviation standard_deviation / Math.sqrt(@samples.size) end end
valid?()
click to toggle source
# File lib/benchmark/http/statistics.rb, line 74 def valid? @samples.size > 0 end
variance()
click to toggle source
Computes Population Variance, σ^2.
# File lib/benchmark/http/statistics.rb, line 79 def variance if valid? average = self.average return @samples.map{|n| (n - average)**2}.sum / @samples.size end end
Private Instance Methods
confident?(factor)
click to toggle source
# File lib/benchmark/http/statistics.rb, line 146 def confident?(factor) if @samples.size > @concurrency * 10 return self.standard_error < (self.average * factor) end return false end