Chapter 15. Performance Testing with JUnitPerf

Introducing JUnitPerf

JUnitPerf is an extension of JUnit 3 (see JUnit 3.8 and JUnit 4) that adds the ability to time test cases and to do simple load and performance testing. Its features are similar to some of the TestNG annotations (see Parallel Testing), but JUnitPerf uses a very different approach, integrating smoothly into the JUnit 3 architecture.

Using an elegant system of JUnit decorators, JUnitPerf lets you use existing unit tests to build simple but effective load and performance tests. First, you write your usual unit test cases to verify that the code is executing correctly and is behaving as expected. Then you can encapsulate some of your key unit tests using JUnitPerf decorators, effectively creating a suite of performance tests without having to modify your original unit tests. This also makes it easy to separate your performance tests from your ordinary unit tests, because you usually don’t run them at the same time.

Incorporating some basic performance testing into your unit tests makes good sense. Without going overboard, it is a good way to detect major performance anomalies early on in the piece. You can also configure these tests to run separately from your ordinary unit tests, in order to avoid penalizing the fast feedback cycle that is one of the trademarks of good unit tests.

Note that we are talking about verifying performance, not optimizing code in an uncontrolled manner. Premature optimization has often been ...

Get Java Power Tools 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.