8.6 Testing Individual Response Times Under Load

8.6.1 Problem

You need to test that a single user's response time is adequate under heavy loads.

8.6.2 Solution

Decorate your JUnit Test with a JUnitPerf TimedTest to simulate one or more concurrent users, and decorate the load test with a JUnitPerf TimedTest to test performance of the load.

8.6.3 Discussion

Testing whether each user experiences adequate response times under varying loads is important. Example 8-4 shows how to write a test that ensures each user (thread) experiences a 3-second response time when there are 100 simultaneous users. If any user takes longer than three seconds the entire test fails. This technique is useful for stress testing, and helps pinpoint the load that causes the code to break down. If there is a bottleneck, each successive user's response time increases. For example, the first user may experience a 2-second response time, while user number 100 experiences a 45-second response time.

Example 8-4. Stress testing
package com.oreilly.javaxp.junitperf;

import junit.framework.Test;
import junit.framework.TestSuite;
import com.clarkware.junitperf.*;

public class TestPerfSearchModel {

    public static Test suite(  ) {
        Test testCase = new TestSearchModel("testAsynchronousSearch");
        Test timedTest = new TimedTest(testCase, 3000, false);
        Test loadTest = new LoadTest(timedTest, 100);

        TestSuite suite = new TestSuite(  );
        return suite;
    public static void main(String args[]) {
        junit.textui.TestRunner.run(suite(  ));

8.6.4 See Also

Recipe 8.3 shows how to create a JUnitPerf TimedTest. Recipe 8.4 shows how to create a JUnitPerf LoadTest. Recipe 8.7 shows how to use Ant to execute JUnitPerf tests.