Load Testing using a JUnit Test Case

Last week I had a requirement to execute a JUnit test multiple times concurrently in order to collect some performance metrics to see how much time it is taking to execute a service call with 2 separate sets of data.


Here is what I need to do:

  1. Execute a test with minimal data which triggers no search on the server but returns a +ve results.
  2. Execute a test with full set of data which triggers a deep search on the server side and return the results.
What normally comes to any programmers mind in this case is to write a Thread and execute it for multiple times concurrently. Or we might concurrently execute the JUnit to produce the same result. I thought of going the JUnit way and execute it multiple times. I want to make this a common utility so that we can use in all other places where such requirement exist.

I did a quick search to see if any such thing already exists that fits my requirement, that’s when I came across the JUnitPerf. It seemed promising to me. So I started going through the details of it. After 10 min I was successfully able to execute my first JUnit test with 500 users and 1 iteration per user. It is that simple.
JUnitPref is implemented as a collection of decorators on Junit Test to measure the performance and stability of the functionality that is JUnited.
There are 2 types of decorators that exist in JUnitPref are:
  1. Load test
Load test decorator simulates the load on the system with a given number of users and iterations.
  1. Timed test
Timed test is a decorator to measure the elapsed time of the test. This really simulates to see whether the requested functionality can be executed with the given elapsed time. [I did not tried this one yet.]

Simple Example:

I want to call a remote service method and get the response. I would like to measure the time it took to execute the method in order to get my expected output on an EJB. I am only interested in the time taken to execute the method but not the service lookup.

This is precisely my test case. I have couple of asserts after this test to conclude what I got is accurate.


JUnitPerf code:

try{
      lookup();//do the look up for the required EJB.
      long time = System.currentTimeMillis();
      response=remoteObj.retrieveAccessLevel(“userid”,“datax”,“datay”,plist<V>);
      long time2 = System.currentTimeMillis();
      System.out.println(“time to respond: “+(time2-time));
    }catch(Exception e){
      e.printStackTrace();
    }

You should implement the below constructor in order to execute the individual methods on the test case.

public MyMultiThreadedTest (String name){
    super(name);
}
Create another class as shown below and instantiate your JUnit class.

import junit.framework.Test;
import com.clarkware.junitperf.*;
public class MyMultiThreadLoadTest {
public static Test suite(){
   int maxUsers = 500;
   int maxIterations = 1;
   Test testCase = new MyMultiThreadedTest (“testAccessLevelRetrival”);
   Test loadTest = new LoadTest(testCase, maxUsers, maxIterations);
   return loadTest;
}
public static void main(String[] args) {
   junit.textui.TestRunner.run(suite());
}
}


Finally, run the test using TestRunner.

That’s it, now I can run the required JUnit test case with concurrent Threads. I felt this is much simpler than writing the multi threading on my own.


There are couple of things worth noting here:

  • JUnitPerf forces the decorated test to creates the threads itself.
  • JUnitPerf does not manage “runaway” threads.
References: