美文网首页
java性能测试jmh

java性能测试jmh

作者: 水煮鱼又失败了 | 来源:发表于2020-06-29 16:30 被阅读0次

    1 简介

    jmh主要用于开发人员进行性能压测,代替手动编写相关程序。

    官网:

    http://openjdk.java.net/projects/code-tools/jmh/

    JMH是一个Java工具,用于构建、运行和分析用Java和其他语言编写的针对JVM的nano/micro/milli/宏基准测试。

    2 使用

    2.1 maven依赖

    <dependency>
        <groupId>org.openjdk.jmh</groupId>
        <artifactId>jmh-core</artifactId>
        <version>1.23</version>
    </dependency>
    <dependency>
        <groupId>org.openjdk.jmh</groupId>
        <artifactId>jmh-generator-annprocess</artifactId>
        <version>1.23</version>
        <scope>test</scope>
    </dependency>
    

    2.2 实例代码

    import org.openjdk.jmh.runner.Runner;
    import org.openjdk.jmh.runner.options.Options;
    import org.openjdk.jmh.runner.options.OptionsBuilder;
    
    import java.util.concurrent.TimeUnit;
    //使用模式,默认是Mode.Throughput,表示吞吐量(AverageTime,表示每次执行时间,SampleTime表示采样时间,SingleShotTime表示只运行一次,用于测试冷启动消耗时间,All表示统计前面的所有指标)
    @BenchmarkMode(Mode.Throughput)
    //预热次数(预热2轮,每次1s)
    @Warmup(iterations = 2, time = 1, timeUnit = TimeUnit.SECONDS)
    //执行次数(运行3次,一次运行2秒)
    @Measurement(iterations = 3, time = 2, timeUnit = TimeUnit.SECONDS)
    //配置同时起多少个线程执行,默认值是Runtime.getRuntime().availableProcessors()
    @Threads(1)
    //启动多个单独的进程分别测试每个方法
    @Fork(1)
    //统计结果的时间单元
    @OutputTimeUnit(TimeUnit.SECONDS)
    public class MyBenchmark {
        
        //表示此方法需要进行测试
        @Benchmark
        public static void test01(){
            String s="1"+"2"+"3"+"4";
        }
        
        @Benchmark
        public static void test02(){
            String s=new StringBuffer("1").append("2").append("3").append("4").toString();
        }
        
        public static void main(String[] args) throws Exception{
            Options opt=new OptionsBuilder()
                    //导入要测试的类
                    .include(MyBenchmark.class.getSimpleName())
                    .build();
            new Runner(opt).run();
        }
    }
    

    2.3 输出结果

    # JMH version: 1.23
    # VM version: JDK 1.8.0_121, Java HotSpot(TM) 64-Bit Server VM, 25.121-b13
    # VM invoker: C:\Program Files\Java\jdk1.8.0_121\jre\bin\java.exe
    # VM options: -javaagent:C:\work\software\IntelliJ IDEA 2019.2.3\lib\idea_rt.jar=53609:C:\work\software\IntelliJ IDEA 2019.2.3\bin -Dfile.encoding=UTF-8
    # Warmup: 2 iterations, 1 s each
    # Measurement: 3 iterations, 2 s each
    # Timeout: 10 min per iteration
    # Threads: 1 thread, will synchronize iterations
    # Benchmark mode: Throughput, ops/time
    # Benchmark: com.test.jmh.MyBenchmark.test01
    
    # Run progress: 0.00% complete, ETA 00:00:16
    # Fork: 1 of 1
    # Warmup Iteration   1: 1668396652.659 ops/s
    # Warmup Iteration   2: 1909507594.198 ops/s
    Iteration   1: 1857680113.235 ops/s
    Iteration   2: 1928208879.048 ops/s
    Iteration   3: 1967828174.481 ops/s
    
    
    Result "com.test.jmh.MyBenchmark.test01":
      1917905722.254 ±(99.9%) 1017856695.855 ops/s [Average]
      (min, avg, max) = (1857680113.235, 1917905722.254, 1967828174.481), stdev = 55792160.103
      CI (99.9%): [900049026.399, 2935762418.110] (assumes normal distribution)
    
    
    # JMH version: 1.23
    # VM version: JDK 1.8.0_121, Java HotSpot(TM) 64-Bit Server VM, 25.121-b13
    # VM invoker: C:\Program Files\Java\jdk1.8.0_121\jre\bin\java.exe
    # VM options: -javaagent:C:\work\software\IntelliJ IDEA 2019.2.3\lib\idea_rt.jar=53609:C:\work\software\IntelliJ IDEA 2019.2.3\bin -Dfile.encoding=UTF-8
    # Warmup: 2 iterations, 1 s each
    # Measurement: 3 iterations, 2 s each
    # Timeout: 10 min per iteration
    # Threads: 1 thread, will synchronize iterations
    # Benchmark mode: Throughput, ops/time
    # Benchmark: com.test.jmh.MyBenchmark.test02
    
    # Run progress: 50.00% complete, ETA 00:00:09
    # Fork: 1 of 1
    # Warmup Iteration   1: 107194791.613 ops/s
    # Warmup Iteration   2: 99651519.700 ops/s
    Iteration   1: 170972299.861 ops/s
    Iteration   2: 207053028.473 ops/s
    Iteration   3: 203784523.495 ops/s
    
    
    Result "com.test.jmh.MyBenchmark.test02":
      193936617.276 ±(99.9%) 364048302.894 ops/s [Average]
      (min, avg, max) = (170972299.861, 193936617.276, 207053028.473), stdev = 19954715.908
      CI (99.9%): [≈ 0, 557984920.171] (assumes normal distribution)
    
    
    # Run complete. Total time: 00:00:18
    
    REMEMBER: The numbers below are just data. To gain reusable insights, you need to follow up on
    why the numbers are the way they are. Use profilers (see -prof, -lprof), design factorial
    experiments, perform baseline and negative tests that provide experimental control, make sure
    the benchmarking environment is safe on JVM/OS/HW level, ask for reviews from the domain experts.
    Do not assume the numbers tell you what you want them to tell.
    
    Benchmark            Mode  Cnt           Score            Error  Units
    MyBenchmark.test01  thrpt    3  1917905722.254 ± 1017856695.855  ops/s
    MyBenchmark.test02  thrpt    3   193936617.276 ±  364048302.894  ops/s
    
    

    3 参数说明

    3.1 测试模式

    Mode表示JMH进行Benchmark时所使用的模式,代表测量的维度。

    (1)Throughput:整体吞吐量,如1秒内可以执行多少次调用

    (2)AverageTime:调用的平均时间,如每次调用平均耗时

    (3)SampleTime:随机取样,最后输出取样结果的分布,如xx%的调用在xx毫秒以内,xx%的调用在xx毫秒以上

    (4)SingleShotTime:只运行一次,用于测试冷启动的性能

    相关文章

      网友评论

          本文标题:java性能测试jmh

          本文链接:https://www.haomeiwen.com/subject/ulxafktx.html