zondag 4 januari 2015

JMH and printing Assembly

In preparation of a blogpost I'm working on, I need to access the assembly generated from a JMH benchmark.

A few resources are available how to print the assembly for a normal application, e.g.
But in case of JMH it is a bit more tricky since JMH forks a new JVM and the arguments need to be applied to that JVM; not to the JVM of JMH itself.

Below is a copy/paste nonsense example of how to do configure JMH to print the assembly of the methods you are interested in:
package org.sample;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.Random;

@State(Scope.Thread)
public class SomeBenchmark {
    public static final int ARRAY_SIZE = 1000 * 1000 * 100;
    private int[] array;

    @Setup
    public void setup() {
        array = new int[ARRAY_SIZE];
        Random random = new Random();
        for (int k = 0; k < array.length; k++) {
            array[k] = random.nextInt();
        }
    }

    @Benchmark
    public int benchmarkFoo() {
        int[] array = this.array;
        int sum = 0;
        int length = array.length;
        for (int k = 0; k < length; k++) {
            sum += array[k];
        }
        return sum;
    }

    public static void main(String... args) throws Exception {
        Options opts = new OptionsBuilder()
                .include(".*")
                .warmupIterations(2)
                .operationsPerInvocation(ARRAY_SIZE)
                .measurementIterations(5)
                .jvmArgs("-server",
                        "-XX:+UnlockDiagnosticVMOptions",
                        "-XX:PrintAssemblyOptions=intel",
                        "-XX:CompileCommand=print,*SomeBenchmark.benchmark*")
                .forks(1)
                .build();

        new Runner(opts).run();
    }
}

I'm only interested in the benchmarkFoo method, so only the assembly generated for that particular method is printed. I'm using a benchmark* wildcard, so I can easily add other benchmark methods and have their assembly printed.

Geen opmerkingen:

Een reactie posten

Will temporary assignment to local variable impact performance?

This post is written as a consequence of the following stack overflow question . The question is if temporary assigning the results of some ...