Creating a LoadTest

Problem

You need to make sure that code executes correctly under varying load conditions, such as a large number of concurrent users.

Solution

Decorate an existing JUnit Test with a JUnitPerf LoadTest.

Discussion

A JUnitPerf LoadTest decorates an existing JUnit test to simulate a given number of concurrent users, in which each user may execute the test one or more times. By default, each simulated user executes the test once. For more flexibility, a load test may use a com.clarkware.junitperf.Timer to ramp up the number of concurrent users during test execution. JUnitPerf provides a ConstantTimer and RandomTimer to simulate delays between user requests. By default all threads are started at the same time by constructing a ConstantTimer with a delay of zero milliseconds.

Tip

If you need to simulate unique user information, each test must randomly choose a different user ID (for example). This can be accomplished using JUnit’s setUp( ) method.

Here is an example that constructs a LoadTest with 100 simultaneous users:

public static Test suite(  ) {
    Test testCase = new TestSearchModel("testAsynchronousSearch");
    Test loadTest = new LoadTest(testCase, 100);
    TestSuite suite = new TestSuite(  );
    suite.addTest(loadTest);
    return suite;
}

Here is an example that constructs a LoadTest with 100 simultaneous users, in which each user executes the test 10 times:

public static Test suite( ) { Test testCase = new TestSearchModel("testAsynchronousSearch"); Test loadTest = new LoadTest(testCase, ...

Get Java Extreme Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.