使用JMH微基准测试一切

栏目: Java · 发布时间: 6年前

内容简介:使用JMH微基准测试一切

引用江南白衣的一句话:没有测试数据证明的论断,都是可疑的,诚意推荐JMH。这篇文章简要介绍怎么使用JMH来做基准测试,可以作为JMH的入门教程。

使用Maven搭建基准测试项目骨架

JMH官方推荐使用Maven来搭建基准测试的骨架,使用也很简单,使用如下命令来生成maven项目:

mvn archetype:generate \
          -DinteractiveMode=false \
          -DarchetypeGroupId=org.openjdk.jmh \
          -DarchetypeArtifactId=jmh-java-benchmark-archetype \
          -DgroupId=org.sample \
          -DartifactId=test \
          -Dversion=1.0

上面的maven命令使用了jmh-java-benchmark-archetype来生成 java 语言的基准测试骨架,如果使用其他语言可以将这个参数对应替换,所有可选参数参考 jmh ,生成的项目groupId是org.sample,artifaceId是test,执行完之后会在当前目录下生成一个test目录,切换到test目录下执行 mvn clean install 就会生成benchmarks.jar,再使用 java -jar benchmarks.jar 就可以执行基准测试了。

JMH参数配置

如果你想直接在已有maven项目中集成JMH,那也很简单,手动在POM文件中添加以下两个依赖就行了,

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

从maven archetype插件生成的pom文件来看,这个工程使用了maven-shade-plugin来将所有的依赖打包到同一个jar包中,并在Manifest文件中配置了Main-Class属性,这样就能直接通过java -jar命令来执行了,其实通过maven-assembly-plugin也可以达到同样的效果,如下所示:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.1.0</version>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <archive>
            <manifest>
                <mainClass>org.openjdk.jmh.Mainp</mainClass>
            </manifest>
        </archive>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id> <!-- this is used for inheritance merges -->
            <phase>package</phase> <!-- bind to the packaging phase -->
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

可以看到jar包的入口在 org.openjdk.jmh.Mainp 这个类,查看这个类的源码可以发现这个类会从 /META-INF/BenchmarkList 文件中读取基准测试列表。在工作中你可能经常听过别人说不要logger中使用字符串拼接来打印日志,而是使用占位符或者使用logger.isDebugEnable()语句来判断,这三种写法的性能差异到底有多大,我们就来测试一下。

本文出于演示的目的来讲解JMH的使用,直接使用了官方的例子,使用slf4j+logback的组合来打印日志,首先在POM文件中添加依赖:

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>1.7.7</version>
</dependency>
<dependency>
  <groupId>ch.qos.logback</groupId>
  <artifactId>logback-classic</artifactId>
  <version>1.0.11</version>
</dependency>

在resources目录下添加logback.xml文件,如下所示:

<configuration>
  <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%highlight(%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n)</pattern>
    </encoder>
  </appender>
  
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder><pattern>%msg%n</pattern></encoder>
  </appender>
  
  <root level="INFO">
    <appender-ref ref="CONSOLE" />
  </root>
</configuration>

接下来就在MyBenchmark类中写测试代码了,三个方法分别对应三种不同的打印日志的写法,如下所示:

package org.sample;

import org.openjdk.jmh.annotations.Benchmark;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MyBenchmark {

    private static final Logger logger = LoggerFactory.getLogger(MyBenchmark.class);


    @Benchmark
    public void testConcatenatingStrings() {
        String x = "", y = "", z = "";

        for (int i = 0; i < 100; i++) {
            x += i; y += i; z += i;

            logger.debug("Concatenating strings " + x + y + z);
        }
    }

    @Benchmark
    public void testVariableArguments() {

        String x = "", y = "", z = "";

        for (int i = 0; i < 100; i++) {
            x += i;
            y += i;
            z += i;
            logger.debug("Variable arguments {} {} {}", x, y, z);
        }
    }

    @Benchmark
    public void testIfDebugEnabled() {

        String x = "", y = "", z = "";

        for (int i = 0; i < 100; i++) {
            x += i; y += i; z += i;

            if (logger.isDebugEnabled())
                logger.debug("If debug enabled {} {} {}", x, y, z);
        }
    }
}

最后使用maven命令打包并执行:

$ mvn clean install
$ java -jar target/benchmarks.jar

最后三种不同写法的性能对比如下所示:

迭代次数 字符串拼接 占位符 isDebugEnabled
Iteration 1 57108,635 ops/s 97921,939 ops/s 104993,368 ops/s
Iteration 2 58441,293 ops/s 98036,051 ops/s 104839,216 ops/s
Iteration 3 58231,243 ops/s 97457,222 ops/s 106601,803 ops/s
Iteration 4 58538,842 ops/s 100861,562 ops/s 104643,717 ops/s
Iteration 5 57297,787 ops/s 100405,656 ops/s 104706,503 ops/s
Iteration 6 57838,298 ops/s 98912,545 ops/s 105439,939 ops/s
Iteration 7 56645,371 ops/s 100543,188 ops/s 102893,089 ops/s
Iteration 8 56569,236 ops/s 102239,005 ops/s 104730,682 ops/s
Iteration 9 57349,754 ops/s 94482,508 ops/s 103492,227 ops/s
Iteration 10 56894,075 ops/s 101405,938 ops/s 106790,525 ops/s
Average 57491,4534 ops/s 99226,5614 ops/s 104913,1069 ops/s

最后的结果也很明显了,使用isDebugEnabled性能最佳,使用字符串拼接性能最差,使用占位符性能也还不错,但是占位符的代码可读性更好,因此在项目中推荐使用占位符打印日志。

参考文档:

  1. jmh官网

以上所述就是小编给大家介绍的《使用JMH微基准测试一切》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Perl入门经典

Perl入门经典

[美]Curtis "Ovid" Poe / 朱允刚、韩雷、叶斌 / 清华大学出版社 / 2013-9-20 / 78.00

作为最有影响力的编程语言之一,Perl被广泛用在Web开发、数据处理和系统管理中。无论是Perl新手,还是想要加强自己实战技能的Perl程序员,《Perl入门经典》都提供了处理日常情况所需的各种技术。凭借十多年的Perl经验,作者Curtis“Ovid”Poe一开始先简单回顾了Perl的基础知识,然后以此为出发点,举例说明了Perl在工作场所中的各种真实用法。此外,书中还包含了一些动手练习、宝贵建......一起来看看 《Perl入门经典》 这本书的介绍吧!

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具