Isolating setup of StackWalker test call stack in JMH?

Sergey Kuksenko sergey.kuksenko at oracle.com
Sat Aug 26 03:58:23 UTC 2023


You may write a simple baseline benchmark, just constructing call stack.
Something like this:
@Benchmark
    public void forEach_makeCallStack(Blackhole bh) {
        final Blackhole localBH = bh;
        final boolean[] done = {false};
        new TestStack(depth, new Runnable() {
            public void run() {
                done[0] = true;
            }
        }).start();
        if (!done[0]) {
            throw new RuntimeException();
        }
    }

And check the difference between this baseline and the corresponding "StackWalker.getCallerClass()".

________________________________________
From: Brent Christian <brent.christian at oracle.com>
Sent: Friday, August 25, 2023 4:43 PM
To: Sergey Kuksenko; jmh-dev at openjdk.org; jmh-jdk-microbenchmarks-dev at openjdk.org
Subject: Re: Isolating setup of StackWalker test call stack in JMH?

Thanks for the response, Sergey.

On 8/15/23 9:52 PM, Sergey Kuksenko wrote:
 > Invoking nanoTime twice per operation, you will get large nanoTime overhead.
 > https://shipilev.net/blog/2014/nanotrusting-nanotime/

That's very interesting.

 > May I ask you - why do you need to move stack construction into "setup"?

I originally wanted StackWalkBench.getCallerClass() to be able to confirm that
StackWalker.getCallerClass() performs the same, regardless of call stack depth. The benchmark first
makes method calls to create a call stack depth of `depth` (which I don't want to be part of the
measurement) before calling getCallerClass().

At this point, StackWalker is mature enough that we're confident that getCallerClass()
behaves/performs as expected regardless of call stack depth. So in PR 15370[1], the benchmark will
simplify the benchmark to just call getCallerClass(), without adding to the call stack.

Thanks,
-Brent

1. https://github.com/openjdk/jdk/pull/15370


More information about the jmh-jdk-microbenchmarks-dev mailing list