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

bench_range(*args) click to toggle source

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
perform_allocation(objects, **options) click to toggle source

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
perform_at_least(iterations, **options) click to toggle source

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
perform_constant(**options) click to toggle source

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
perform_exp(**options)
Alias for: perform_exponential
perform_exponential(**options) click to toggle source

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
Also aliased as: perform_exp
perform_faster_than(**options, &sample) click to toggle source

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
perform_linear(**options) click to toggle source

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
perform_log(**options)
Alias for: perform_logarithmic
perform_logarithmic(**options) click to toggle source

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
Also aliased as: perform_log
perform_power(**options) click to toggle source

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
perform_slower_than(**options, &sample) click to toggle source

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
perform_under(threshold, **options) click to toggle source

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