How do I prevent JMH from initializing non-JMH-related classes on every benchmark?

Aleksey Shipilev shade at redhat.com
Fri Dec 3 09:50:37 UTC 2021


On 12/2/21 1:42 PM, Timmae219 wrote:
> I am currently working on a very special project which uses also JMH. The program should monitor
> the energy consumption of the System with the Intel Power Gadget during a JMH Benchmark. During
> analyzing some other concurrency problems I found out that my PowerGadgetProfiler class, which
> has only static members, has been initialized every time a new benchmark has begun. But I want it
> to be initialized just once in the whole program (at the beginning).

The default mode is to fork a JVM for every @Benchmark trial. So whatever that @Benchmark used would 
be initialized several times (for each JVM).

There is no annotation-driven way to do @Setup/@TearDown for the entire benchmarks.jar once. The way 
it could be implemented is using Java API (see just about any sample in JMH Samples), set up 
everything in Java, call JMH Runner to execute the benchmark, shutdown and process everything in Java.

-- 
Thanks,
-Aleksey



More information about the jmh-dev mailing list