Faster tests with database_cleaner’s :deletion strategy

Here are some (informal) benchmarks of the speed of our test suite, using database_cleaner and running on Postgres version 9.1.11.

In short, try the :deletion strategy.

4 minutes, 23 seconds: using database_cleaner as described by Avdi Grimm.

3 minutes, 18 seconds: tweaking Avdi’s config to only truncate non-empty tables:

RSpec.configure do |config|
  ...
  config.before(:each, :js => true) do
    DatabaseCleaner.strategy = :truncation {pre_count: true}
  end
  ...
end

2 minutes, 9 seconds: tweaking Avdi’s config to use the :deletion strategy:

RSpec.configure do |config|
  ...
  config.before(:each, :js => true) do
    DatabaseCleaner.strategy = :deletion
  end
  ...
end

For the record, José Valim’s ActiveRecord shared connection solution was a couple seconds faster than even the :deletion strategy. However, it was causing some bugs, and some tests were failing where they should have been passing, so I could not get it to work properly.

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