如何使用JMH测量平均冷启动时间? [英] How to measure average cold start time with JMH?

查看:58
本文介绍了如何使用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):

单次基准测试原本打算运行一次多分支上的测量迭代-估计的方案冷"是指表现.但在许多情况下,您可能需要更多测量在那里迭代,尤其是如果您只运行一个fork,因为会生成更多样本.

Single-shot benchmarks were originally destined to run a single measurement iteration over multiple forks -- the scenarios to estimate "cold" performance. But for many cases, you might want more measurement iterations there especially if you are running only a single fork, because more samples would be generated.

@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测量平均冷启动时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