Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are my rails tests so slow?

Is it normal for my test suite to take 5 seconds just to launch? Even when running an empty suite it still takes this long. Is it because it's firing up a new instance of rails on each run? If so, is there anyway to keep it persistent?

Example:

rlepidi@rlepidi:~/projects/rails/my_project$ time rake test
/usr/bin/ruby1.9.1 -I"lib:test" "/var/lib/gems/1.9.1/gems/rake-0.8.7/lib/rake/rake_test_loader.rb" "test/unit/release_test.rb" 
Loaded suite /var/lib/gems/1.9.1/gems/rake-0.8.7/lib/rake/rake_test_loader
Started

Finished in 0.000181867 seconds.

0 tests, 0 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
0% passed

real    0m4.173s
user    0m3.820s
sys 0m0.288s

As you can see, this empty test is really fast, but there is still 4 seconds of overhead for some reason. I'm using Test::Unit with Shoulda.

like image 731
ryeguy Avatar asked Apr 05 '10 20:04

ryeguy


2 Answers

Test speed bottleneck is the time it takes Rails to load its environment. Run script/console and see how long it takes to load you into your environment...it should be the same.

One you have a large enough codebase with many plugins & gems, load time can be as high as 1 minute!! If you are like me & stop running tests b/c you are sick of waiting 30 seconds just to test a small change, you need to run a test server like spec_server, or even better: spork!

You will find that there may be strange issues dealing with class cacheing with spec_server which is why I prefer spork.

like image 196
simianarmy Avatar answered Sep 20 '22 09:09

simianarmy


I was having the same problem and I resolved it by installing ruby entreprise edition (REE). I don't know why but loading the rails environment took 4-5 seconds under my ruby 1.9.2, vs 1-2 seconds with REE.

I installed it using RVM.

like image 42
alarive Avatar answered Sep 20 '22 09:09

alarive