About Me

My photo
Author of Groovy modules: GBench, GProf, Co-author of a Java book パーフェクトJava, Game Developer at GREE

Thursday, May 2, 2013

GBench Goodness: Compare the performance of different versions of Groovy using GVM

As I announced in my last post, "GProf Goodness: Profile a fork/join program written in GPars", I started Goodness series for GPerfUtils (GBench and GProf). And now it's the turn of GBench.

GBench is the benchmarking module of Groovy. It allow you to accurately and easily benchmark a Groovy program. GVM is a tool for managing parallel versions of Groovy and other Groovy-related tools. By combining the tools, you can easily compare the performance of different versions of Groovy. This is a script to compare the performance of Fibonacci number calculation among v1.7.11, v1.8.9, v2.0.8, and v2.1.3 (you have to install GVM first if you haven't done it yet).

source ~/.gvm/bin/gvm-init.sh
versions=(1.7.11 1.8.9 2.0.8 2.1.3)
for version in ${versions[*]}
  echo -n "Install groovy v${version}"
  gvm install groovy $version
echo "Okie-dokie, let's benchmark them!"
for version in ${versions[*]}
  gvm use groovy $version > /dev/null &&
  running_version=$(groovy -v | cut -d' ' -f 3) &&
  major_version=$(echo $running_version | cut -d'.' -f1,2) &&

  groovy -e "
    def fib(int n) {
        (n < 2) ? 1 : fib(n - 1) + fib(n - 2)
    // benchmark() extension is supported only in Groovy 2.0 or later versions.
    new groovyx.gbench.BenchmarkBuilder().run(quiet:true, measureCpuTime:false) {
        'v${major_version}' { fib(20) }
  " &&

  sleep 1

[NOTE] If you face "gvm: command not found" error, try the following approach until my pull request for fixing the issue is merged to GVM.

  1. Download gvm-include.sh from my Gists
  2. Make gvm-include.sh executable by "chmod +x gvm-include.sh"
  3. Replace "source ~/.gvm/bin/gvm-include.sh" in the benchmark script to "source /path/to/gvm-include.sh"

This is the results of the script in my environment. This shows us that the performance of numeric operations were highly improved in v1.8 at least:

v1.7  870633
v1.8  535506
v2.0  507253
v2.1  489244

Please send me pull requests at the Github page or any issues, requests to the Issue page to improve GBench.