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.



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/benchmark/http/statistics.rb', line 26

def initialize(concurrency = 0)
	@samples = []
	
	@total_time = 0
	
	# 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.



45
46
47
# File 'lib/benchmark/http/statistics.rb', line 45

def concurrency
  @concurrency
end

#samplesObject (readonly)

The individual samples’ durations.



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

def samples
  @samples
end

#total_timeObject (readonly)

The sequential time of all samples.



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

def total_time
  @total_time
end

Instance Method Details

#add(duration, result = nil) ⇒ Object



105
106
107
# File 'lib/benchmark/http/statistics.rb', line 105

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

#averageObject



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

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

#countObject



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

def count
	@samples.count
end

#durationObject



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

def duration
	@samples.sum
end

#latencyObject



63
64
65
# File 'lib/benchmark/http/statistics.rb', line 63

def latency
	duration.to_f / count.to_f
end

#measureObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/benchmark/http/statistics.rb', line 109

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
	
	if @count == 0 and end_time
		@total_time += end_time - @start_time
		@start_time = nil
	end
end

#per_secondObject



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

def per_second
	@samples.count.to_f / total_time.to_f
end


147
148
149
150
151
152
153
# File 'lib/benchmark/http/statistics.rb', line 147

def print(out = STDOUT)
	if self.valid?
		out.puts "#{@samples.count} samples. #{per_second} requests per second. S/D: #{Seconds[standard_deviation]}."
	else
		out.puts "Not enough samples."
	end
end

#sample(confidence_factor, &block) ⇒ Object



138
139
140
141
142
143
144
145
# File 'lib/benchmark/http/statistics.rb', line 138

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

#sequential_durationObject



51
52
53
# File 'lib/benchmark/http/statistics.rb', line 51

def sequential_duration
	duration / @concurrency
end

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

Returns:

  • (Boolean)


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

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

#standard_deviationObject

Population Standard Deviation, σ



93
94
95
96
97
# File 'lib/benchmark/http/statistics.rb', line 93

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

#standard_errorObject



99
100
101
102
103
# File 'lib/benchmark/http/statistics.rb', line 99

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

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
	@samples.count > 1
end

#varianceObject

Computes Population Variance, σ^2.



84
85
86
87
88
89
90
# File 'lib/benchmark/http/statistics.rb', line 84

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