AsyncProfiler - 无法在 JMH 基准测试运行程序中加载分析器

AsyncProfiler - Unable to load profiler in JMH benchmark runner

我正在使用 JMH 对 JUnit 测试进行基准测试。我想开始使用 async-profiler 来分析基准并获取有关 CPU 用法的更多信息。

我的基准跑步者:

import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.profile.AsyncProfiler;
import org.openjdk.jmh.results.format.ResultFormatType;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.TimeValue;

import java.util.concurrent.TimeUnit;

public class BenchmarkRunner {
    public static void main(String[] args) throws Exception {
        Options opt = new OptionsBuilder()
                .include(Benchmarks.class.getSimpleName())
                .addProfiler(AsyncProfiler.class, "output=flamegraph")
                .mode(Mode.Throughput)
                .resultFormat(ResultFormatType.CSV)
                .result("target/test-classes/benchmarkcsv/BM " + System.currentTimeMillis() + ".csv")
                .timeUnit(TimeUnit.MILLISECONDS)
                .warmupIterations(3)
                .warmupTime(TimeValue.seconds(1))
                .measurementIterations(3)
                .measurementTime(TimeValue.seconds(1))
                .timeout(TimeValue.seconds(5))
                .forks(1)
                .warmupForks(1)
                .threads(1)
                .build();

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

在 运行 时,出现以下错误:

Exception in thread "main" org.openjdk.jmh.runner.ProfilersFailedException: Profilers failed to initialize, exiting.
    at org.openjdk.jmh.runner.Runner.internalRun(Runner.java:228)
    at org.openjdk.jmh.runner.Runner.run(Runner.java:209)
    at DummyApp.BenchmarkRunner.main(BenchmarkRunner.java:33)
Caused by: org.openjdk.jmh.profile.ProfilerException: Unable to load async-profiler. Ensure asyncProfiler library is on LD_LIBRARY_PATH (Linux), DYLD_LIBRARY_PATH (Mac OS), or -Djava.library.path. Alternatively, point to explicit library location with -prof async:libPath=<path>.
    at org.openjdk.jmh.profile.AsyncProfiler.<init>(AsyncProfiler.java:237)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.openjdk.jmh.profile.ProfilerFactory.instantiate(ProfilerFactory.java:82)
    at org.openjdk.jmh.profile.ProfilerFactory.getProfiler(ProfilerFactory.java:77)
    at org.openjdk.jmh.profile.ProfilerFactory.getProfilerOrException(ProfilerFactory.java:37)
    at org.openjdk.jmh.runner.Runner.internalRun(Runner.java:225)
    ... 2 more
Caused by: java.lang.UnsatisfiedLinkError: no asyncProfiler in java.library.path
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1867)
    at java.lang.Runtime.loadLibrary0(Runtime.java:870)
    at java.lang.System.loadLibrary(System.java:1122)
    at org.openjdk.jmh.profile.AsyncProfiler$JavaApi.<init>(AsyncProfiler.java:584)
    at org.openjdk.jmh.profile.AsyncProfiler$JavaApi.getInstance(AsyncProfiler.java:573)
    at org.openjdk.jmh.profile.AsyncProfiler.<init>(AsyncProfiler.java:234)
    ... 10 more

Process finished with exit code 1

根据 async-profiler Github 页面,async-profiler 与 IntelliJ IDEA Ultimate 2018.3 及更高版本捆绑在一起。我的系统上 运行 IntelliJ IDEA Community Edition 2021.3.2,但找不到库。

我如何找到这个 library/solve 我遇到的这个错误?

异常消息准确说明了发生的情况以及解决方法:

Unable to load async-profiler. Ensure asyncProfiler library is on
LD_LIBRARY_PATH (Linux), DYLD_LIBRARY_PATH (Mac OS), or -Djava.library.path.
Alternatively, point to explicit library location with -prof async:libPath=<path>.

从中获取 async-profiler https://github.com/jvm-profiling-tools/async-profiler/releases,并在 LD_LIBRARY_PATH 环境变量或 -Djava.library.path JVM 选项中将路径设置为 libasyncProfiler.so

在 Windows 上,您可以选择另一个 JMH 分析器:-prof jfraddProfiler(JavaFlightRecorderProfiler.class)。这将生成 .jfr 记录,您可以直接在 JDK Mission Control or convert to a Flame Graph using converter.jar 中从 async-profiler 打开。