/
matchers.rb
executable file
·175 lines (163 loc) · 5.17 KB
/
matchers.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# frozen_string_literal
require_relative 'allocation_matcher'
require_relative 'comparison_matcher'
require_relative 'complexity_matcher'
require_relative 'iteration_matcher'
require_relative 'timing_matcher'
module RSpec
module Benchmark
# Provides a number of useful performance testing expectations
#
# These matchers can be exposed by including the this module in
# a spec:
#
# RSpec.describe "Performance testing" do
# include RSpec::Benchmark::Matchers
# end
#
# or you can include in globablly in a spec_helper.rb file:
#
# RSpec.configure do |config|
# config.inlucde(RSpec::Benchmark::Matchers)
# end
#
# @api public
module Matchers
# Passes if code block performs at least iterations
#
# @param [Integer] iterations
#
# @example
# expect { ... }.to perform_allocation(10000)
# expect { ... }.to perform_allocation(10000)
#
# @api public
def perform_allocation(objects, **options)
AllocationMatcher::Matcher.new(objects, **options)
end
# Passes if code block performs at least iterations
#
# @param [Integer] iterations
#
# @example
# expect { ... }.to perform_at_least(10000)
# expect { ... }.to perform_at_least(10000).ips
#
# @api public
def perform_at_least(iterations, **options)
IterationMatcher::Matcher.new(iterations, **options)
end
# Passes if code block performs under threshold
#
# @param [Float] threshold
#
# @example
# expect { ... }.to peform_under(0.001)
# expect { ... }.to peform_under(0.001).sec
# expect { ... }.to peform_under(10).ms
#
# @api public
def perform_under(threshold, **options)
TimingMatcher::Matcher.new(threshold, **options)
end
# Passes if code block performs faster than sample block
#
# @param [Proc] sample
#
# @example
# expect { ... }.to peform_faster_than { ... }
# expect { ... }.to peform_faster_than { ... }.at_least(5).times
#
# @api public
def perform_faster_than(**options, &sample)
ComparisonMatcher::Matcher.new(sample, :faster, **options)
end
# Passes if code block performs slower than sample block
#
# @param [Proc] sample
#
# @example
# expect { ... }.to peform_slower_than { ... }
# expect { ... }.to peform_slower_than { ... }.at_most(5).times
#
# @api public
def perform_slower_than(**options, &sample)
ComparisonMatcher::Matcher.new(sample, :slower, **options)
end
# Pass if code block performs constant
#
# @example
# expect { ... }.to perform_constant
# expect { ... }.to perform_constant.within(1, 100_000)
# expect { ... }.to perform_constant.within(1, 100_000, ratio: 4)
#
# @api public
def perform_constant(**options)
ComplexityMatcher::Matcher.new(:constant, **options)
end
# Pass if code block performs logarithmic
#
# @example
# expect { ... }.to perform_logarithmic
# expect { ... }.to perform_logarithmic
# expect { ... }.to perform_logarithmic.within(1, 100_000)
# expect { ... }.to perform_logarithimic.within(1, 100_000, ratio: 4)
#
# @api public
def perform_logarithmic(**options)
ComplexityMatcher::Matcher.new(:logarithmic, **options)
end
alias perform_log perform_logarithmic
# Pass if code block performs linear
#
# @example
# expect { ... }.to perform_linear
# expect { ... }.to perform_linear.within(1, 100_000)
# expect { ... }.to perform_linear.within(1, 100_000, ratio: 4)
#
# @api public
def perform_linear(**options)
ComplexityMatcher::Matcher.new(:linear, **options)
end
# Pass if code block performs power
#
# @example
# expect { ... }.to perform_power
# expect { ... }.to perform_power.within(1, 100_000)
# expect { ... }.to perform_power.within(1, 100_000, ratio: 4)
#
# @api public
def perform_power(**options)
ComplexityMatcher::Matcher.new(:power, **options)
end
# Pass if code block performs exponential
#
# @example
# expect { ... }.to perform_exponential
# expect { ... }.to perform_exponential.within(1, 100_000)
# expect { ... }.to perform_exponential.within(1, 100_000, ratio: 4)
#
# @api public
def perform_exponential(**options)
ComplexityMatcher::Matcher.new(:exponential, **options)
end
alias perform_exp perform_exponential
# Generate a geometric progression of inputs
#
# The default range is generated in the multiples of 8.
#
# @example
# bench_range(8, 8 << 10)
# # => [8, 64, 512, 4096, 8192]
#
# @param [Integer] start
# @param [Integer] limit
# @param [Integer] ratio
#
# @api public
def bench_range(*args)
::Benchmark::Trend.range(*args)
end
end # Matchers
end # Benchmark
end # RSpec