Ruby on Rails 3.1 with Ruby 1.9.2 – speeding up rspec 2 (2.11) tests

After reading a number of posts on speeding up tests with Rails:
* http://www.skuunk.com/2012/05/speeding-up-rspec.html
* http://www.dixis.com/?p=553
* http://stackoverflow.com/questions/3663075/speeding-up-rspec-tests-in-a-large-rails-application
* http://www.rubyinside.com/careful-cutting-to-get-faster-rspec-runs-with-rails-5207.html
* http://highgroove.com/articles/2012/04/06/sane-rspec-config-for-clean,-and-slightly-faster,-specs.html
* http://blog.plataformatec.com.br/2011/12/three-tips-to-improve-the-performance-of-your-test-suite/
* https://makandracards.com/makandra/950-speed-up-rspec-by-deferring-garbage-collection
* http://kpumuk.info/ruby-on-rails/my-top-7-rspec-best-practices/

We implemented a combination of practices to reduce our test execution time from 15 minutes for 549 tests down to 7 minutes!:

* Implement the shared connection code to ensure Capybara and non-request tests share a database connection via spec/spec_helper.rb: http://kpumuk.info/ruby-on-rails/my-top-7-rspec-best-practices/

class ActiveRecord::Base
  mattr_accessor :shared_connection
  @@shared_connection = nil
  def self.connection
    @@shared_connection || retrieve_connection
  end
end

* Decrease logging from DEBUG to INFO in spec/spec_helper.rb – http://blog.plataformatec.com.br/2011/12/three-tips-to-improve-the-performance-of-your-test-suite/

 Rails.logger.level = 2

* Switch from rspec transactions to database_cleaner transactions in spec/spec_helper.rb – http://highgroove.com/articles/2012/04/06/sane-rspec-config-for-clean,-and-slightly-faster,-specs.htm. This change sped up test runs by over 30%!

  RSpec.configure do |config|

    config.before(:all) {
      DatabaseCleaner.strategy = :transaction
      DatabaseCleaner.clean_with(:truncation)
    }

    config.before(:each) {
      Rails.application.routes.default_url_options[:host] = %{example.com:80}
      DatabaseCleaner.start
    }

    config.after(:each) {
      DatabaseCleaner.clean
    }

Tried tweaking Ruby’s GC performance with 1.9.2 but couldn’t find settings that helped performance noticeably without starving the system of memory.

This entry was posted in Ruby, Ruby on Rails. Bookmark the permalink.

Leave a comment