| Subcribe via RSS or via Email

Groovy 1.5, first thoughts

January 20th, 2008 | No Comments | Posted in Groovy

Groovy 1.5 was released at the start of the month, so I decided to re-run my speed test to see if it was any faster. Turns out that while it is still much slower than the java version, it is about 25% faster than the groovy 1.0 version.

The other new feature that I love about groovy 1.5 is the much improved groovy console, which is so much better that it can actually be used when you want to test out more than an 3 line script :)
Another interesting feature is the cross compiler, which can compile Java source code too.

How to unit test Java code and not lose your mind, part one

September 23rd, 2007 | No Comments | Posted in Groovy

Two of the universal truths about programming are; everyone knows that they should be unit testing, and that everyone (well everyone I know) hates unit testing.

Imagine the situation; you have a complex package of Java classes which you aren’t quite sure are working right. What do you do? You know that unit testing would be the right thing to do and although tools such as eclipse and JUnit make the process easier, its still going to be dull and time consuming. So much so that you wont bother unit testing and instead you will keep fixing bugs as they crop up, possibly causing new ones in the process (go on admit it you’ve been there and bought the t-shirt).

But what if you could combine unit testing with something useful like learning a cool new programming language . You just might be inclined to actually do some unit testing then. But what if as well as learning a new cool new language, you were also able to use the features of this new language to enable you to produce unit tests faster and which are more readable than if you were using plain old Java. And what if you could do all of this without scaring your boss by bringing in radical new language that requires its own runtime. Then you might actually get quite excited about unit testing. Well excited is probably the wrong word, enthusiastic is probably better (if you do indeed get excited about unit testing then I would suggest that you get professional help or take a holiday).

All this is possible with Groovy. If you haven’t heard about groovy then I suggest that you to to the groovy home page and read a bit about it before you carry on. It may also be useful to read my last post about Groovy.

The first things to remember about Groovy when unit testing are that JUnit is embedded in it and that a GroovyTestCase (which is an extension of a JUnit test case) has 11 extra assertion methods in addition to the standard JUnit ones. For more information see the page on Groovy unit testing.
Unit testing Java code from Groovy is just as easy as unit testing Java code from Java; remember that its all the same Java bytecode in the end.

Ok now its time for an example. Lets unit test my Java Fibonacci code from before with Groovy.

First write the Groovy test cases:

class JavaFibonacciTest extends GroovyTestCase

{
 void testFibResults()
 {
//some standard assertions from the JUnit TestCase
 	assertEquals(SpeedTestJava.fib(0), 0)  //fib 0 = 0
 	assertEquals(SpeedTestJava.fib(1), 1)  //fib 1 = 1
 	assertEquals(SpeedTestJava.fib(2), 1)  //fib 2 = 1
 	assertEquals(SpeedTestJava.fib(3), 2)  //fib 3 = 2
 	assertEquals(SpeedTestJava.fib(4), 3)  //fib 4 = 3

 }

 void testFibResults2()
 {
 	//some assertions from the GroovyTestCase
 	//assertToString converts the result to a string by calling its toString() method
 	assertToString(SpeedTestJava.fib(0), '0')
 	assertToString(SpeedTestJava.fib(4), '3')

 }

}

To run this you can simply use the command:

groovy JavaFibonacciTest.groovy

or you can compile the Groovy code using:

groovyc JavaFibonacciTest.groovy

and then run it using:

groovy JavaFibonacciTest

Either way the output will look like this:

..

Time: 0.04OK

(2 tests)

And that’s it. You’ve written a Test case for Java in groovy. Notice that you didn’t need a main method, or a constructor as you would have done to run the same code as a JUnit test case. Its also import to note that Groovy test cases don’t need a package declaration, for reasons which I don’t totally understand.

I think that’s about enough for now. In part two I will explain how to organise Groovy tests into test suites and how to integrate them with eclipse.

Groovy: my new favourite programming language

September 17th, 2007 | 1 Comment | Posted in Groovy

Recently I discovered the Groovy programming language. Basically it is a dynamic language that runs on the JVM and compiles to .class files just as Java does. A better description can be found on the groovy homepage.

However much as I like the syntax and features of Groovy I am concerned that groovy going to be too slow for anything complex. Some reports put it orders of magnitude slower than plain Java. I struggled to find benchmarks, so I decided to write my own trivial test. The algorithm that all programmers learn early in their career, the Fibonacci sequence.

I decided to use the traditional (but less efficient) recursive method. I also stored each result into a list as it is processed, although this wasn’t used in the computation. The two files look as follows:

SpeedTestJava.java:

import java.util.ArrayList;
import java.util.List;

public class SpeedTestJava
{
	public static void main(String[] args)
	{
		List list = new ArrayList();
		for(int index = 0; index <= 35; index++)
		{
			long currentTime = System.currentTimeMillis();
			fib(index);
			currentTime = System.currentTimeMillis() - currentTime;
			list.add(currentTime);
			System.out.println(index + ")t" + currentTime);
		}
	}
	public static int fib(int num)
	{
		if(num == 0 || num == 1)
		{
			return num;
		}
		else
		{
			return fib(num - 1) + fib(num - 2);
		}
	}
}

SpeedTestGroovy.groovy:

class SpeedTestGroovy
{
	static void main(args)
	{
		def list = []
		for(index in 0..35)
		{
			def currentTime = System.currentTimeMillis()
			fib(index)
			currentTime = System.currentTimeMillis() - currentTime
			list << currentTime
			println index + ")t" + currentTime
		}
	}
	static int fib(int num)
	{
		if(num == 0 || num == 1)
		{
			return num;
		}
		else
		{
			return fib(num - 1) + fib(num - 2);
		}
	}
}

The results were quite surprising. For Fibonacci of 35 the Java execution time was 171ms (~ 2 hundredths of a second), while for Groovy the execution time was a staggering 103989ms (just over 100 seconds). The Java code was 2 orders of magnitude faster (don’t hear that much eh?), which is a stupidly large amount for such a simple task. It is clear that performance in Groovy has a long way to go. I personally hope that it does get there and does not get labeled with the ‘is too slow’ tag that has stuck with Java since it was introduced.

After demonstrating that Groovy is slow, it should be noted that because Groovy can call Java code natively then it is trivial to put any time consuming code into Java and then call this from Groovy, for example (provided that SpeedTestJava.class is on the classpath when compiling and running the Groovy Code) it is possible to do this:

class SpeedTestGroovyJava
{
	static void main(args)
	{
		def list = []
		for(index in 0..35)
		{
			def currentTime = System.currentTimeMillis()
			SpeedTestJava.fib(index)
			currentTime = System.currentTimeMillis() - currentTime
			list << currentTime
			println index + ")t" + currentTime
		}
	}
}

This code executes in approximately the same time as the pure Java solution and is the perfect way to use the strengths of both languages. It would, for example, be much simpler in groovy to do some processing on the results list, to build a GUI to allow the user to specify which Fibonacci number to compute or to write the results to an XML file (or any file in fact).

Well thats the end of my first real blog post, it was quite interesting for me writing it, learning about some the different wordpress tools, and listening to the legend that is Sir Paul McCartney on Radio One.