Saturday, September 8, 2007

The Future of Ruby is JRuby?




While fighting with ActiveRecord-JDBC (I thought Derby was built in JDK 1.6, but I still have to copy the derby .jar's into jruby/lib ???) this morning I ran across Ola Bini'a Blog and an a great prezo on JRuby. If you read between the lines (I obviously didn't see/hear the prezo) but Ola seems to be suggesting the future of Ruby is indeed JRuby. Slide after slide there are aspects of C-Ruby that are broken (and will not be fixed in 1.9) but where Java is superior : GC, performance (maybe), multi-core, internationalization, etc.

Monday, September 3, 2007

Ruby Only Netbeans Builds: Nothing to Be Angry About


I've never used IDEs that much (with the exception of Visual C# a while back} but I downloaded the latest build of the the Netbeans Ruby IDE. and it was amazing how fast it loaded without all that useless Java crap that causes the progress bar to take soooo long on the full-featured Netbeans 6.0 preview releases.

Sunday, September 2, 2007

Absolutely Worthless JRoR Benchmark: Tomcat 5.5 vs. Glassfish v3

So for grins (not worth much else, was looking for something to pass the time before my kids went to sleep) I decided to compare initial page load times for the hello world between Tomcat and Glassfishv3 using the Arun Gupta's tutorial app

Tomcat 5.5.23
Initial Load: 3.229s
Subsequent Loads: 0.115 0.323 0.323s

Glassfish v3
Inital Load: 9.671s
Subsequent Loads: 3.005 0.554 0.594s

Webrick
Initial Load: 1.278s
Subsequent Loads: 0.559s 0.623

For Webrick is the time to boot it was about 3-4 times as long as it took to start Tomcat and deploy app through the manager (no more than 10 seconds) but for Glassfish, deployments took anywhere from 29-52 seconds.

Not like it really matters, but the hardware/software: VMWare Debian 4.0 JRE1.6 running on aging AMD K7-1.5 with 1.2GB RAM.


franz-g4:~ mdfranz$ time wget http://192.168.100.91:8080/hello/say/hello
--19:30:56-- http://192.168.100.91:8080/hello/say/hello
=> `hello.4'
Connecting to 192.168.100.91:8080... connected.
HTTP request sent, awaiting response... 200 OK
Length: 86 [text/html]

19:30:59 (1.61 MB/s) - `hello.4' saved [86/86]


real 0m3.229s
user 0m0.002s
sys 0m0.009s

JRuby and Embedded Derby: First Steps

Before tackling ActiveRecord-JDBC I thought I'd play around with the embedded version of Apache Derby with JRuby. This actually took me a lot longer that it should of, mostly because its been months since I've done anything with JRuby and years since I've touched JDBC. But here are the basic steps, which probably aren't that much difference from any other JDBC in JRuby:

require 'java'
include_class "java.sql.Connection"
include_class "java.sql.DriverManager"
include_class "java.sql.ResultSet"
include_class "java.sql.Statement"
driver = org.apache.derby.jdbc.EmbeddedDriver.new()
conn = DriverManager.getConnection("jdbc:derby:derbyDB;create=true")
conn.setAutoCommit(false)
s = conn.createStatement()

Some of the things that tripped me up, as I was trying to convert the simple Java example to JRuby:
  • The Class.forName(driver).newInstance() nonsense, I guess I've never run across this convention before but probably not surprising since I'm not a Java developer
  • org.apache.derby.impl.jdbc.EmbedSQLException: Derby system shutdown - yes you should do a DriverManager.getConnection("jdbc:derby:;shutdown=true") and yes it apparently always throws an exception
  • Getting useful results sets back is where I spent most of my time. Fortunately Derby documentation (like most Apache projects) are among of the best of any, whether commercial or Open Source APIs.

Since Blogger mangles source code so badly, see the code over on my wiki. Next I'm going to try HSQLDB because it supports a larger set of rails migrations using ActiveRecord-JDBC than Derby, but the Hypersonic gem definitely had problems on OSX due to issues with YAJB. But me sneaking suspicions is that that library is for C-Ruby and not JRuby. Doh!

Saturday, September 1, 2007

JRuby/Rails on Glassfish Actually Works!

So traffic was so bad on Thursday I popped open my Powerbook on the Edens spur (I swear it took me an hour to go 3 miles) and listened to Java Posse 138 which had a lengthy (but quite excellent interview with several Sun developers from Glassfish. Java Posse is an excellent podcast. I wish there was something of comparable quality on Ruby, but no time for that rant.

Besides the performance improvements (indeed the startup time is quite amazing compared to JBOSS, Geronimo, or Sun's appserver) they mentioned running JRuby apps and I ran across this blog entry on running interpreted rails apps within Glassfish v3 and the Grizzly connector.

I had to make few changes to get it working to Linux-ify the tutorial (had to create a symlink within glassfish/lib to point to the jruby directory) but it was relatively straightforward on Debian 4.0, (JRuby 1.0.1 and jdk1.6.0_02)

Here the slight differences:

I set the environment variables in .bashrc


JAVA_HOME=~/jdk6
JRUBY_HOME=~/java/jruby
GLASSFISH_HOME=~/java/glassfish
PATH=$PATH:$JAVA_HOME/bin/:$JRUBY_HOME/bin:$GLASSFISH_HOME/bin


Install rails (added --no-ri)


# jruby -S gem install rails -y --no-ri --no-rdoc



Modify glassfish/config/asenv.conf instead of asenv.bat


JRUBY_HOME="/root/java/jruby"


Make asadmin exectuable (chmod u+x/700)

Saturday, August 25, 2007

Ruby Statistics Functions

The StatArray class provides some basic statistical operations to be used on arrays:

irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'statarray'
=> true
irb(main):003:0> a = [1,2,3,4,6]
=> [1, 2, 3, 4, 6]
irb(main):004:0> sa = a.to_statarray
=> [1, 2, 3, 4, 6]
irb(main):005:0> sa.mean
=> 3.2
irb(main):007:0> sa.median
=> 3

Wednesday, August 8, 2007

Ruby and Syslog

As others have also pointed out there is no ri documentation for the syslog module but there is a lot of useful stuff in the ext/syslog directory.

But here is the most basic example I can come up with:


irb(main):007:0> mdfranz@franz-t61:~$ irb
irb(main):001:0> require 'syslog'
=> true
irb(main):002:0> Syslog.open('labradoodle')
=> <#Syslog: opened=true, ident="labradoodle", options=3, facility=8, mask=255>
irb(main):003:0> Syslog.crit('w00f w00f')
=> <#Syslog: opened=true, ident="labradoodle", options=3, facility=8, mask=255>
irb(main):004:0> Syslog.close
=> nil
irb(main):005:0> Syslog.crit('w00f w00f')
RuntimeError: must open syslog before write
from (irb):5:in `crit'
from (irb):5
from :0

And /var/log/syslog

Aug 8 10:03:22 franz-t61 labradoodle[3688]: w00f w00f
Aug 8 10:04:24 franz-t61 kernel: [ 9700.304000] iwl4965: REPLY_ADD_STA failed
Aug 8 10:06:27 franz-t61 kernel: [ 9823.088000] iwl4965: REPLY_ADD_STA failed