Creating a Timed Test for Varying Loads
Problem
You need to test throughput under varying load conditions.
Solution
Decorate your JUnit Test with a JUnitPerf
LoadTest to simulate one or more concurrent users,
and decorate the load test with a JUnitPerf
TimedTest to test the performance of the load.
Discussion
So far we have seen how to create timed and load tests for existing JUnit tests. Now, let’s delve into how JUnitPerf can test that varying loads do not impede performance. Specifically, we want to test that the application does not screech to a halt as the number of users increases. The design of JUnitPerf allows us to accomplish this task with ease. Example 8-3 shows how.
Example 8-3. Load and performance 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 loadTest = new LoadTest(testCase, 100);
Test timedTest = new TimedTest(loadTest, 3000, false);
TestSuite suite = new TestSuite( );
suite.addTest(timedTest);
return suite;
}
public static void main(String args[]) {
junit.textui.TestRunner.run(suite( ));
}
}Remember that JUnitPerf was designed using the decorator pattern. Thus, we are able to decorate tests with other tests. This example decorates a JUnit test with a JUnitPerf load test. The load test is then decorated with a JUnitPerf timed test. Ultimately, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access