On JRuby by itself and JRubyOnRails

Posted on September 30, 2007

I heard about the JRuby project a while ago, but back at the time I barely scraped the surface of Ruby itself so I didn’t even give it a shot.

Yesterday I finally decided to give it a try, after reading a lot about in the ruby blogs around the net. My main goal was to try deploying Rails apps to a J2EE server to compare the speed and memory usage against the mongrel setup I’m using now.

Well, installing jruby and JBoss server was quite easy. Running the first app (a really simple one) on this setup was a bit more complicated, but I managed to do it. The GoldSpike plugin is really wonderful, I must say. The JBoss server was taking a lot of RAM (like 350Mb), but this was due to the fact I used the default configuration which loads tons of stuff I didn’t need, so if you take some time tweaking it it should use considerably less.

I was testing the performance of the app using the ApacheBenchmark tool. The first run had response times about 3-4 times greater then the mongrel ones (750ms vs. 400ms). The second and later runs were about 2 times faster, due to JBoss starting all the 4 default dispatchers for the app. The thing that stroke me was that after starting 3 more dispatchers the JBoss app memory usage grew only by, like, 4 or 5 megabytes. This became interesting. Then I wondered what would happen if I deployed more apps to the server (actually I deployed the same app under different names). With 4 deployed apps with 4 dispatchers each the JBoss process grew only to 400Mb. This looked quite promising, as with mongrels it would take 1650=800Mb at least. The performance is not quite good, but it can be fixed with more dispatchers.

Now I wanted to test a real-world app with it. That was the moment the trouble began. The app required the act_as_ferret plugin and the ferret gem. The gem wouldn’t install, due to some hardcoded calls to ‘ruby’, as I understand, but I simply removed the plugin and references to it in the models. Then came the simple_capcha plugin. It needs RMagick to draw the pictures, but the rmagick gem doesn’t work with JRuby. There is rmagick4j gem written to replace it, but it lacks lots of functionality needed by simple_captcha. I had to tweak the simple_captcha code to work with it, ending up with a picture with text but no effects whatsoever. Anyway, I now could run the app and it was working quite fine until i stumbled into something I totally didn’t expect. A list of options said:

  • 1 Year — 10.1$
  • 2 Year — 20.2$
  • 3 Year — 30.299999999999997$
  • Etc.

Whoa! This was something totally unexpected. I checked the code and it simply read (2..10).map{|x| [10.1x,x]}. So why this number? I checked in pure jirb — it was the same. Then I wrote to the jruby-users maling list about this issue and I was told it was a Java issue and there was nothing could do about it. It’s just the way Java counts floating point numbers. So i checked Java itself, and yes, it does work this way. 3 times 10.1 is 30.299999999999997. All the other languages i checked (ruby, C, Obj-C, php…) were kind egough to answer 30.3 to my simple request. I still do not know what to think of it, but it surely is not a way I want a language I work with to behave.

_Update:

I actually found out later that it was not a problem with java, but a difference in ruby and jruby behaviour in outputting float numbers. jruby just spits out the number with all the precision it has (as does java), while ruby (and others mentioned) has some sort of algoritm to hide the imprecision if it considers it small enough. I, personnally, consider that jruby should implement this kind of trick to mimic MRI’s behaviour in this case_

So, to conclude, I can only say that JRuby on a J2EE server is quite a good solution to host many small and simple apps (like if many of us needed hosting lots of small and simple apps), or a few simple apps with a really high load (dunno, something like del.icio.us). It’s very scalable, while not taking much RAM for extra apps/dispatchers. In the same time it’s not yet (I hope it changes, really) suitable for use with big and complex application, nor is it useful to host a single lightly loaded app. I think I’ll keep watching the subject, maybe someday I’ll end up by using it.

Comments
  1. SharkAugust 14, 2008 @ 03:44 PM
    Dear Sir, Can you please share the hack you did to make simple_captcha work with JRuby?
Post a comment
Comment