module RSpec::Benchmark::Matchers
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
Public Instance Methods
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
# File lib/rspec/benchmark/matchers.rb, line 170 def bench_range(*args) ::Benchmark::Trend.range(*args) end
Passes if code block performs at least iterations
@param [Integer] iterations
@example
expect { ... }.to perform_allocation(10000) expect { ... }.to perform_allocation(10000)
@api public
# File lib/rspec/benchmark/matchers.rb, line 37 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
# File lib/rspec/benchmark/matchers.rb, line 50 def perform_at_least(iterations, **options) IterationMatcher::Matcher.new(iterations, **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
# File lib/rspec/benchmark/matchers.rb, line 102 def perform_constant(**options) ComplexityMatcher::Matcher.new(:constant, **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
# File lib/rspec/benchmark/matchers.rb, line 152 def perform_exponential(**options) ComplexityMatcher::Matcher.new(:exponential, **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
# File lib/rspec/benchmark/matchers.rb, line 77 def perform_faster_than(**options, &sample) ComparisonMatcher::Matcher.new(sample, :faster, **options) end
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
# File lib/rspec/benchmark/matchers.rb, line 128 def perform_linear(**options) ComplexityMatcher::Matcher.new(:linear, **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
# File lib/rspec/benchmark/matchers.rb, line 115 def perform_logarithmic(**options) ComplexityMatcher::Matcher.new(:logarithmic, **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
# File lib/rspec/benchmark/matchers.rb, line 140 def perform_power(**options) ComplexityMatcher::Matcher.new(:power, **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
# File lib/rspec/benchmark/matchers.rb, line 90 def perform_slower_than(**options, &sample) ComparisonMatcher::Matcher.new(sample, :slower, **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
# File lib/rspec/benchmark/matchers.rb, line 64 def perform_under(threshold, **options) TimingMatcher::Matcher.new(threshold, **options) end