Reputation: 33083
I am using JMH to benchmark some code on the JVM, and I have written a method annotated as a @Setup
method which takes quite a long time to run. Of course this method is not included in the benchmarks, as expected - but unfortunately, when I try to use JMH's built-in profiling support, it is included, which pollutes the profile report with all kinds of irrelevant method calls which aren't called during the actual benchmark method itself. This behaviour doesn't seem to be documented anywhere, but in the JMH source code, there is the following comment:
// profilers start way before the workload starts to capture
// the edge behaviors.
There doesn't seem to be any option to disable this behaviour in JMH. How can I exclude the @Setup
method(s), and any stack traces that originate from them, from being included in profiler results? (The @Setup
methods are not called, directly or indirectly, from the @Benchmark
methods.)
Upvotes: 0
Views: 667
Reputation: 33083
A relatively obvious way to do this is to avoid using JMH and just use a profiler on its own. This requires a bit of setup, however. In Java, you can add a public static void main
method, or in Scala 2.12, you can add a companion object
which extends App
, to create a utility application, which should contain the following code:
Benchmark b = new Benchmark(); // assuming your benchmark is defined in a class called Benchmark
b.setupMethod();
System.out.println("Attach a profiler and then press Enter to continue");
new Scanner(System.in).nextLine();
b.benchmarkedMethod();
If you are using a sampling profiler and your benchmarkedMethod
is quite fast, put it in a for
loop with many iterations, so that the program runs for long enough to enable the sampling profiler to collect enough samples for a useful report. But if you are using a traditional profiler which tries to record all method calls, and your benchmarked method is functionally deterministic (it does the same thing, modulo different memory addresses and low-level things like that, every time), don't do that, because it is totally unnecessary for profiling.
Then run the utility program, attach the profiler when instructed, and press Enter.
Upvotes: 0