JMH之程序常用的采样方式

时间:2020-04-11
本文章向大家介绍JMH之程序常用的采样方式,主要包括JMH之程序常用的采样方式使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

一、吞吐量测量方式

/**
 * 程序吞吐量测量方式
 */
public class Test_JMH02 {

    @Benchmark
    @BenchmarkMode(Mode.Throughput)
    @OutputTimeUnit(TimeUnit.SECONDS)
    public void measureThroughput() throws InterruptedException {
        //程序在此处进行休眠10秒
        Thread.sleep(10000);

    }

    public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder().include(Test_JMH02.class.getSimpleName())
                .forks(1).build();
        new Runner(opt).run();
    }
}

测试结果

Benchmark                      Mode  Cnt  Score    Error  Units
Test_JMH02.measureThroughput  thrpt    5  0.100 ±  0.001  ops/s
表示每秒可以进行0.1个操作

二、测试方法的平均执行时间

/**
 * 统计方法的平均执行时间
 */
public class Test_JMH03 {

    @Benchmark
    @BenchmarkMode(Mode.AverageTime)
    @OutputTimeUnit(TimeUnit.MICROSECONDS)
    public void measureAverageTime() throws InterruptedException {
        Thread.sleep(100);

    }

    public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder().include(Test_JMH03.class.getSimpleName())
                .forks(1).build();
        new Runner(opt).run();
    }
}

测试结果

Benchmark                     Mode  Cnt       Score     Error  Units
Test_JMH03.measureAverageTime  avgt    5  100494.779 ± 240.881  us/op
每次执行大约需要100毫秒

三、采样

/**
 * 采样,得到方法的部分执行时间
 */
public class Test_JMH04 {

    @Benchmark
    @BenchmarkMode(Mode.SampleTime)
    @OutputTimeUnit(TimeUnit.MICROSECONDS)
    public void measureSampleTime() throws InterruptedException {
        Thread.sleep(1000);

    }

    public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder().include(Test_JMH04.class.getSimpleName())
                .forks(1).build();
        new Runner(opt).run();

        
    }
}

测试结果

Benchmark                                                 Mode  Cnt        Score     Error  Units
Test_JMH04.measureSampleTime                            sample   50   999670.415 ± 272.664  us/op
Test_JMH04.measureSampleTime:measureSampleTime·p0.00    sample        998244.352            us/op
Test_JMH04.measureSampleTime:measureSampleTime·p0.50    sample        999292.928            us/op
Test_JMH04.measureSampleTime:measureSampleTime·p0.90    sample       1000341.504            us/op
Test_JMH04.measureSampleTime:measureSampleTime·p0.95    sample       1000341.504            us/op
Test_JMH04.measureSampleTime:measureSampleTime·p0.99    sample       1000341.504            us/op
Test_JMH04.measureSampleTime:measureSampleTime·p0.999   sample       1000341.504            us/op
Test_JMH04.measureSampleTime:measureSampleTime·p0.9999  sample       1000341.504            us/op
Test_JMH04.measureSampleTime:measureSampleTime·p1.00    sample       1000341.504            us/op
结果表明,平均执行时间是999670 us,有一半的调用在999292 us内完成,90%的调用在1000341 us内完成,全部的采样在1000341 us内完成。

原文地址:https://www.cnblogs.com/beanbag/p/12679318.html