Created
July 26, 2011 18:35
-
-
Save alexch/1107492 to your computer and use it in GitHub Desktop.
This file contains 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 "performance_monitor" | |
require "time" | |
describe PerformanceMonitor do | |
before do | |
@monitor = PerformanceMonitor.new | |
@eleven_am = Time.parse("2011-1-2 11:00:00") | |
end | |
it "takes about 0 seconds to run an empty block" do | |
@monitor.run do | |
end.should be_within(0.1).of(0) | |
end | |
it "takes exactly 0 seconds to run an empty block (with stubs)" do | |
Time.stub!(:now).and_return(@eleven_am) | |
@monitor.run do | |
end.should == 0 | |
end | |
it "takes about 1 second to run a block that sleeps for 1 second" do | |
@monitor.run do | |
sleep 1 | |
end.should be_within(0.1).of(1) | |
end | |
it "takes exactly 1 second to run a block that sleeps for 1 second (with stubs)" do | |
fake_time = @eleven_am | |
Time.stub!(:now).and_return { fake_time } | |
@monitor.run do | |
fake_time += 1 # adds one second to fake_time | |
end.should == 1 | |
end | |
it "runs a block N times" do | |
n = 0 | |
@monitor.run(4) do | |
n += 1 | |
end | |
n.should == 4 | |
end | |
it "returns the average time, not the total time, when running multiple times" do | |
run_times = [8,6,5,7] | |
fake_time = @eleven_am | |
Time.stub(:now).and_return { fake_time } | |
@monitor.run(4) do | |
fake_time += run_times.pop | |
end.should == 6.5 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment