Main

Services

Clients

OOXML

 
Posted 8 months ago by Nazar Aziz

Rails Unit Testing is Slow Under Cygwin and Windows - Solutions & Alternatives

2727 reads - 13 comments

Test Driven Development (TDD) has been with us close to a decade so if you haven't already started using this methodology now is the time.

Ruby on Rails (RoR) provides built-in support for unit testing through Rake commands. Cygwin is provides a Unix like environment for Microsoft Windows users.

As a RoR developer I prefer developing my Rails apps under Cygwin for the following reasons:

  1. My Rails applications are deployed on a Linux server. My development environment should mirror the deployment server as closely as possible to guarantee that my tests are not tainted or influenced by any quirks Windows introduces to the Windows port of Ruby.
  2. I use Capistrano for deployment. Capistrano requires a POSIX environment so it will not run under Windows. The only way to get Capistrano to work in Windows is via Cygwin. Capistrano enables me to deploy my Rails applications in seconds so it is a must have tool!
  3. Git is a distributed source control system. My main repository, which I share with clients and other developers runs on Subversion. However, I do the bulk of my non-day-job development on my daily train commute so I cannot access my SVN repository beyond the latest commit. Git allows me to carry my entire SVN repository with me which I then synch to SVN via git-svn. Git is not supported under Windows except vvia Cygwin.
  4. My last reason is a rant: i hate Windows command prompt. I hated cmd.exe in 1992 and I hate it even more today. Cmd.exe is as useful as a chocolate teapot and a few minutes working under it drives me to despair. I much prefer to Putty onto Cygwin.
Cygwin does a pretty good job of providing me with an environment that closely mirrors my web-server. I can now develop and test my Rails apps and incorporate Unix features (i.e. forking to Linux commands) and test these on my laptop.

There is one major drawback to this: Ruby is slow under windows. Windows native Ruby is slow under Windows but Cygwin Ruby is even slower. This become significant when:
  1. script/generate takes about 20-30 seconds to start
  2. ruby test/unit/my_model_test.ruby takes 30 seconds to start
  3. spec spec/my_model_spec.rb takes over a minute to start
  4. rake test...
Before you ask, "why not switch to OSX or Linux", my answer would be: I can't as I develop and maintain Windows applications and abandoning this platform (although more tempting every day) is not an option for the time being.

This makes TDD impractical as the start-up times of test scripts just takes too long. Windows Ruby takes a insignificantly long time to load the rails environment. I've watched a few Rails web casts and these suggest that under OSX script/generate and testing takes no more than a couple of seconds to start under this platform.

If TDD driven development is a must there are a few workarounds:

1. Switch to RSpec.

RSpec is based on BDD which in turn is based on TDD. An RSpec based plugin for Rails provides spec_server, which loads the Rails environment once and allows you to send tests against this server.

This cuts down the spec X command from a minute to at most a couple of seconds.

To install RSpec and the required spec_server:

ruby script/plugin install http://rspec.rubyforge.org/svn/tags/CURRENT/rspec
ruby script/plugin install http://rspec.rubyforge.org/svn/tags/CURRENT/rspec_on_rails

ruby script/generate rspec
Once installed, rake -T reports:
rake spec:server:restart          # reload spec_server.
rake spec:server:start # start spec_server.
rake spec:server:stop # stop spec_server.
At the command prompt:
rake spec:server:start
.. to start the spec server and load Rails' environment once. Then run subsequent tests using:
spec spec/my_model_spec.rb --drb
The --drb is critical as it instructs spec to use the running spec server.

2. Setup a RoR Development Environment Under a Virtual Linux Machine.

This started as an experiment but was quite an eye opener. Let me state it here:

Ruby runs faster under a Linux virtual machine in VMWare under windows than it does natively under Windows.

My HD space is limited on my ageing laptop so I needed an easy to setup and light weight distro. I settled on Xubuntu.

