OpenJDK JMH——Java程序的基准测试工具

开发 开发工具
JMH提供了一组注解和API,使得编写微基准测试变得简单和方便。使用JMH,您可以定义和运行各种类型的基准测试,包括方法级别的基准测试、类级别的基准测试和多线程基准测试。JMH还提供了丰富的选项和配置,以控制基准测试的执行方式,如迭代次数、线程数、预热时间等。

背景

JMH(Java Microbenchmark Harness)是一个专门用于编写、运行和分析Java微基准测试的工具。它是由OpenJDK项目提供的一个开源项目,旨在帮助开发人员准确地测量和评估Java代码的性能。

JMH提供了一组注解和API,使得编写微基准测试变得简单和方便。使用JMH,您可以定义和运行各种类型的基准测试,包括方法级别的基准测试、类级别的基准测试和多线程基准测试。JMH还提供了丰富的选项和配置,以控制基准测试的执行方式,如迭代次数、线程数、预热时间等。

JMH的主要特点包括:

  • 自动优化:JMH会自动优化基准测试,以消除热点代码的影响,并提供准确的性能度量。
  • 避免优化:JMH使用黑魔法技巧来避免JIT编译器过早优化基准测试代码。
  • 统计数据:JMH提供了丰富的统计数据和报告,包括平均执行时间、吞吐量、标准差等,以帮助您分析和理解基准测试的性能。
  • 基准测试模式:JMH支持多种基准测试模式,如平均时间模式、吞吐量模式、采样模式等,以满足不同的性能测试需求。

JMH使用方法

使用JMH编写和运行基准测试的一般步骤包括:

  • 在测试类或测试方法上使用JMH提供的注解,如@Benchmark、@State、@Setup等,来定义基准测试。
  • 使用JMH提供的命令行工具或API来运行基准测试,并指定相关的选项和参数。
  • 分析和解释JMH生成的性能报告,以评估和优化代码的性能。

JMH使用示例

jmh提供了大量的测试用例,参考资料【1】所示。

@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public class JMHSample_21_ConsumeCPU {

    /*
     * At times you require the test to burn some of the cycles doing nothing.
     * In many cases, you *do* want to burn the cycles instead of waiting.
     *
     * For these occasions, we have the infrastructure support. Blackholes
     * can not only consume the values, but also the time! Run this test
     * to get familiar with this part of JMH.
     *
     * (Note we use static method because most of the use cases are deep
     * within the testing code, and propagating blackholes is tedious).
     */

    @Benchmark
    public void consume_0000() {
        Blackhole.consumeCPU(0);
    }

    @Benchmark
    public void consume_0001() {
        Blackhole.consumeCPU(1);
    }

    @Benchmark
    public void consume_0002() {
        Blackhole.consumeCPU(2);
    }

    @Benchmark
    public void consume_0004() {
        Blackhole.consumeCPU(4);
    }

    @Benchmark
    public void consume_0008() {
        Blackhole.consumeCPU(8);
    }

    @Benchmark
    public void consume_0016() {
        Blackhole.consumeCPU(16);
    }

    @Benchmark
    public void consume_0032() {
        Blackhole.consumeCPU(32);
    }

    @Benchmark
    public void consume_0064() {
        Blackhole.consumeCPU(64);
    }

    @Benchmark
    public void consume_0128() {
        Blackhole.consumeCPU(128);
    }

    @Benchmark
    public void consume_0256() {
        Blackhole.consumeCPU(256);
    }

    @Benchmark
    public void consume_0512() {
        Blackhole.consumeCPU(512);
    }

    @Benchmark
    public void consume_1024() {
        Blackhole.consumeCPU(1024);
    }

    /*
     * ============================== HOW TO RUN THIS TEST: ====================================
     *
     * Note the single token is just a few cycles, and the more tokens
     * you request, then more work is spent (almost linearly)
     *
     * You can run this test:
     *
     * a) Via the command line:
     *    $ mvn clean install
     *    $ java -jar target/benchmarks.jar JMHSample_21 -f 1
     *    (we requested single fork; there are also other options, see -h)
     *
     * b) Via the Java API:
     *    (see the JMH homepage for possible caveats when running from IDE:
     *      http://openjdk.java.net/projects/code-tools/jmh/)
     */

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

        new Runner(opt).run();
    }

}

JMH总结

JMH是一个强大的工具,可以帮助开发人员进行准确和可靠的Java微基准测试,以评估和改进代码的性能。它广泛应用于Java开发社区,并被认为是Java性能测试领域的事实标准。

参考资料:

【1】https://github.com/openjdk/jmh/tree/master/jmh-samples/src/main/java/org/openjdk/jmh/samples。

责任编辑:姜华 来源: 今日头条
相关推荐

2024-03-06 18:09:06

Linux性能工具

2021-07-08 14:59:05

JMHMongodb数据

2009-03-13 09:25:47

浏览器测试PeacekeeperIE

2011-07-13 10:07:28

服务器基准测试

2023-05-12 13:21:12

JMHJava程序

2022-01-24 17:05:12

Java程序员工具

2020-06-10 10:40:03

JavaJMH字符串

2022-07-06 09:29:40

JMH性能测试

2009-06-26 10:22:58

JSF测试

2011-05-31 18:09:05

动态测试

2009-03-31 09:49:40

Rational功能测试性能测试

2010-06-13 17:12:10

Linux串口测试工具

2009-12-21 09:54:54

Web应用程序安全测试

2017-10-12 10:57:51

MySQL基准测试sysbench工具

2022-01-24 16:55:09

LinuxTCP工具

2009-10-10 11:11:40

服务器测试

2013-11-13 10:49:50

2011-05-31 18:25:25

测试工具

2010-05-28 10:53:07

Linux串口测试工具

2018-01-23 15:02:42

负载测试工具Gatling
点赞
收藏

51CTO技术栈公众号