Performance tests using Rspec

  1. Know of any guides to writing performance tests using Rspec?

  2. Does Rspec have the capability to run specs in benchmarking or
    profiling mode similar to the way Rails has performance tests that run
    in these modes (http://guides.rubyonrails.org/
    performance_testing.html)?

On Jan 16, 2011, at 8:29 PM, Evan wrote:

  1. Know of any guides to writing performance tests using Rspec?

There’s nothing built in as of yet, but you can do this pretty easily:

require 'benchmark'

RSpec::Matchers.define :take_less_than do |n|
chain :seconds do; end

match do |block|
@elapsed = Benchmark.realtime do
block.call
end
@elapsed <= n
end

end

describe “addition” do
it “runs fast” do
expect do
10000.times { 1 + 2 }
end.to take_less_than(0.001).seconds
end
end

  1. Does Rspec have the capability to run specs in benchmarking or
    profiling mode similar to the way Rails has performance tests that run
    in these modes (http://guides.rubyonrails.org/
    performance_testing.html)?

There is a --profile command line switch:

rspec spec --profile

which gives you the 10 slowest running examples, but that includes time
to run all the before, after, and around hooks as well. My guess is
you’d be better off with the matcher above.

HTH,
David

A while ago, I was looking at Minitest’s benchmarking capabilities
(contained in a single file
herehttps://github.com/seattlerb/minitest/blob/master/lib/minitest/benchmark.rb)
and I thought it would be great to have an equivalent in RSpec. They
have
methods to assert type of performance (linear, exponential…) of a
given
block. Looking at minitest’s file it might be relatively trivial to
implement, but the question is: do you think that would naturally fit in
rspec-matchers, or in a separate rspec-benchmark gem/plugin?
Personally I think it’s a good piece of functionality and deserves being
baked in RSpec, but I’m not sure if many people would actually use it.
Thoughts?

On Jan 26, 2011, at 4:06 AM, Josep M. Bach wrote:

A while ago, I was looking at Minitest’s benchmarking capabilities (contained in
a single file here) and I thought it would be great to have an equivalent in
RSpec. They have methods to assert type of performance (linear, exponential…) of
a given block. Looking at minitest’s file it might be relatively trivial to
implement, but the question is: do you think that would naturally fit in
rspec-matchers, or in a separate rspec-benchmark gem/plugin?
Personally I think it’s a good piece of functionality and deserves being baked
in RSpec, but I’m not sure if many people would actually use it. Thoughts?

I’d like to see it in an extension gem first. We can always merge it in
if it becomes a standard component. If not, it’s just more stuff to
maintain that nobody uses :slight_smile: