Commit 22506ddc authored by Yorick Peterse's avatar Yorick Peterse

Added benchmark_subject method for benchmarks

This class method can be used in "describe" blocks to specify the
subject of a benchmark. This lets you write:

    benchmark_subject { Foo }

instead of:

    benchmark_subject { -> { Foo } }
parent 19893a1c
...@@ -14,25 +14,25 @@ describe User, benchmark: true do ...@@ -14,25 +14,25 @@ describe User, benchmark: true do
let(:iterations) { 1000 } let(:iterations) { 1000 }
describe 'using a capitalized username' do describe 'using a capitalized username' do
subject { -> { User.by_login('Alice') } } benchmark_subject { User.by_login('Alice') }
it { is_expected.to iterate_per_second(iterations) } it { is_expected.to iterate_per_second(iterations) }
end end
describe 'using a lowercase username' do describe 'using a lowercase username' do
subject { -> { User.by_login('alice') } } benchmark_subject { User.by_login('alice') }
it { is_expected.to iterate_per_second(iterations) } it { is_expected.to iterate_per_second(iterations) }
end end
describe 'using a capitalized Email address' do describe 'using a capitalized Email address' do
subject { -> { User.by_login('Alice@gitlab.com') } } benchmark_subject { User.by_login('Alice@gitlab.com') }
it { is_expected.to iterate_per_second(iterations) } it { is_expected.to iterate_per_second(iterations) }
end end
describe 'using a lowercase Email address' do describe 'using a lowercase Email address' do
subject { -> { User.by_login('alice@gitlab.com') } } benchmark_subject { User.by_login('alice@gitlab.com') }
it { is_expected.to iterate_per_second(iterations) } it { is_expected.to iterate_per_second(iterations) }
end end
......
module BenchmarkMatchers module BenchmarkMatchers
extend RSpec::Matchers::DSL extend RSpec::Matchers::DSL
def self.included(into)
into.extend(ClassMethods)
end
matcher :iterate_per_second do |min_iterations| matcher :iterate_per_second do |min_iterations|
supports_block_expectations supports_block_expectations
...@@ -39,4 +43,17 @@ module BenchmarkMatchers ...@@ -39,4 +43,17 @@ module BenchmarkMatchers
report.entries[0] report.entries[0]
end end
module ClassMethods
# Wraps around rspec's subject method so you can write:
#
# benchmark_subject { SomeClass.some_method }
#
# instead of:
#
# subject { -> { SomeClass.some_method } }
def benchmark_subject(&block)
subject { block }
end
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment