Dealing with slow tests and JRuby on Rails

I have started using JRuby in a Rails project and mostly I am very happy with it. However, one major issue is that Rails startup under JRuby is very slow… and I mean veeery slow. Running a single unit test under JRuby takes 15-16 seconds while running the same test using Ruby (MRI) takes around 0.15 seconds, a factor 100.

The reason for this is that startup of the Rails environment takes a long time under JRuby. When everything is up and running however, performance is great!

So, how to deal with this?

I figured the best way would be if it was possible to run tests using either JRuby or MRI. This would boost the speed in the development process while keeping the possibility or running tests with JRuby once or twice before deployment. So, how could this be accomplished.

Actually it was very simple. By adding a few

if RUBY_PLATFORM =~ /java/
 ...
end

in strategic places I was able to run almost everything under MRI.

These conditionals also needed to find their way into database.yml since it is necessary to choose either JDBC or ruby db driver depending on which “mode” we are running in.

Excerpt from database.yml:

test:
<% if RUBY_PLATFORM =~ /java/ %>
  <<: *defaults_jdbc
  url: jdbc:mysql://127.0.0.1/db_test?useUnicode=true&characterEncoding=UTF-8
<% else %>
  <<: *defaults
  host: 127.0.0.1
  database: db_test
<% end %>

So, now I can enjoy the power of Java whenever I like and I can still run 95% of my unit tests (which do not excercise Java-dependent code) as fast as ever possible!

Leave a Reply