Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to run a JMH benchmark under an external profiler?

I'm currently performance hunting. In order to measure the throughput, and to enforce that we don't regress, I'm using the wonderful JMH.

When I come across something that is slow though, I want to start profiling to see what is going on, according to this link the author of JMH writes:

While JMH profilers can provide the aid in analyzing, I don't think they are the substitute for proper profiling. E.g. the "stack" profiler is good to glance over the profiles, but not for the serious work.

Run the workload for longer, and attach your profiler of choice to the running VM.

I was secretly hoping I could tweak the JMH test from the command line, then attach something like visualvm to that... but so far I've been able to get that to work. I guess that is a bad idea? It would be ideal if I could share the code I use to profile, as well as the code I use to enforce.

like image 817
Alun Avatar asked Apr 23 '16 05:04

Alun


People also ask

How do you run JMH benchmarks?

There are two ways to run the JMH benchmark, uses Maven or run it via a JMH Runner class directly. 3.1 Maven, package it as a JAR and run it via org. openjdk. jmh.

What is JMH used for?

JMH (Java Microbenchmark Harness) is a library for writing benchmarks on the JVM, and it was developed as part of the OpenJDK project. It is a Java harness for building, running, and analyzing benchmarks of performance and throughput in many units, written in Java and other languages targeting the JVM.


1 Answers

Yes it is possible, it is simple to do, and I recommend it as a best practice to do it all the time! It is always good to have data available to explain what is going on without the need to spend extra time to reproduce the issue.

I wrote my own JMH profiler to use JFR (Java Flight Recorder): JmhFlightRecorderProfiler which will start JMH profiling and save the data to a file when your benchmark ends. You can open this file with Java Mission control shipped with the JDK.

Here is how you can start your benchmarks and use it (complete example):

Options opt = new OptionsBuilder()
        .include(".*")
        .addProfiler(JmhFlightRecorderProfiler.class)
        .jvmArgs("-Xmx256m", "-Xms256m", "-XX:+UnlockCommercialFeatures",
                "-Djmh.stack.profiles=" + destinationFolder,
                "-Djmh.executor=FJP",
                "-Djmh.fr.options=defaultrecording=true,settings=" + profile)
        .result(destinationFolder + "/" + "benchmarkResults.csv")
        .resultFormat(ResultFormatType.CSV)
        .warmupIterations(10)
        .measurementIterations(10)
        .forks(1)
        .build();
 new Runner(opt).run();

I run benchmarks during the standard build, I use Jenkins JMH plugin to monitor/trend benchmark result/build and if any degradation is observed I have the Benchmark profiles available to investigate the potential cause.

like image 162
user2179737 Avatar answered Sep 21 '22 17:09

user2179737