Integrating JMH with JUnit

There is no official JUnit integration of JMH. In spite of that, it is not that hard to do it yourself. There are lots of possible designs, but in the context of this book we will do the following:

  • Our integration will go through a JUnit runner
  • The benchmark classes will be identified by extracting the nested classes of the test class which will avoid using any scanning to find the benchmark classes or explicit listing
  • We will introduce an @ExpectedPerformances annotation to be able to add assertions based on the execution

Structurally, a microbenchmark test using this structure will look like this:

@RunWith(JMHRunner.class)public class QuoteMicrobenchmarkTest {    @ExpectedPerformances(score = 2668993660.)    public ...

Get Java EE 8 High Performance 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.