Skip to content

Instantly share code, notes, and snippets.

@alainravet
Created March 14, 2009 14:11
Show Gist options
  • Save alainravet/79077 to your computer and use it in GitHub Desktop.
Save alainravet/79077 to your computer and use it in GitHub Desktop.
benchmark your ruby tests
# modified version of http://github.com/gittobi/tobi_test_timer
# differences :
# - prints all the tests
# - adds a counter and a bargraph
#
# output :
#. 336 2222222 : 2.584 - test_create_an_image_with_some_participations_in_one_go(ImageAdvancedTest)
#. 337 ++ : 0.173 - test cleanup_user_picture_path() does not translate proper upload
#. 338 ++ : 0.135 - test cleanup_user_picture_path() translates tmp upload path(ImagePathRepairTest)
#. 339 : 0.005 - test shorten_image_url() clears 1 level(ImagePathRepairTest)
#. 340 1111 : 1.817 - test adding an illustration to a public production should ..
class Test::Unit::TestCase
@@counter = 0
def run_with_test_timing(*args, &block)
return if self.name.starts_with?('default_test(')
result = args[0] # Test::Unit::TestResult
# extend the class to do what we want
# Note this does not work with the regular class_eval
# I was unable to successfully oepn up Test::Unit::TestResult
result.class.class_eval do
def add_error_with_log_error(error)
puts "\nERROR: #{error.test_name}"
add_error_without_log_error(error)
end
def add_failure_with_log_failure(failure)
puts "\nFAILURE: #{failure.test_name}"
add_failure_without_log_failure(failure)
end
alias_method_chain :add_error, :log_error unless method_defined?(:add_error_without_log_error)
alias_method_chain :add_failure, :log_failure unless method_defined?(:add_failure_without_log_failure)
end
#time the test
begin_time = Time.now
run_without_test_timing(*args, &block)
end_time = Time.now
duration = (end_time - begin_time)
# aravet
# long_test = duration > '5.0'.to_f
# puts "\nSLOW TEST: #{duration} - #{self.name}" if long_test
if duration > '10.0'.to_f then puts " %4d ******************** : %.3f - #{self.name}" % [@@counter, duration]
elsif duration > '7.0'.to_f then puts " %4d 77777777777777 : %.3f - #{self.name}" % [@@counter, duration]
elsif duration > '5.0'.to_f then puts " %4d 5555555555 : %.3f - #{self.name}" % [@@counter, duration]
elsif duration > '2.0'.to_f then puts " %4d 2222222 : %.3f - #{self.name}" % [@@counter, duration]
elsif duration > '1.0'.to_f then puts " %4d 1111 : %.3f - #{self.name}" % [@@counter, duration]
elsif duration > '0.5'.to_f then puts " %4d +++ : %.3f - #{self.name}" % [@@counter, duration]
elsif duration > '0.1'.to_f then puts " %4d ++ : %.3f - #{self.name}" % [@@counter, duration]
else puts " %4d : %.3f - #{self.name}" % [@@counter, duration]
end
@@counter += 1
#aravet/end
end
# Since we require the test helper in all tests we need to check this out.
alias_method_chain :run, :test_timing unless method_defined?(:run_without_test_timing)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment