Jul 29, 2011

High-speed start-up Jython/Clojure by GroovyServ

GroovyServ:
https://github.com/kobo/groovyserv

GroovyServ made a start-up time of the invocation of Groovy script very quick. However, it can be used not only for Groovy but for all JVM languages, e.g. Scala, Jython and Clojure, etc.

On Linux, when setting the following aliases, You can enjoy the power of GroovyServ for Jython:
alias gython="groovyclient -cp /tmp/jython.jar -e 'import org.python.util.jython; jython.main(args)' --"
$ time jython -c "print('Hello')"
Hello

real    0m2.503s
user    0m2.891s
sys     0m0.431s

$ time jython -c "print('Hello')"
Hello

real    0m2.613s
user    0m2.889s
sys     0m0.435s

$ time gython -c "print('Hello')"
Hello

real    0m0.959s
user    0m0.001s
sys     0m0.003s

$ time gython -c "print('Hello')"
Hello

real    0m1.156s
user    0m0.001s
sys     0m0.003s

Gython(Jython + GroovyServ) is faster by one second than normal Jython. In the above way, the overhead which groovyclient passes the classpath of the additional jython.jar to groovyserver at each invocation is so large. You can do it still faster by starting groovyserver with CLASSPATH environment variable which has jython.jar.

$ alias gython="groovyclient -e 'import org.python.util.jython; jython.main(args)' --"
$ export CLASSPATH=/tmp/jython.jar
$ groovyserver -r
$ time gython -c "print('Hello')"
Hello

real    0m0.909s
user    0m0.001s
sys     0m0.002s

$ time gython -c "print('Hello')"
Hello

real    0m0.076s
user    0m0.001s
sys     0m0.003s

$ time gython -c "print('Hello')"
Hello

real    0m0.045s
user    0m0.001s
sys     0m0.003s
Yeah!

Notice 1: I can't assure that a testing framework or a complex library works well. Do it by your self-responsibility ;-)

Notice 2: If your OS is Windows, use Groovy and GroovyServ which are installed from Zip archives. I experienced a trouble by using them of Windows-installer of version 1.8.0.

Examples in my .bashrc

alias glojureserver="env CLASSPATH=/usr/local/Cellar/clojure/1.2.0/clojure.jar groovyserver"
alias glojure="dgroovyclient -e 'import clojure.main;main.main(args)' --"
alias gythonserver="env CLASSPATH=/usr/local/Cellar/jython/2.5.2/libexec/jython.jar groovyserver"
alias gython="groovyclient -e 'import org.python.util.jython; jython.main(args)' --"
alias jvmserver="env CLASSPATH=/usr/local/Cellar/jython/2.5.2/libexec/jython.jar:/usr/local/Cellar/clojure/1.2.0/clojure.jar groovyserver"
Usage:
$ glojureserver -r -v
Groovy command path: /usr/local/bin/groovy (found at PATH)
GroovyServ home directory: /usr/local/Cellar/groovyserv/0.7/libexec
Original classpath: /usr/local/Cellar/clojure/1.2.0/clojure.jar
GroovyServ default classpath: /usr/local/Cellar/clojure/1.2.0/clojure.jar:/usr/local/Cellar/groovyserv/0.7/libexec/lib/*
Killed groovyserver of 56789(1961)
Restarting groovyserver
Starting....
groovyserver 59802(1961) is successfully started

$ time glojure -e "(println 'Hello)"
Hello

real    0m0.867s
user    0m0.001s
sys     0m0.004s

$ time glojure -e "(println 'Hello)"
Hello

real    0m0.053s
user    0m0.001s
sys     0m0.004s

$ gythonserver -r -v
Groovy command path: /usr/local/bin/groovy (found at PATH)
GroovyServ home directory: /usr/local/Cellar/groovyserv/0.7/libexec
Original classpath: /usr/local/Cellar/jython/2.5.2/libexec/jython.jar
GroovyServ default classpath: /usr/local/Cellar/jython/2.5.2/libexec/jython.jar:/usr/local/Cellar/groovyserv/0.7/libexec/lib/*
Killed groovyserver of 59802(1961)
Restarting groovyserver
Starting....
groovyserver 59938(1961) is successfully started

$ time gython -c "print('Hello')"
Hello

real    0m1.540s
user    0m0.001s
sys     0m0.004s

$ time gython -c "print('Hello')"
Hello

real    0m0.108s
user    0m0.001s
sys     0m0.003s

$ time gython -c "print('Hello')"
Hello

real    0m0.057s
user    0m0.001s
sys     0m0.004s