首页 > 其他 > 详细

JMH是什么?

时间:2020-09-23 17:34:06      阅读:173      评论:0      收藏:0      [点我收藏+]
 
一、JMH是什么
       JMH是Java性能测试工具,主要是对工程中一些方法进行一些基准测试,支持的时间单位为:nano / micro / milli / macro
 
二、JMH使用案例-代码:
  
1 POM文件 
    
技术分享图片
<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>org.openjdk.jmh</groupId>
    <artifactId>jmh-core</artifactId>
    <version>1.20</version>
  </dependency>
  <dependency>
    <groupId>org.openjdk.jmh</groupId>
    <artifactId>jmh-generator-annprocess</artifactId>
    <version>1.20</version>
    <scope>provided</scope>
  </dependency>
</dependencies>


<build>
  <plugins>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>exec-maven-plugin</artifactId>
      <executions>
        <execution>
          <id>run-benchmarks</id>
          <phase>integration-test</phase>
          <goals>
            <goal>exec</goal>
          </goals>
          <configuration>
            <classpathScope>test</classpathScope>
            <executable>java</executable>
            <arguments>
              <argument>-classpath</argument>
              <classpath />
              <argument>org.openjdk.jmh.Main</argument>
              <argument>.*</argument>
            </arguments>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>
View Code

 2 测试代码:

  

技术分享图片
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;


import java.util.concurrent.TimeUnit;


@BenchmarkMode(Mode.AverageTime)     //使用模式  :是对吞吐量 平均响应时间等
@Warmup(iterations = 3)     //配置预热次数, 这里我们设置为3次 ,让Jvm该加载的都加载
//本例是一次运行5秒,总共运行3次
// 在性能对比时候,采用默认1秒即可,
@Measurement(iterations = 3, time = 5, timeUnit = TimeUnit.SECONDS)
@Threads(1) // 配置同时起多少个线程执行 ,也可以设置到方法上
@Fork(1)    //代表启动多个单独的进程分别测试每个方法,我们这里指定为每个方法启动一个进程
@OutputTimeUnit(TimeUnit.NANOSECONDS)   //OutputTimeUnit 统计结果的时间单位,这个例子的单位为 秒
public class JMHTestDemo {


    @Benchmark
    public void testStringAdd() {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < 1000; i++) {
            sb.append(i);
        }
//        System.out.println( sb.toString());
    }

    @Benchmark
    public void testStringBuilderAdd() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 1000; i++) {
            sb.append(i);
        }
//        System.out.println( sb.toString());
    }


    public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder()
                .include(JMHTestDemo.class.getSimpleName())
                .build();
        new Runner(opt).run();
    }
}
View Code

 

 

三、问题:

  1、使用Maven install 的时候不能用,最后使用Mvn package 打包,然后右键运行的
  2、 Maven的一个插件“exec-maven-plugin” 在Idea中一直是红的,没找到具体是什么问题,但是测试可以使用
  3、官方建议使用Maven来生成项目结构,但是我是直接把对应java拷到Maven项目中做的(也许这是我遇到问题比较多的原因)
mvn原型:生成\
-DinteractiveMode = false \
-DarchetypeGroupId = org.openjdk.jmh \
-DarchetypeArtifactId = jmh- java -benchmark-archetype \
-DgroupId = org.sample \
-DartifactId = 测试 \
-Dversion = 1.0
 
 
四、官网地址
 
五、官网使用案例
 
 
 

 

 

 

 

 

 

JMH是什么?

原文:https://www.cnblogs.com/lean-blog/p/13719524.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!