Testing Individual Response Times Under Load

Problem

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

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.

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( ); suite.addTest(loadTest); return suite; } public static void main(String args[]) { junit.textui.TestRunner.run(suite( )); ...

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.