JMH-低级基准测试
我正在使用JMH来基准JUNIT测试。
我的基准:
import org.openjdk.jmh.annotations.*;
public class Benchmarks {
@Benchmark
public void bmUnitTest1() {
UnitTests.UnitTest1();
}
@Benchmark
public void bmUnitTest2() {
UnitTests.UnitTest2();
}
}
我的基准跑者:
import org.openjdk.jmh.annotations.Mode;
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())
.mode(Mode.SingleShotTime)
.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();
}
}
目前,我在整个单元测试中获得了性能指标。我想知道是否有可能在较低级别上看到这些性能指标。
示例:
UnitTest1具有多个函数调用。功能1所需10ms,功能2需要20m。
而不是看到这个(简化):
UnitTest1: 30ms/op
我想看看:
UnitTest1: 30ms/op
Function1: 10ms/op
Function2: 20ms/op
我可以在JMH中做到这一点吗?如果没有,我可以使用任何替代方法来基准Junit测试吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
JMH有几个探查器,可以在运行基准测试时启用。参见。
其中之一是
async
基于项目。它可以完全做您想要的事情 - 显示单元测试调用的方法花费大多数CPU时间(带有堆栈跟踪!)才能启用此探测器,请调用
options builder.addprofiler(asyncprofiler.class.class,“ output = flameGraph”) /code>,或(如果从命令行运行)add
-prof async:output = flamegraph
option。基准测试完成后,JMH将以.html格式生成一个探测器报告:
输出
选项:JMH has several profilers that can be enabled while running benchmarks. See JMHSample_35_Profilers for details.
One of them is
async
profiler based on async-profiler project. It can do exactly what you want - show which methods called by your unit test take most CPU time (with stack traces!)To enable this profiler, call
OptionsBuilder.addProfiler(AsyncProfiler.class, "output=flamegraph")
, or (if running from a command line) add-prof async:output=flamegraph
option.When benchmarking completes, JMH will generate a profiler report in .html format:
If you want to get a flat profile - just methods with the percentage of CPU time spent - use
async
profiler withoutoutput
option: