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.
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.
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.
How long does your test run? Usually Rails tests take long due to database access. To speed them up as initial improvement you could:
stop using instantiated fixtures by setting:
self.use_instantiated_fixtures = false
start using transactional fixtures by setting:
self.use_transactional_fixtures = true
To improve tests performance even more please consider using preloaded fixtures.
精彩评论