BlackHole to be a star

JMH provides a particular class—org.openjdk.jmh.infra.Blackhole—that can seem weird as it mainly only allows you to consume an instance. It is retrieved by injecting it into the parameters of the benchmark:

public void compute(final QuoteState quoteState, final Blackhole blackhole) {    blackhole.consume(quoteState.service.findByName("test"));}

Why consume the returned value of a method if we do nothing about it? Remember that the Oracle JVM has what is called the just in time compilation (JIT) which optimizes the code at runtime depending on the statistics of the code paths. If you don't call that consume method, you can end up not measuring the actual code you want, but some very optimized flavors of this code, since, ...

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.