Microbenchmarking with Maven

One approach to getting started with JMH is to use the JMH Maven archetype. The first step is to create a new JMH project. At our system's command prompt, we will enter the mvn command followed by a long set of parameters to create a new Java project and the necessary Maven pom.xml file:

mvn archetype:generate -DinteractiveMode=false -DarchetypeGroupId=org.openjdk.jmh -DarchetypeArtifactId=jmh-java-benchmark-archetype -DgroupId=com.packt -DartifactId=chapter8-benchmark -Dversion=1.0

Once you enter the mvn command and the preceding detailed parameters, you will see the results reported to you via the Terminal. Depending on your level of use, you might see a large number of downloads from https://repo.maven.apache.org/maven2/org/apache/maven/plugins/ and other similar repository sites.

You will also see an information section that informs you about the project build process, as shown in the following screenshot:

Maven build process

There will likely be additional plugin and other resources downloaded from the https://repo.maven.apache.org repositories. Then, you will see an informational feedback component that lets you know the project is being generated in batch mode, shown as follows:

Maven project generation

Finally, you will be presented with a set of parameters and a note that your project build was successful. As you can see with the following example, the process took less than 21 seconds to complete:

New Maven project

A folder will be created based on the parameter we included in the -DartifactId option. In our example, we used -DartifactId=chapter8-benchmark, and Maven created a chapter8-benchmark project folder, shown as follows:

Benchmark project folder

You will see that Maven created the pom.xml file as well as a source (src) folder. In that folder, under the subdirectory structure of C:chapter8-benchmarksrcmainjavacompackt, is the MyBenchmark.java file. Maven created a benchmark class for us, shown in the next screenshot:

MyBenchmark.java file location

Here are the contents of the MyBenchmark.java class created by the JMH Maven project creation process:

/*
* Copyright (c) 2014, Oracle America, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
are met:
*
*
* Redistributions of source code must retain the above copyright
notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of Oracle nor the names of its contributors may
be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.packt;
import org.openjdk.jmh.annotations.Benchmark;

public class MyBenchmark {
@Benchmark
public void testMethod() {
// This is a demo/sample template for building your JMH benchmarks.
// Edit as needed.
// Put your benchmark code here.
}
}

Our next step is to modify testMethod() so that there is something to test. Here is the modified method we will use for the benchmark test:

@Benchmark
public void testMethod() {
int total = 0;
for (int i=0; i<100000; i++) {
total = total + (i * 2 );
}
System.out.println("Total: " + total);
}

With our code edited, we will navigate back to the project folder, C:chapter8-benchmark, in our example, and execute mvn clean install at the command prompt.

You will see several repository downloads, source compilations, plugin installations and, finally the Build Success indicator, as shown here:

Build results

You will now see .classpath and .project files as well as a new .settings and target subfolders in the project directory, shown as follows:

Project directory

If you navigate to the arget subfolder, you will see that our benchmarks.jar file was created. This JAR contains what we need to run our benchmarks.

External dependencies in benchmarks.jar are configured in the pom.xml file.

We can update our MyBenchmark.java file in an IDE, such as Eclipse. Then, we can execute mvn clean install again to overwrite our files. After the initial execution, our builds will be much faster, as nothing will need to be downloaded.

Here is a look at the output from the build process after the initial execution:

Clean install process

Our last step is to run the benchmark tool from the C:chapter8-benchmark arget folder. We can do that with the following command -java -jar benchmarks.jar. Even for small benchmarks on simplistic code, as with our example, the benchmarks could take some time to run. There will likely be several iterations including warm-ups to provide a more concise and valid set of benchmark results. 

Our benchmark results are provided here. As you can see, the test ran for 00:08:08 hours:

MyBenchmark.java file location
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset