当前位置 博文首页 > 文章内容

    JMH是什么?

    作者: 栏目:未分类 时间:2020-09-23 17:02:05

    本站于2023年9月4日。收到“大连君*****咨询有限公司”通知
    说我们IIS7站长博客,有一篇博文用了他们的图片。
    要求我们给他们一张图片6000元。要不然法院告我们

    为避免不必要的麻烦,IIS7站长博客,全站内容图片下架、并积极应诉
    博文内容全部不再显示,请需要相关资讯的站长朋友到必应搜索。谢谢!

    另祝:版权碰瓷诈骗团伙,早日弃暗投明。

    相关新闻:借版权之名、行诈骗之实,周某因犯诈骗罪被判处有期徒刑十一年六个月

    叹!百花齐放的时代,渐行渐远!



     
    一、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
     
     
    四、官网地址
     
    五、官网使用案例