代码之家  ›  专栏  ›  技术社区  ›  Dimidrie

AsyncProfiler-无法在JMH benchmark runner中加载探查器

  •  0
  • Dimidrie  · 技术社区  · 2 年前

    我正在使用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,但找不到库。

    如何找到此库/解决我遇到的此错误?

    0 回复  |  直到 2 年前
        1
  •  0
  •   apangin    2 年前

    异常消息准确地告诉您发生了什么以及如何解决它:

    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>.
    

    从获取异步探查器 https://github.com/jvm-profiling-tools/async-profiler/releases ,并将路径设置为 libasyncProfiler.so 在里面 LD_LIBRARY_PATH 环境变量或in -Djava.library.path JVM选项。

    在Windows上,您可以选择另一个JMH探查器: -prof jfr addProfiler(JavaFlightRecorderProfiler.class) 。这将产生 .jfr 可以在中直接打开的录制 JDK Mission Control 或使用转换为火焰图 converter.jar 来自异步探查器。