Class: Benchmark::HTTP::Stopwatch
- Inherits:
-
Object
- Object
- Benchmark::HTTP::Stopwatch
- Defined in:
- lib/benchmark/http/statistics.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#concurrency ⇒ Object
readonly
The maximum number of executing measurements at any one time.
-
#samples ⇒ Object
readonly
The individual samples’ durations.
Instance Method Summary collapse
- #add(duration, result = nil) ⇒ Object
- #average ⇒ Object
- #count ⇒ Object
- #duration ⇒ Object
-
#initialize(concurrency = 0) ⇒ Stopwatch
constructor
A new instance of Stopwatch.
- #latency ⇒ Object
- #measure ⇒ Object
- #per_second(duration = self.sequential_duration) ⇒ Object
- #print(out = STDOUT) ⇒ Object
- #sample(confidence_factor, &block) ⇒ Object
- #sequential_duration ⇒ Object
- #similar?(other, difference = 2.0) ⇒ Boolean
-
#standard_deviation ⇒ Object
Population Standard Deviation, σ.
- #standard_error ⇒ Object
- #valid? ⇒ Boolean
-
#variance ⇒ Object
Computes Population Variance, σ^2.
Constructor Details
#initialize(concurrency = 0) ⇒ Stopwatch
Returns a new instance of Stopwatch.
26 27 28 29 30 31 32 33 34 |
# 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 |
Instance Attribute Details
#concurrency ⇒ Object (readonly)
The maximum number of executing measurements at any one time.
40 41 42 |
# File 'lib/benchmark/http/statistics.rb', line 40 def concurrency @concurrency end |
#samples ⇒ Object (readonly)
The individual samples’ durations.
37 38 39 |
# File 'lib/benchmark/http/statistics.rb', line 37 def samples @samples end |
Instance Method Details
#add(duration, result = nil) ⇒ Object
100 101 102 |
# File 'lib/benchmark/http/statistics.rb', line 100 def add(duration, result = nil) @samples << duration end |
#average ⇒ Object
68 69 70 71 72 |
# File 'lib/benchmark/http/statistics.rb', line 68 def average if @samples.any? @samples.sum / @samples.size end end |
#count ⇒ Object
50 51 52 |
# File 'lib/benchmark/http/statistics.rb', line 50 def count @samples.size end |
#duration ⇒ Object
42 43 44 |
# File 'lib/benchmark/http/statistics.rb', line 42 def duration @samples.sum end |
#latency ⇒ Object
58 59 60 |
# File 'lib/benchmark/http/statistics.rb', line 58 def latency duration.to_f / count.to_f end |
#measure ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# 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) ⇒ Object
54 55 56 |
# 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) ⇒ Object
136 137 138 139 140 141 142 |
# 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, &block) ⇒ Object
128 129 130 131 132 133 134 |
# 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 ⇒ Object
46 47 48 |
# File 'lib/benchmark/http/statistics.rb', line 46 def sequential_duration duration / @concurrency end |
#similar?(other, difference = 2.0) ⇒ Boolean
62 63 64 65 66 |
# 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 ⇒ Object
Population Standard Deviation, σ
88 89 90 91 92 |
# File 'lib/benchmark/http/statistics.rb', line 88 def standard_deviation if variance = self.variance Math.sqrt(variance.abs) end end |
#standard_error ⇒ Object
94 95 96 97 98 |
# 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? ⇒ Boolean
74 75 76 |
# File 'lib/benchmark/http/statistics.rb', line 74 def valid? @samples.size > 1 end |
#variance ⇒ Object
Computes Population Variance, σ^2.
79 80 81 82 83 84 85 |
# 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 |