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

时间:2017-03-21 02:27:10

标签: java jmh

在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();

没有产生理想的效果。

1 个答案:

答案 0 :(得分:2)

您必须使用非常旧的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")