JMH - 以编程方式为 "stack" 分析器设置 "lines" 参数

JMH - Setting "lines" parameter for "stack" profiler programmatically

在JMH中StackProfiler.class接受几个参数:"lines"、"top"、"detailLine"等

在命令行上可以这样定义参数值:

java -jar my-benchmarks.jar -prof stack -jvmArgsAppend -Djmh.stack.lines=3

看似显而易见

new OptionsBuilder().addProfiler("stack").jvmArgsAppend("-Djmh.stack.lines=3")

@Fork(jvmArgsAppend="-Djmh.stack.lines=3")

System.setProperty("jmh.stack.lines", "3");
...
new Runner(opt).run();

没有达到预期的效果。

您一定是在使用非常古老的 JMH,因为堆栈分析器已经接受选项:

$ java -jar jmh-samples/target/benchmarks.jar -prof stack:help
Usage: -prof <profiler-name>:opt1=value1,value2;opt2=value3

Options accepted by org.openjdk.jmh.profile.StackProfiler:

[...]

  lines=<int>  Number of stack lines to save in each stack trace. 
               Larger values provide more insight into who is 
               calling the top stack method, as the expense of more
               stack trace shapes to collect. (default: [1]) 

[...]

无法从注释中访问它,但是 Java API 接受探查器选项字符串:

/**
 * Add the profiler in the run
 * @param profiler profiler class
 * @param initLine profiler options initialization line
 * @return builder
 */
ChainedOptionsBuilder addProfiler(Class<? extends Profiler> profiler,
                                  String initLine);

/**
 * Add the profiler in the run
 * @param profiler profiler class name, or profiler alias
 * @param initLine profiler options initialization line
 * @return builder
 */
ChainedOptionsBuilder addProfiler(String profiler, String initLine);

所以,像这样的东西应该可以工作:

.addProfiler("stack", "lines=3")