如何使用 JMH 计算 CPU 时间和内存量?例如,我有: 代码:
@State(Scope.Thread)
@BenchmarkMode(Mode.All)
public class JMHSample_My {
int x = 1;
int y = 2;
@GenerateMicroBenchmark
public int measureAdd() {
return (x + y);
}
@GenerateMicroBenchmark
public int measureMul() {
return (x * y);
}
public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(".*" + JMHSample_My.class.getSimpleName() + ".*")
.warmupIterations(5)
.measurementIterations(5)
.forks(1)
.build();
new Runner(opt).run();
}
}
结果:
Benchmark Mode Samples Mean Mean error Units
JMHSample_My.measureAdd thrpt 5 1060579.757 39506.950 ops/ms
JMHSample_My.measureMul thrpt 5 1046872.684 79805.116 ops/ms
JMHSample_My.measureAdd avgt 5 0.000 0.000 ms/op
JMHSample_My.measureMul avgt 5 0.000 0.000 ms/op
JMHSample_My.measureAdd sample 9549793 0.000 0.000 ms/op
JMHSample_My.measureMul sample 9287002 0.000 0.000 ms/op
JMHSample_My.measureAdd ss 5 0.001 0.000 ms
JMHSample_My.measureMul ss 5 0.001 0.000 ms
我可以看到请求时间的数量,测试的平均时间,但看不到平均CPU使用量和内存使用量。这可以通过JMH来完成吗?