Skip to content

Getting Started

Reiss Cashmore edited this page Sep 20, 2018 · 7 revisions

Getting Started

Using JMH is easy!

1. Install Java JDK on your platform (Version 8+)

Using the terminal to install the JDK globally

Mac OSX with Homebrew*
brew cask install java

Linux
sudo apt-get install openjdk-8-jdk

OR

Download the Tar files and install manually

For Linux, Mac OSX and Windows
http://www.oracle.com/technetwork/java/javase/downloads/index.html


Validate you have installed the JDK correctly by running the following in your terminal: javac -version

2. Installing maven

Using the terminal to install maven globally

Mac OSX with Homebrew*
brew install maven

Linux
sudo apt-get install maven

Local install using the maven package

Mac OSX, Linux & Windows

You can just download and unpack maven into your project folder, either run the mvn from within the /bin/ directory of the package or add a PATH variable/alias so that you can use the mvn command easily
Maven Package Download: http://maven.apache.org/download.cgi
Install Instructions: https://maven.apache.org/install.html

3. Compile benchmarks

Run:
mvn clean install

This will generate a benchmarks.jar in: /target

  1. Run the benchmarks
    Run the following command java -jar target/benchmarks.jar Enable JSON output by adding the --json flag to your command or run:
    java -jar benchmarks.jar -rf json

You can export your benchmark to other formats, to see them run:
java -jar benchmarks.jar -lrf

  1. Run benchmarks

Writing your benchmark

Here is a simple example

package org.benchmarking;

import java.util.concurrent.TimeUnit;
import java.util.*;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;

@BenchmarkMode(Mode.AverageTime)
@Warmup(iterations = 2, time = 750, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 3, time = 700, timeUnit = TimeUnit.MILLISECONDS)
public class SimpleBenchmark {


    // To pass parameters into your test functions you must define a class decorated with @State
    @State(Scope.Benchmark)
    public static class BenchmarkState {

        @Param({ "1", "2", "3", "4", "5"})
        int iterations;

        public void setUp() {
            // You can use this function to prepare/preprocess any variables that need to be maniuplated before they are passed to your test function
        }
    }

    @Benchmark
    public static double functionToBeBenchmarked(BenchmarkState _benchmarkState){

        double value = 4;
        int iterations = _benchmarkState.iterations;
        for (int i = 0; i < iterations; i++) {
            value = Math.pow(value, i);
        }
        return value;
    }

}

Save your file:

src/main/java/org/benchmarking/SimpleBenchmark.java

Benchmarking decorations documentation

For more great resources check out the Wiki: http://hg.openjdk.java.net/code-tools/jmh/file/tip/jmh-samples/src/main/java/org/openjdk/jmh/samples/ http://java-performance.info/jmh/ http://java-performance.info/introduction-jmh-profilers/

Clone this wiki locally