Skip to content

Instantly share code, notes, and snippets.

@chadoh
Forked from tatey/benchmark.rb
Created May 15, 2012 19:50
Show Gist options
  • Save chadoh/2704578 to your computer and use it in GitHub Desktop.
Save chadoh/2704578 to your computer and use it in GitHub Desktop.
Mocha VS MiniTest::Mock and SimpleDelegate VS SimpleMock
require 'benchmark'
require 'delegate'
require 'mocha'
require 'simple_mock'
require 'rr'
extend RR::Adapters::RRMethods
n = 1000
array = [1]
Benchmark.bm(10) do |x|
x.report 'mocha' do
n.times do
array.expects(:push).with(2).returns([1, 2])
array.push(2)
end
end
x.report 'delegator' do
n.times do
mock = Class.new(SimpleDelegator) { def push(*args); [1, 2]; end }.new(array)
mock.push(2)
end
end
x.report 'simple_mock' do
n.times do
mock = SimpleMock.new(array).expect(:push, [1, 2], [2])
mock.push(2)
end
end
x.report 'rr' do
n.times do
mock = mock(array).push(2).returns([1, 2])
mock.push(2)
end
end
end
user system total real
mocha 1.660000 0.010000 1.670000 ( 1.721667)
delegator 0.010000 0.000000 0.010000 ( 0.011092)
simple_mock 0.020000 0.000000 0.020000 ( 0.016473)
rr
⮁ ruby benchmark.rb > benchmark.txt
benchmark.rb:33:in `block (3 levels) in <main>': undefined method `push' for #<RR::DoubleDefinitions::DoubleDefinition:0x007fe9aa28e240> (NoMethodError)
from benchmark.rb:31:in `times'
from benchmark.rb:31:in `block (2 levels) in <main>'
from /Users/costro0001/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/benchmark.rb:280:in `measure'
from /Users/costro0001/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/benchmark.rb:362:in `item'
from benchmark.rb:30:in `block in <main>'
from /Users/costro0001/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/benchmark.rb:174:in `benchmark'
from /Users/costro0001/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/benchmark.rb:205:in `bm'
from benchmark.rb:11:in `<main>'
@chadoh
Copy link
Author

chadoh commented May 15, 2012

I tried to add in a comparison of RR, but I couldn't figure out what I was doing incorrectly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment