Created
October 8, 2010 10:13
-
-
Save ahx/616592 to your computer and use it in GitHub Desktop.
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
From 3786858be2d9ea56444fc0554a289a558d8bede9 Mon Sep 17 00:00:00 2001 | |
From: Andreas Haller <[email protected]> | |
Date: Fri, 8 Oct 2010 11:39:17 +0200 | |
Subject: [PATCH] `test_timer=on rake test` to print how long each test takes to run | |
--- | |
Rakefile | 7 +++++++ | |
lib/test_timer.rb | 40 ++++++++++++++++++++++++++++++++++++++++ | |
test/test_helper.rb | 7 +++++++ | |
3 files changed, 54 insertions(+), 0 deletions(-) | |
create mode 100644 lib/test_timer.rb | |
diff --git a/Rakefile b/Rakefile | |
index f122f77..bc4c0e9 100644 | |
--- a/Rakefile | |
+++ b/Rakefile | |
@@ -8,3 +8,10 @@ require 'rake/testtask' | |
require 'rake/rdoctask' | |
Pardy::Application.load_tasks | |
+ | |
+# You can print out the time for each test to find slow tests like | |
+# test_time=on rake test | |
+if(ENV["test_timer"]) | |
+ require File.expand_path('../lib/test_timer', __FILE__) | |
+ Rake.application['test'].enhance { TestTimer.print_result; } | |
+end | |
diff --git a/lib/test_timer.rb b/lib/test_timer.rb | |
new file mode 100644 | |
index 0000000..8c792eb | |
--- /dev/null | |
+++ b/lib/test_timer.rb | |
@@ -0,0 +1,40 @@ | |
+require 'pstore' | |
+module TestTimer | |
+ module TestCase | |
+ def self.included(klass) | |
+ klass.setup { TestTimer.start("#{self.class.name}::#{method_name}") } | |
+ klass.teardown { TestTimer.stop("#{self.class.name}::#{method_name}") } | |
+ end | |
+ end | |
+ | |
+ @@start = {} | |
+ @@result = PStore.new(Rails.root.join('tmp','test_timer_result.pstore'), true) | |
+ | |
+ module_function | |
+ def result | |
+ @@result | |
+ end | |
+ | |
+ def clear | |
+ result.transaction { result[:result] ||= {} } | |
+ end | |
+ | |
+ def print_result | |
+ result.transaction { | |
+ # sorting by value | |
+ puts result[:result].sort {|a,b| a[1] <=> b[1]} | |
+ } | |
+ end | |
+ | |
+ def start(name) | |
+ @@start[name] = Time.now.to_f | |
+ end | |
+ | |
+ def stop(name) | |
+ time = Time.now.to_f - @@start[name] | |
+ result.transaction { result[:result][name] = time } | |
+ end | |
+ | |
+ # Clear | |
+ self.clear | |
+end | |
diff --git a/test/test_helper.rb b/test/test_helper.rb | |
index d9c608e..33eb994 100644 | |
--- a/test/test_helper.rb | |
+++ b/test/test_helper.rb | |
@@ -46,3 +46,10 @@ Dir[Rails.root.join *%w(test fixtures files *.html)].each{|file| | |
) | |
} | |
+# See Rakefile for explanation | |
+if(ENV["test_timer"]) | |
+ require "test_timer" | |
+ class ActiveSupport::TestCase | |
+ include TestTimer::TestCase | |
+ end | |
+end | |
-- | |
1.7.1.1+GitX |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To print how long each of your tests takes to run call:
test_timer=on rake test