Module: RSpec::Benchmark::Matchers

Defined in:
lib/rspec/benchmark/matchers.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#bench_range(*args) ⇒ Object

Generate a geometric progression of inputs

The default range is generated in the multiples of 8.

Examples:

bench_range(8, 8 << 10)
# => [8, 64, 512, 4096, 8192]

Parameters:

  • start (Integer)
  • limit (Integer)
  • ratio (Integer)


170
171
172
# File 'lib/rspec/benchmark/matchers.rb', line 170

def bench_range(*args)
  ::Benchmark::Trend.range(*args)
end

#perform_allocation(objects, **options) ⇒ Object

Passes if code block performs at least iterations

Examples:

expect { ... }.to perform_allocation(10000)
expect { ... }.to perform_allocation(10000)

Parameters:

  • iterations (Integer)


37
38
39
# File 'lib/rspec/benchmark/matchers.rb', line 37

def perform_allocation(objects, **options)
  AllocationMatcher::Matcher.new(objects, **options)
end

#perform_at_least(iterations, **options) ⇒ Object

Passes if code block performs at least iterations

Examples:

expect { ... }.to perform_at_least(10000)
expect { ... }.to perform_at_least(10000).ips

Parameters:

  • iterations (Integer)


50
51
52
# File 'lib/rspec/benchmark/matchers.rb', line 50

def perform_at_least(iterations, **options)
  IterationMatcher::Matcher.new(iterations, **options)
end

#perform_constant(**options) ⇒ Object

Pass if code block performs constant

Examples:

expect { ... }.to perform_constant
expect { ... }.to perform_constant.within(1, 100_000)
expect { ... }.to perform_constant.within(1, 100_000, ratio: 4)


102
103
104
# File 'lib/rspec/benchmark/matchers.rb', line 102

def perform_constant(**options)
  ComplexityMatcher::Matcher.new(:constant, **options)
end

#perform_exponential(**options) ⇒ Object Also known as: perform_exp

Pass if code block performs exponential

Examples:

expect { ... }.to perform_exponential
expect { ... }.to perform_exponential.within(1, 100_000)
expect { ... }.to perform_exponential.within(1, 100_000, ratio: 4)


152
153
154
# File 'lib/rspec/benchmark/matchers.rb', line 152

def perform_exponential(**options)
  ComplexityMatcher::Matcher.new(:exponential, **options)
end

#perform_faster_than(**options, &sample) ⇒ Object

Passes if code block performs faster than sample block

Examples:

expect { ... }.to peform_faster_than { ... }
expect { ... }.to peform_faster_than { ... }.at_least(5).times

Parameters:

  • sample (Proc)


77
78
79
# File 'lib/rspec/benchmark/matchers.rb', line 77

def perform_faster_than(**options, &sample)
  ComparisonMatcher::Matcher.new(sample, :faster, **options)
end

#perform_linear(**options) ⇒ Object

Pass if code block performs linear

Examples:

expect { ... }.to perform_linear
expect { ... }.to perform_linear.within(1, 100_000)
expect { ... }.to perform_linear.within(1, 100_000, ratio: 4)


128
129
130
# File 'lib/rspec/benchmark/matchers.rb', line 128

def perform_linear(**options)
  ComplexityMatcher::Matcher.new(:linear, **options)
end

#perform_logarithmic(**options) ⇒ Object Also known as: perform_log

Pass if code block performs logarithmic

Examples:

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)


115
116
117
# File 'lib/rspec/benchmark/matchers.rb', line 115

def perform_logarithmic(**options)
  ComplexityMatcher::Matcher.new(:logarithmic, **options)
end

#perform_power(**options) ⇒ Object

Pass if code block performs power

Examples:

expect { ... }.to perform_power
expect { ... }.to perform_power.within(1, 100_000)
expect { ... }.to perform_power.within(1, 100_000, ratio: 4)


140
141
142
# File 'lib/rspec/benchmark/matchers.rb', line 140

def perform_power(**options)
  ComplexityMatcher::Matcher.new(:power, **options)
end

#perform_slower_than(**options, &sample) ⇒ Object

Passes if code block performs slower than sample block

Examples:

expect { ... }.to peform_slower_than { ... }
expect { ... }.to peform_slower_than { ... }.at_most(5).times

Parameters:

  • sample (Proc)


90
91
92
# 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) ⇒ Object

Passes if code block performs under threshold

Examples:

expect { ... }.to peform_under(0.001)
expect { ... }.to peform_under(0.001).sec
expect { ... }.to peform_under(10).ms

Parameters:

  • threshold (Float)


64
65
66
# File 'lib/rspec/benchmark/matchers.rb', line 64

def perform_under(threshold, **options)
  TimingMatcher::Matcher.new(threshold, **options)
end