Skip to content

Instantly share code, notes, and snippets.

@ngauthier
Created February 14, 2012 16:50
Show Gist options
  • Save ngauthier/1828061 to your computer and use it in GitHub Desktop.
Save ngauthier/1828061 to your computer and use it in GitHub Desktop.
333% Test Speedup

333% Test Speedup

  1. Normal Rails setup: 30 seconds for rake test
  2. Using 1.9.3-falcon: 22 seconds
  3. Using merged task w/ db reset: 16 seconds
  4. Using merged task w/o db reset: 12 seconds
  5. Using raw ruby execution: 9 seconds

You can run rake test:fast:db to get a merged run that resets your db. However, if you have clean tests that don't pollute the db between runs, you can just run rake test:fast and it will omit db:reset. This means your sequence numbers (under PostgreSQL, at least) may keep increasing, but if you tests against those I have other words for you.

xoxo <3 <3 <3 @ngauthier

# Usage: rtest.sh test/**/*_test.rb
ruby -Ilib:test `bundle list rake`/lib/rake/rake_test_loader.rb $*
namespace :test do
desc 'Run tests quickly by merging all types and not resetting db'
Rake::TestTask.new('fast') do |t|
t.libs << 'test'
t.pattern = "test/**/*_test.rb"
end
namespace :fast do
desc 'Run tests quickly, but also reset db'
task :db => ['db:test:prepare', 'test:fast']
end
end
@dstrelau
Copy link

I think you may run into problems trying run the tests in the context of the rake task, since at that point Rails has already loaded (MyApp::Application.load_tasks) without being in the test env — normally the first thing the sub-process does is load test_helper.rb, which defines ENV['RAILS_ENV'] = 'test' at the top.

If you're not using the rake dependencies or other niceties, you can instead bypass rake altogether. In the end, Rake essentially just requires all the files sequentially, so you can just wrap that in a small script. Here's some quick benchmarks with that approach: https://gist.github.com/1839453

@ngauthier
Copy link
Author

Awesome, yeah that was what I was going to try next. Maybe turn that into a bash alias or something. If you enable bash's globstar you can just do ruby -Itest -Ilib test/**/*_test.rb.

@ngauthier
Copy link
Author

Updated with rtest.sh which is even faster.

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