Writing a benchmark with JMH

Before being able to write code with JMH, you need to add it as a dependency of your project. We will use Maven syntax in the following example, but it has equivalents for Gradle, and so on.

The first thing to do is to add into your pom.xml the following dependencies; we will use the scope test since the dependencies are only needed for our performance tests and not the "main" code:

<dependencies>  <!-- your other dependencies -->  <dependency>    <groupId>org.openjdk.jmh</groupId>    <artifactId>jmh-core</artifactId>    <version>${jmh.version}</version>    <scope>test</scope>  </dependency>  <dependency>    <groupId>org.openjdk.jmh</groupId>    <artifactId>jmh-generator-annprocess</artifactId>    <version>${jmh.version}</version>    <

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.