Permalink
Browse files

add a timer for timing requests

  • Loading branch information...
1 parent 4646385 commit 148294e7e6945c35f9e53610aba39a0928f6ca0d @jamesgolick committed Apr 30, 2009
Showing with 33 additions and 0 deletions.
  1. +1 −0 lib/trample.rb
  2. +9 −0 lib/trample/timer.rb
  3. +23 −0 test/timer_test.rb
View
@@ -8,6 +8,7 @@ module Trample
autoload :Runner, File.dirname(__FILE__) + "/trample/runner"
autoload :Cli, File.dirname(__FILE__) + "/trample/cli"
autoload :Logging, File.dirname(__FILE__) + "/trample/logging"
+ autoload :Timer, File.dirname(__FILE__) + "/trample/timer"
class << self
attr_reader :current_configuration
View
@@ -0,0 +1,9 @@
+module Trample
+ module Timer
+ def time
+ start = Time.now
+ yield
+ Time.now.to_f - start.to_f
+ end
+ end
+end
View
@@ -0,0 +1,23 @@
+require 'test_helper'
+
+class TimerTest < Test::Unit::TestCase
+ context "Timing the execution of a block" do
+ setup do
+ now = Time.now
+ invocations = 0
+ stub(Time).now do
+ invocations += 1
+ # not sure why we have to start at 1,
+ # but this seems to get called somewhere
+ invocations == 2 ? now : (now + 1.5)
+ end
+
+ @obj = Class.new { include Trample::Timer }.new
+ end
+
+ should "return the amount of time elapsed while the block was run" do
+ assert_equal(1.5, @obj.time {})
+ end
+ end
+end
+

0 comments on commit 148294e

Please sign in to comment.