Is JUnit the right tool to write performance tests?

Freiheit picture Freiheit · Feb 7, 2012 · Viewed 34.9k times · Source

In the last week I've created two classes which my team expressed some concerns about in regards to their performance. To evaluate my code I wrote some simple JUnit tests which exercised these classes by building rich sets of test data, then feeding that data through the relevant methods for thousands of iterations. I recorded the runtime of each iteration then logged out the high, low, and average times by using loops and System.nanoTime(). Finally I had JUnit assert that the high and average times were within acceptable limits. This testing approach gave my team confidence in this code.

Is JUnit the right tool to test performance in this way? Are there better tools to test performance at the unit (method and class) level?

Answer

kostja picture kostja · Feb 7, 2012

There may be better approaches, but there are also some frameworks that help implement benchmarking with JUnit. Some useful practices are warmup runs, statistical evaluations. Take a look at JUnitBenchmarks and JUnitPerf

EDIT looks like JUnitBenchmarks has been deprecated since the question has been stated. The project's maintainers recommend moving on to JMH. Thank you, Barry NL for the heads-up.