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?