如何使用JMH测量平均冷启动时间

如何使用JMH测量平均冷启动时间

本文介绍了如何使用JMH测量平均冷启动时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JMH(Java Microbenchmark Harness)中,我们可以使用

In JMH(Java Microbenchmark Harness), we can use

@BenchmarkMode(Mode.AverageTime)
@Warmup(iterations = 10)
@Measurement(iterations = 10)

评估JVM预热后平均执行时间.

to evaluate the average time of an execution after JVM warms up.

我们也可以使用

@BenchmarkMode(Mode.SingleShotTime)
@Measurement(iterations = 1)

估计执行的冷启动时间.但这只会执行一次基准测试,这可能会引入偏差.那么,有什么方法可以评估JMH中冷启动的平均时间吗?

to estimate the cold start time of an execution. But this only executes the benchmark once, which may introduce bias. So is there any method to evaluate the average time of the cold start in JMH?

推荐答案

根据 Alexey本人(尽管从2014年开始):

According to Alexey himself (though from 2014):

@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public class AverageSingleShot {

    public static void main(String[] args) throws Exception {
        Options opt = new OptionsBuilder()
            .include(AverageSingleShot.class.getSimpleName())
            .build();

        new Runner(opt).run();
    }

    @Fork(100)
    @Benchmark
    @BenchmarkMode(Mode.SingleShotTime)
    public int test() {
        return ThreadLocalRandom.current().nextInt() + ThreadLocalRandom.current().nextInt();
    }

}

除了可以告诉您平均值(请参见 100 )之外,您还可以这样做:

Besides the fact that this will tell you the average (see that 100):

 Benchmark               Mode  Cnt      Score      Error  Units
 AverageSingleShot.test    ss  100  41173.540 ± 2871.546  ns/op

您还将获得百分位数直方图.

这篇关于如何使用JMH测量平均冷启动时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 19:21