Download both VMware Workstation 6 and a Xubutu ISO from your closest mirror.

Create a new Linux virtual machine with a 6GB disk space. Point the virtual CD-ROM to the downloaded Xubuntu ISO and boot. Install Xubuntu.

Unfortunately setting up Xubuntu under VMWare and setting up a Rails development environment isn't as simple as I'd hoped it would be. Here are the steps I followed:

Setup VMWare and VMWare Tools

Download Linux complier and build environment:
sudo apt-get install build-essential linux-headers-`uname -r`
Before mounting the VMWare CD-Rom make sure that the VMWare virtual cd-rom is disconnected. From the VMWare main menu: VM -> Install VMWare Tools. This should mount the VMWare Tools cd onto your Linux environment. To install:
cd ~
mkdir vmtools && cd vmtools
cp /cdrom/VMWareTools-*.tar.gz .
tar xf * && cd *
sudo ./vmware-install.pl
and follow the prompts accepting all defaults. Once completed. The wheel on my mouse stopped working under Linux at this stage so:
sudo nano /etc/X11/xorg.conf
Find the "Configured Mouse" section section (use Control-w in nano) and change options "Protocol" to:
Options "Protocol" "imps/2"
Save with Control-x
sudo shutdown -r now
.. to restart. On Vmware's main menu: enabke View -> Autofix Guest. This will resize the Linux screen to match the VMware window. Press F11 to maximise the VMware window for the Linux window to expand to full screen.

Install Rails Enviroment under Ubuntu / Kubuntu / Xubuntu

sudo apt-get install ruby rdoc irb libyaml-ruby libzlib-ruby ri libopenssl-ruby ruby1.8-dev 
cd ~
mikdir gems && cd gems
wget http://rubyforge.org/frs/download.php/29516/rubygems-1.0.0.tgz
tar xf *
cd rubygems*
sudo ruby setup.rb
sudo ln -s /usr/bin/gem1.8 /usr/bin/gem
sudo gem update --system
sudo gem install rails
sudo gem install mongrel
sudo apt-get install mysql-server libmysqlclient15-dev
sudo gem install mysql
echo "export RUBYOPT=rubygems" >> ~/.profile

To install GIT

sudo apt-get build-dep git-core 
sudo apt-get install tk8.4
wget http://kernel.org/pub/software/scm/git/git-1.5.3.8.tar.bz2
tar xf * && cd *
./configure && make && sudo make install

To Install a RoR Development Environment Using Netbeans

Install the Sun Java 6 Console from Applications -> System -> Add/Remove
Get the latest Netbeans Ruby build from http://deadlock.netbeans.org/hudson/job/ruby/
cd ~
wget http://deadlock.netbeans.org/hudson/job/ruby/6212/artifact/ruby/rubyide/dist/netbeans-rubyide-hudson-6212.zip
unzip net*.zip
~/nbrubyide/bin/nbrubyide 

3. Switch to Linux

This is also a viable alternative and I must admit that it is a tempting alternative. I can still do Windows development by running my Windows development environment under VMWare for Linux.

4. Switch to Mac OSX

Also a very very tempting and viable alternative. My laptop is ageing and the MacBook or even MacBook air do look very inviting.

Digg it! Slashdot Del.icio.us Technorati Google Bookmarks Reddit! Dzone Yahoo! MyWeb

Comments - add your comment

What others have said:

 
By: Alex G

Drop cygwin and just get VMWare and install normal linux in it. That’s what I was doing and it works just fine.

Any rake task in rails has to load the whole rails environment first, hence the delay. Your 30 seconds seem a bit excessive thou (i assume you either have a huge app or a shitty box).

I just did a test–cold start windows, first rake load–25 seconds. second rake load 6 seconds. For testing, I recommend using Autotest, which loads rails and monitors file changes and only executes changed specs.

I have tried MacBook and hated it, especially for lack of tools im used to on Windows. If you split at least 50/50 on windows, don’t even bother going to OSX or Linux as main OS.

