Over a million developers have joined DZone.
Gold Partner

A Novel Approach to Load Testing with ExecutorService

· Java Zone

The Java Zone is brought to you in parnership with New Relic. Read about the 10 Surprisingly Interesting Ways to Earn a Living Using Java and rejuvenate your love for programming.

Although 1.5's concurrency improvements have been around for yonks, they provide quite a handy means of parallel programming.

A scenario

Let's say you have some web service that's exposed via an interface called 'UserStorageDAO'. A typical implementation of this would call a web service with your given POJO instance and return true/false on successful storage. A quick and dirty way of seeing how resilient your setup is would be in a heavy usage scenario is to hit that service with as many concurrent calls as might be relevant - say 100.

Using our age-old friend JSPs, we can quickly hack something together using the Java 1.5 java.util.concurrent package that'll take x number of threads and run them in a managed manner; much easier than your normal route. 


// inside a JSP declaration tag (for defining class methods), with java.util.concurrent.* imported...
public UserStorageThread implements Runnable {
private int max;

public UserStorageThread(int max) {
this.max = max;

public void run() {
UserStorage storer = new UserStorageImpl();
User user = new User();
// set some random props on user
for (int i = 0; i < this.max; i++) {

 Above is just a simple Thread class that'll be executed by our ExecutorService instance. In here  we call our interface for storing with our web service.

Now, let's consider a simple form that has input for the maximum above. The use case is simple: the user enters a maximum number of threads they want executed and we create a limited number of threads, and then set them to run. It's important to note that we don't want to generate N threads where N = maximum as this could overload our server(s). Instead, we should generate a smaller amount, say 10, and hand them the work.

ExecutorService ex = new ExecutorService();
int max = Integer.parseInt(request.getParameter("max"));// null & integer checks omitted for brevity
int countPerThread = (int) max / 10;
for (int i = 0; i < 10; i++) {
ex.submit(new UserStorageThread(countPerThread));// exec thread when it chooses


So now we have some simple code that'll create our threads and execute them asynchronously for us. This saves us a lot of hassle with implementation detail. 

What's also great about the ExecutorService is its ability to force shutdown or the Future objects that it returns from the submit/invoke methods. These tell us whether the particular threads have finished or not. Some useful stuff.

You could also extend this JSP to force shutdown should you need to (using shutdownNow()) or even execute Runnables in a collection you already have. 


If you want a quick and easy web service testing tool for features such as one-click submission to your WS or running a fake instance of a service locally you can use the excellent tool SoapUI which boasts features such as:

  • service simulation
  • functional testing of web services
  • load testing
  • multiple protocol support - not just SOAP
The feature list is extensive and there's an open source version available too.

The Java Zone is brought to you in parnership with New Relic. Read about the 10 Surprisingly Interesting Ways to Earn a Living Using Java and rejuvenate your love for programming.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}