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 } }
This commit is contained in:
parent
19893a1c10
commit
22506ddc50
2 changed files with 21 additions and 4 deletions
|
@ -14,25 +14,25 @@ describe User, benchmark: true do
|
|||
let(:iterations) { 1000 }
|
||||
|
||||
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) }
|
||||
end
|
||||
|
||||
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) }
|
||||
end
|
||||
|
||||
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) }
|
||||
end
|
||||
|
||||
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) }
|
||||
end
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
module BenchmarkMatchers
|
||||
extend RSpec::Matchers::DSL
|
||||
|
||||
def self.included(into)
|
||||
into.extend(ClassMethods)
|
||||
end
|
||||
|
||||
matcher :iterate_per_second do |min_iterations|
|
||||
supports_block_expectations
|
||||
|
||||
|
@ -39,4 +43,17 @@ module BenchmarkMatchers
|
|||
|
||||
report.entries[0]
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue