8.2 When to Use JUnitPerf

8.2.1 Problem

You want to track down potential performance and scalability problems but are unsure of the tools you need.

8.2.2 Solution

Use a commercial profiling tool, such as JProbe or OptimizeIt, to manually inspect code and identify application bottlenecks. Use JUnitPerf to ensure that new features and refactoring do not slow down code that used to be fast enough.

8.2.3 Discussion

JUnitPerf is a tool for continuous performance testing. The goal of a performance test is to ensure that the code executes fast enough, even under varying load conditions. Let's take a look at a typical scenario.

You just completed a custom search algorithm, complete with a suite of JUnit tests. Next, the code is run through a profiling tool to look for any potential bottlenecks. If any performance issues are found, a new JUnit test is written to isolate the code (if one does not already exist). For example, the profiling tool reports that the search took ten seconds, but requirements dictate that it execute in less than three. The new JUnit test is wrapped around a JUnitPerf TimedTest to expose the performance bug. The timed test should fail; otherwise, there is no performance issue with the code you have isolated. Next, refactor the code that is causing the performance problem until the timed test passes.

If the profiling tool did not report any performance issues, you do not have to write JUnitPerf tests. If you are concerned that a new feature might slow down an important piece of code, consider adding a JUnitPerf test to ensure that the code is always fast enough.

Here are the typical steps for writing a JUnitPerf test:

  1. Write a JUnit test suite for you search algorithm.

  2. Run the search algorithm through a profiling tool to find bottlenecks.

    Having a test that runs just the algorithm and exercises it with a reasonable set of test data makes it easy to gather repeatable metrics. Instead of manually clicking through an application, you can run your test through the profiler.

  3. If performance is an issue, write another JUnit test to isolate the code with poor performance (if one does not already exist).

  4. Write a JUnitPerf TimedTest for the new JUnit test. The test should fail. If the test passes, there is no performance issue with the code you have isolated.

  5. Tune the code until the performance test passes.

8.2.4 See Also

Recipe 8.3 shows how to create a JUnitPerf TimedTest. Recipe 8.7 shows how to use Ant to execute JUnitPerf tests.