Skip to content

Instantly share code, notes, and snippets.

@amatsuda
Last active December 23, 2015 06:29
Show Gist options
  • Save amatsuda/6594242 to your computer and use it in GitHub Desktop.
Save amatsuda/6594242 to your computer and use it in GitHub Desktop.
Want to know how much time you're spending for cleaning database before and after each test?
# put this piece of code in your spec/spec_helper.rb or spec/support/any_rb_file.rb
# then run `rake spec`
module CleanerBench
def clean(*)
now = Time.now
super
@time_spent ||= 0
@time_spent += Time.now - now
end
def clean_with(*)
now = Time.now
super
@time_spent ||= 0
@time_spent += Time.now - now
end
end
DatabaseCleaner.singleton_class.send :prepend, CleanerBench
at_exit { p "Spent #{DatabaseCleaner.instance_variable_get(:'@time_spent')} seconds for cleaning database." }
#=> 35.698736906051636 seconds under database_cleaner (transaction + truncation if js: true),
# 4.483331918716431 seconds under database_rewinder ( https://github.com/amatsuda/database_rewinder ) in my app.
@mariovisic
Copy link

1836 tests in total for a Rails app

DatabaseCleaner w/ transactions, deletion for some integration: 1.3523050000000003 seconds
DatabaseCleaner w/ deletion for all tests:                      10.618906999999998 seconds
DatabaseRewinder w/ deletion:                                   0.4486619999999972 seconds

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment