jmh

JMH

简介

创建JMH测试

  1. 创建Maven项目,添加依赖
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <encoding>UTF-8</encoding>
    <java.version>1.8</java.version>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <groupId>mashibing.com</groupId>
    <artifactId>HelloJMH2</artifactId>
    <version>1.0-SNAPSHOT</version>


    <dependencies>
    <!-- https://mvnrepository.com/artifact/org.openjdk.jmh/jmh-core -->
    <dependency>
    <groupId>org.openjdk.jmh</groupId>
    <artifactId>jmh-core</artifactId>
    <version>1.21</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.openjdk.jmh/jmh-generator-annprocess -->
    <dependency>
    <groupId>org.openjdk.jmh</groupId>
    <artifactId>jmh-generator-annprocess</artifactId>
    <version>1.21</version>
    <scope>test</scope>
    </dependency>
    </dependencies>


    </project>
  2. idea安装JMH插件 JMH plugin v1.0.3
  3. 由于用到了注解,打开运行程序注解配置

    compiler -> Annotation Processors -> Enable Annotation Processing

  4. 定义需要测试类PS (ParallelStream)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    package com.mashibing.jmh;

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;

    public class PS {

    static List<Integer> nums = new ArrayList<>();
    static {
    Random r = new Random();
    for (int i = 0; i < 10000; i++) nums.add(1000000 + r.nextInt(1000000));
    }

    static void foreach() {
    nums.forEach(v->isPrime(v));
    }

    static void parallel() {
    nums.parallelStream().forEach(PS::isPrime);
    }

    static boolean isPrime(int num) {
    for(int i=2; i<=num/2; i++) {
    if(num % i == 0) return false;
    }
    return true;
    }
    }
  5. 写单元测试

    这个测试类一定要在test package下面

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    package com.mashibing.jmh;

    import org.openjdk.jmh.annotations.Benchmark;

    import static org.junit.jupiter.api.Assertions.*;

    public class PSTest {
    @Benchmark
    public void testForEach() {
    PS.foreach();
    }
    }
  6. 运行测试类,如果遇到下面的错误:
    1
    2
    3
    ERROR: org.openjdk.jmh.runner.RunnerException: ERROR: Exception while trying to acquire the JMH lock (C:\WINDOWS\/jmh.lock): C:\WINDOWS\jmh.lock (拒绝访问。), exiting. Use -Djmh.ignoreLock=true to forcefully continue.
    at org.openjdk.jmh.runner.Runner.run(Runner.java:216)
    at org.openjdk.jmh.Main.main(Main.java:71)
    这个错误是因为JMH运行需要访问系统的TMP目录,解决办法是:
    打开RunConfiguration -> Environment Variables -> include system environment viables
  7. 阅读测试报告

JMH中的基本概念(基本注解)

  1. @Warmup
    预热,由于JVM中对于特定代码会存在优化(本地化),预热对于测试结果很重要
    1. iterations:调用次数
    2. time:等待时间
  2. @fork:使用多少个线程去执行程序
  3. @Mesurement
    总共执行多少次测试
    1. iteratiosns:次数
    2. Timeout:超时时间
  4. @Benchmark mode
    基准测试的模式
  5. Benchmark
    测试哪一段代码

jmh
https://x-leonidas.github.io/2022/02/01/11技术栈/测试/jmh/
作者
听风
发布于
2022年2月1日
更新于
2025年6月25日
许可协议