zoukankan      html  css  js  c++  java
  • 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内完成。
  • 相关阅读:
    Drupal Coder 模块远程命令执行分析(SA-CONTRIB-2016-039)
    Python 实现 ZoomEye API SDK
    程序员互动联盟第一届编码大赛第二题解题分享
    python中各进制之间的转换
    记一次ctf比赛解密题的解决(可逆加密基本破解之暴力破解)
    使用JsonConfig控制JSON lib序列化
    openMRS项目
    Harmonic Number(调和级数+欧拉常数)
    Pairs Forming LCM(素因子分解)
    Uva 11395 Sigma Function (因子和)
  • 原文地址:https://www.cnblogs.com/beanbag/p/12679318.html
Copyright © 2011-2022 走看看