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
# 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

#concurrencyObject (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

#samplesObject (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

#averageObject



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

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

#countObject



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

def count
	@samples.size
end

#durationObject



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

def duration
	@samples.sum
end

#latencyObject



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

def latency
	duration.to_f / count.to_f
end

#measureObject



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


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_durationObject



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

def sequential_duration
	duration / @concurrency
end

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

Returns:

  • (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_deviationObject

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_errorObject



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

Returns:

  • (Boolean)


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

def valid?
	@samples.size > 1
end

#varianceObject

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