h1

Speed tests in Ruby

April 11, 2008

Maybe 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 }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: