问题
Background:
I am using the RailsInstaller 2 package from Engine Yard, which means Ruby 1.9.2-p290 and Rails 3.1.1, on Windows 7 x64.
Problem:
While attempting the Ruby on Rails Tutorial and after getting autotest and spork to work as intended, it is clear while rspec reported that the tests finished in a few seconds, the actual time consumed is much greater than that and closer to half a minute, a far cry from the split-second response as observed in the screencasts. I am aware that most of that can be attributed to the Rails load time (also evident from amount of time spork spends at the preloading stage), and how JRuby is slower (as compared to Ruby on linux), but 15+s per rspec run on average (with or without spork, taking into account the load times) is quite untenable for TDD. Are there further ways to reduce it to the order of a few seconds, short of switching to Linux?
Edit: is there something wrong with the way I worded this question?
回答1:
Rails loading time is quite long. As you mentioned this can be a TDD killer.
There are several approaches on how to tackle the problem:
- Use Guard to notify fs events and to invoke forking rails using spork, and to invoke rspec tests run on spec file or SUT (subject under test) file change.
- Move logic outside of rails dependent classes (controllers, active-record models) and use plain ruby object instead.
I use both approaches. The second approach has many advantages:
- You can test your classes in isolation with no dependency of rails
- Tests will run super fast since you are not loading rails, only specific required classes
- It drives you towards SRP (single responsibility principle) where each class / module has a small part of the whole system
- It encourages DRY (Don't repeat yourself) - when you have tiny pieces it is easier to reuse them.
- You are using DI (Dependency Injection) to give context to your small objects which makes them easier to test and reuse.
Must watch:
- http://arrrrcamp.be/videos/2011/corey-haines---fast-rails-tests/
- http://pyvideo.org/video/631/fast-test-slow-test
回答2:
This is probably the best at the moment. An identical setup on Windows translates to mere seconds on Linux. I have not tested on Ruby 1.9.3 but that is unlikely to change the situation significantly.
回答3:
I recently came up with a solution that I haven't seen mentioned elsewhere:
I run Ubuntu in VirtualBox, and have my project's directory configured as a shared folder. This means that the Ubuntu box always sees the latest version of my code, so I can enjoy the linux CLI time, and still keep Windows as my development platform. I've been using this setup for only about a week so far, but I haven't encountered any downsides as of yet.
来源:https://stackoverflow.com/questions/8906872/how-to-optimize-rails-rspec-load-time-on-windows-to-the-order-of-seconds-with