After following a few inspiring talks at this year’s JAZOON conference I decided to try to optimize some build processes with regard to better use of multicore architecture. Specifically, I was slightly annoyed by the long-running, serial execution of some complex integration tests and decided to parallelize their execution.

The set of tests I wanted to optimize is basically a JUnit-library based suit of test cases executed with the maven-failsafe-plugin, as well as a set of (real) unit tests executed with surefire. The first thing I though of was using JUnits wonderful Test Runner abstraction. Basically, I was expecting something like this to exist:

@RunWith(ParallelExecutor.class)
public class SomeDomainObjectTest {
   ....
}

Surprisingly, such an extension seems to have never made it into stable JUnit releases. While there is support for concurrency, I only came across custom runners providing such features. While certainly a good idea, I did not want to introduce such dependencies (or write my own extensions). So I gave up explicitly parallelizing the test cases in the code and turned to configuring concurrent execution in maven. This, however, is a straightforward thing to do but suprisingly little known. A nice blog post on DZone shows how it works. Simply add the following configuration to the maven surefire and failsafe plugin configurations:

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-failsafe-plugin</artifactId>
	<configuration>
		<parallel>classes</parallel>
	</configuration>
</plugin>

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-surefire-plugin</artifactId>
	<configuration>
		<parallel>classes</parallel>
	</configuration>
</plugin>

While there are more parameters to adjust, the default configuration (two threads per core) seem decent. Also, my measurements strongly suggest that parallel execution of classes is significantly faster (around 30% – 50%) for normal test cases than parallelizing on a per-method basis. However this may change if you have few classes with many long-running methods.
With this simple setup, I already had a speed up of around 30% a dual-core processor which ran both the tests and the test subject (a heavyweight JEE Application) at the same time. On a Quadcore integration system, the gain was even better (about 200%). Sweet!

Share →

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>