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内完成。
  • 相关阅读:
    我不写博客的原因就是cnblogs不好用
    使用KeePass愉快的来管理你的密码
    Visual studio常用的code snippets
    日常工作小贴士
    win10下面visual studio, sublime ctrl+shift+f快捷键失效的原因
    Markdown常用语法对应
    常见的Message Queue应用场景
    二分查找(binary search)
    在ROS中使用花生壳的域名服务
    排序算法 选择排序(selection sort)
  • 原文地址:https://www.cnblogs.com/beanbag/p/12679318.html
Copyright © 2011-2022 走看看