Class: Benchmark::HTTP::Stopwatch

Inherits:
Object
  • Object
show all
Defined in:
lib/benchmark/http/statistics.rb

Direct Known Subclasses

Statistics

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(concurrency = 0) ⇒ Stopwatch

Returns a new instance of Stopwatch.



11
12
13
14
15
16
17
18
19
# File 'lib/benchmark/http/statistics.rb', line 11

def initialize(concurrency = 0)
  @samples = []
  
  # The number of currently executing measurements:
  @count = 0
  
  @concurrency = concurrency
  @start_time = nil
end

Instance Attribute Details

#concurrencyObject (readonly)

The maximum number of executing measurements at any one time.



25
26
27
# File 'lib/benchmark/http/statistics.rb', line 25

def concurrency
  @concurrency
end

#samplesObject (readonly)

The individual samples’ durations.



22
23
24
# File 'lib/benchmark/http/statistics.rb', line 22

def samples
  @samples
end

Instance Method Details

#add(duration, result = nil) ⇒ Object



85
86
87
# File 'lib/benchmark/http/statistics.rb', line 85

def add(duration, result = nil)
  @samples << duration
end

#averageObject



53
54
55
56
57
# File 'lib/benchmark/http/statistics.rb', line 53

def average
  if @samples.any?
    @samples.sum / @samples.size
  end
end

#countObject



35
36
37
# File 'lib/benchmark/http/statistics.rb', line 35

def count
  @samples.size
end

#durationObject



27
28
29
# File 'lib/benchmark/http/statistics.rb', line 27

def duration
  @samples.sum
end

#latencyObject



43
44
45
# File 'lib/benchmark/http/statistics.rb', line 43

def latency
  duration.to_f / count.to_f
end

#measureObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/benchmark/http/statistics.rb', line 89

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



39
40
41
# File 'lib/benchmark/http/statistics.rb', line 39

def per_second(duration = self.sequential_duration)
  @samples.size.to_f / duration.to_f
end

#sample(confidence_factor, &block) ⇒ Object



113
114
115
116
117
118
119
# File 'lib/benchmark/http/statistics.rb', line 113

def sample(confidence_factor, &block)
  yield
  
  begin
    measure(&block)
  end until confident?(confidence_factor)
end

#sequential_durationObject



31
32
33
# File 'lib/benchmark/http/statistics.rb', line 31

def sequential_duration
  duration / @concurrency
end

#similar?(other, difference = 2.0) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
51
# File 'lib/benchmark/http/statistics.rb', line 47

def similar?(other, difference = 2.0)
  ratio = other.latency / self.latency
  
  return ratio < difference
end

#standard_deviationObject

Population Standard Deviation, σ



73
74
75
76
77
# File 'lib/benchmark/http/statistics.rb', line 73

def standard_deviation
  if variance = self.variance
    Math.sqrt(variance.abs)
  end
end

#standard_errorObject



79
80
81
82
83
# File 'lib/benchmark/http/statistics.rb', line 79

def standard_error
  if standard_deviation = self.standard_deviation
    standard_deviation / Math.sqrt(@samples.size)
  end
end

#to_json(options) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/benchmark/http/statistics.rb', line 129

def to_json(options)
  {
    count: self.count,
    concurrency: self.concurrency,
    latency: self.latency,
    standard_deviation: self.standard_deviation,
    standard_error: self.standard_error,
    per_second: self.per_second,
    duration: self.duration,
    variance: self.variance,
  }.to_json(options)
end

#to_sObject



121
122
123
124
125
126
127
# File 'lib/benchmark/http/statistics.rb', line 121

def to_s
  if self.valid?
    "#{@samples.size} samples. #{per_second} requests per second. S/D: #{Seconds[standard_deviation]}."
  else
    "Not enough samples."
  end
end

#valid?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/benchmark/http/statistics.rb', line 59

def valid?
  @samples.size > 0
end

#varianceObject

Computes Population Variance, σ^2.



64
65
66
67
68
69
70
# File 'lib/benchmark/http/statistics.rb', line 64

def variance
  if valid?
    average = self.average
    
    return @samples.map{|n| (n - average)**2}.sum / @samples.size
  end
end