Skip to content

Instantly share code, notes, and snippets.

@alexch
Created July 26, 2011 18:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexch/1107412 to your computer and use it in GitHub Desktop.
Save alexch/1107412 to your computer and use it in GitHub Desktop.
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]
run_index = 0
fake_time = @eleven_am
Time.stub(:now).and_return { fake_time }
@monitor.run(4) do
fake_time += run_times[run_index] # add 8, then 6, ... seconds to fake_time
run_index += 1
end.should == 6.5
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment