Tuesday, October 7, 2008

JUnitPerf timed load JUnit test example

For my own future reference, here is a complete example of a JUnitPerf timed load test of a JUnit test.

First the normal JUnit test case.
In this case, just write a non-trivial sized file...


import junit.framework.TestCase;
import java.io.*;
import java.lang.reflect.Array;

public class AppTest extends TestCase {
public AppTest(String testName) { super(testName); }
public void testApp() {
byte[] ary = (byte[]) Array.newInstance(byte.class, 65534);
try {
File tmp = File.createTempFile("writeTest", ".txt");
tmp.deleteOnExit();
FileOutputStream os = new FileOutputStream(tmp);
FileDescriptor fd = os.getFD();
for(int i=0; i<1000; i++) { os.write(ary); }
os.flush(); fd.sync();
} catch (IOException e) { }
}
}


Then a JUnit-compatible junitperf test suite. Run in sizes of powers of two (and 3 times each)...


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

public class AppTimedLoadTest extends TestCase {

private static final String TEST = "testApp";

public static Test timedNload(int n, int s, String testName) {
boolean wait = true;
Test test = new AppTest(testName);
LoadTest load = new LoadTest(test, n); load.setEnforceTestAtomicity(false);
Test timed = new TimedTest(load, s * 1000, wait);
return timed;
}

public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTest(timedNload(1, 30, TEST));
suite.addTest(timedNload(1, 30, TEST));
suite.addTest(timedNload(1, 30, TEST));
suite.addTest(timedNload(2, 30, TEST));
suite.addTest(timedNload(2, 30, TEST));
suite.addTest(timedNload(2, 30, TEST));
suite.addTest(timedNload(4, 30, TEST));
suite.addTest(timedNload(4, 30, TEST));
suite.addTest(timedNload(4, 30, TEST));
suite.addTest(timedNload(8, 30, TEST));
suite.addTest(timedNload(8, 30, TEST));
suite.addTest(timedNload(8, 30, TEST));
return suite;
}
}


The results of a mvn test run (with added spaces to see the grouping)...


-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.295 sec
Running AppTimedLoadTest
TimedTest (WAITING): LoadTest (NON-ATOMIC): ThreadedTest: testApp(AppTest): 2245 ms
TimedTest (WAITING): LoadTest (NON-ATOMIC): ThreadedTest: testApp(AppTest): 2397 ms
TimedTest (WAITING): LoadTest (NON-ATOMIC): ThreadedTest: testApp(AppTest): 2241 ms

TimedTest (WAITING): LoadTest (NON-ATOMIC): ThreadedTest: testApp(AppTest): 4274 ms
TimedTest (WAITING): LoadTest (NON-ATOMIC): ThreadedTest: testApp(AppTest): 4425 ms
TimedTest (WAITING): LoadTest (NON-ATOMIC): ThreadedTest: testApp(AppTest): 4374 ms

TimedTest (WAITING): LoadTest (NON-ATOMIC): ThreadedTest: testApp(AppTest): 9067 ms
TimedTest (WAITING): LoadTest (NON-ATOMIC): ThreadedTest: testApp(AppTest): 10204 ms
TimedTest (WAITING): LoadTest (NON-ATOMIC): ThreadedTest: testApp(AppTest): 8802 ms

TimedTest (WAITING): LoadTest (NON-ATOMIC): ThreadedTest: testApp(AppTest): 17439 ms
TimedTest (WAITING): LoadTest (NON-ATOMIC): ThreadedTest: testApp(AppTest): 18558 ms
TimedTest (WAITING): LoadTest (NON-ATOMIC): ThreadedTest: testApp(AppTest): 19724 ms
Tests run: 45, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 103.782 sec
Results :
Tests run: 46, Failures: 0, Errors: 0, Skipped: 0

4 comments:

Unknown said...
This comment has been removed by the author.
Unknown said...

byte[] ary = (byte[]) Array.newInstance(byte.class, 65534);

why not just write

byte[] ary = new byte[65534];

Jason Brazile said...

Thanks, Fake. Your suggestion is of course better. Sometimes the old C mental model in my brain shows through.

MrBCut said...

this was a great example! I'm learning of the benefits of JUnitPerf. Thanks :)