
Speed tests in Ruby
April 11, 2008Maybe this is already somewhere, but I did not find it in the
Test::Unit::TestCase
documentation where I expected it. I want to make sure that a certain block of code would execute within a given time period. Granted, it’s a bit obvious, but in case you searched first like I did and found this:
class Test::Unit::TestCase
def assert_faster_than(delta)
start = Time.now.to_f
yield
finish = Time.now.to_f
assert_in_delta start, finish, delta, "Expected to take less than #{delta} seconds but took #{finish-start}."
end
end
# Then just use it like this....
assert_faster_than(3) { something_that_could_take_a_while }