Posted 8 months ago

 
By: nazar
Joined: September 21, 2007
Posts: 15

Hi Alex.

Thanks for the suggestions and tips re Autotest: I’ve heard of Autotest before but didn’t know that it monitors changed tests and only runs those.

I should have mentioned that I do have an old Dell Laptop. I keep using it purely because it holds an 8 hour charge, which is a must for me as I am on the train 6-7 hours a day.

Also, thanks for the tip re MacBook.

Posted 8 months ago

 
andLinux as alternative to VMWare
By: Ray Ng

Thanks! I faced similar situation as you when developing Ruby/Rails on Windows.

andLinux could be an alternative to VMWare, as andLinux is free and use much fewer resources. For more information, please go to http://blog.rayngwf.com/2008/01/getting-almost-all-ruby-gems-and-c.html

Posted 8 months ago

 
But why?
By: Clemens

These are all good workarounds, BUT WHY is Ruby/Rails@win32 so much slower? Has this ever been investigated?

Posted 2 months ago

 
By: nazar
Joined: September 21, 2007
Posts: 15

Hi Clemens

Maybe NTFS is to blame? Or Windows’s disk caching? Linus has been known to bash Window’s file system and described it as being “broken”. Maybe there is some truth there?

Posted 2 months ago

 
By: Clemens

Dear Nazar,
thanks for replying.
I can’t imagine it’s ntfs (only). We talk about 2s vs 20s and there are not 1000s of files to load, are there?
IMHO the slowness comes from the InstantRails’s Ruby build (1.8.6-p0), but I might be wrong. Just downloaded the oneclick installer Ruby (1.8.6-p111) from http://rubyinstaller.rubyforge.org/wiki/wiki.pl and got an incredible performance boost…

Regards
Clemens

Posted 2 months ago

 
Why not Capistrano?

I’ve developed for Rails projects natively on Windows for about two years. I never had a problem with Capistrano–it requires a POSIX environment to DEPLOY to, not to run in. The only difference I noticed between running it on Windows and Linux (apart from speed) was the ssh password being printed to the screen as cleartext. I think that’s fixed now.

It’s definitely true that scripting languages like Python and Ruby run faster on Linux… Unit tests just crawl. I found Linux to be very productive indeed for developing Rails apps (I used Ubuntu but pure Debian is, I hear, faster).

In an office environment Ubuntu failed for me. Network issues, and an office reliance on Microsoft Exchange caused enough problems that I’m now back on Windows.

Posted 2 months ago

 
By: hoenth

I have been reading that developing RoR app on windows was more difficult than linux, largely due to the speed issues with loading tests. It is true that in the small app I am playing with, it takes a good 10 seconds to load up and run, with only a few tests. So I took the time yesterday to install vmware, ubuntu, and aptana studio (which I had been using in windows). After all was said and done, the performance difference was negligible. I had expected to be able to shift-alt-X U and see things turn from red to green or green to red relatively instantaneously. But such is not the case.

I am looking into rspec and the rspec server, but since I like to run things inside of RadRails, I am not sure if I can append the–dbi (I think?) to the test call that tells the test to run in the already running rspec server.

Since this was my first foray into Linux, i was pleasantly surprised at the ease of installation. A total of about 4 hours work to go from pure windows to windows and ubuntu (with rails and Radrails installed). Boy do I know what it is like to stand on the shoulders of giants.

Posted about 1 month ago

 
By: nazar
Joined: September 21, 2007
Posts: 15

Hi there.

Sorry to hear that you haven’t noticed much of a difference between Windows and Linux Ruby environments.

I’ve got a 13,000 LOC Rails app that takes about 12 seconds to start under Windows (for a script/console command at the command prompt) against three seconds for the same command under VMWare Ubuntu 7.10 Linux. The Windows start-up is severely worse once I start doing RSpec tests…

Posted about 1 month ago