Creating your first JMH benchmark

After all that theory, here is a basic benchmark developed with JMH:

public class QuoteMicrobenchmark {    @Benchmark    public void compute() {        //    }}

In this case, we have a single benchmark scenario called compute. It doesn't use any states and didn't customize any thread or fork count.

In practice, this will not be enough and you will often need a state to get a service. So, it will more look like this:

public class QuoteMicrobenchmark {    @Benchmark    public void compute(final QuoteState quoteState) {        quoteState.service.findByName("test");    }    @State(Scope.Benchmark)    public static class QuoteState {        private QuoteService service;        @Setup        public void setup() {            service = new QuoteService();        }    }}

Here, we created a nested ...

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.