-
-
Save chadoh/2704578 to your computer and use it in GitHub Desktop.
Mocha VS MiniTest::Mock and SimpleDelegate VS SimpleMock
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
⮁ 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>' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I tried to add in a comparison of RR, but I couldn't figure out what I was doing incorrectly.