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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.