JaVa
   

LoadTest Example

Like the TimedTest, a LoadTest is a test decorator. The LoadTest runs a test with a number of simulated concurrent users and iterations. When you create a LoadTest class instance, you pass it a number of concurrent users. Each user runs the test once, unless you set the iteration higher when you construct a LoadTest. First, to see a simple usage of the LoadTest, let’s take our testVectorAlot and this time load-test with five threads. Because we are running this test on a machine with one processor, we expect the time to go up by a factor of five or so. Here is the code to run the LoadTest:

int users = 5;
Test loadTest = new LoadTest(
 new SimpleTest('testVectorAlot'), users);
TestRunner.run(loadTest); 


Notice that the LoadTest constructor is a lot like the TimeTest constructor in the last section when we wrapped the SimpleTest in the TimedTest. Now, instead of using the TimedTest and passing the max time out, we are using the LoadTest class and passing the number of user threads to simulate. Here is the output from this test:

.....
Time: 27.75
OK (5 tests)


The JUnit framework uses the decorator design pattern to add additional functionality to tests dynamically. Thus, you can nest a test inside a decorator test that adds additional functionality but still has the same public interface as the nested test. You can nest the test to decorate the nested test. If we wanted a test where each user ran 5 tests, the test had to complete in 150 seconds, and the test failed as soon as 150 seconds passed, we could create it as follows. We first import the RepeatedTest from JUnit (we could have used this approach instead of the forXXXAlot tests earlier):

import junit.extensions.RepeatedTest;


Next, we create the simpleTest to run the testVector as follows:

 Test simpleTest = new SimpleTest('testVector');


We create the repeated test to execute 2,000 iterations:

 int iterations = 2000;
 Test repeatedTest = new RepeatedTest(simpleTest, iterations);


Notice that the simpleTest is passed to the repeated test constructor, along with the number of iterations that we want the test run. We decorated the simple test with a repeated test. Next, we decorate the repeated test with a load test:

 int users = 5;
 Test loadTest = new LoadTest(repeatedTest, users);


Finally, we decorate the loadTest with the timedTest and run the timedTest as follows:

 long maxElapsedTime = 150 * 1000;
 Test timedTest = new TimedTest(loadTest, maxElapsedTime);
 TestRunner.run(timedTest); 


In this section, we covered how to decorate test with different tests. As we can see, you can nest these tests pretty deep.


JaVa
   
Comments