Java Benchmark 基准测试的实例详解

所属分类: 软件编程 / java 阅读数: 17
收藏 0 赞 0 分享

Java Benchmark 基准测试的实例详解

import java.util.Arrays; 
import java.util.concurrent.TimeUnit; 
 
import org.openjdk.jmh.annotations.Benchmark; 
import org.openjdk.jmh.annotations.BenchmarkMode; 
import org.openjdk.jmh.annotations.Measurement; 
import org.openjdk.jmh.annotations.Mode; 
import org.openjdk.jmh.annotations.OutputTimeUnit; 
import org.openjdk.jmh.annotations.Scope; 
import org.openjdk.jmh.annotations.State; 
import org.openjdk.jmh.annotations.Threads; 
import org.openjdk.jmh.annotations.Warmup; 
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; 
 
@BenchmarkMode(Mode.Throughput)//基准测试类型 
@OutputTimeUnit(TimeUnit.SECONDS)//基准测试结果的时间类型 
@Warmup(iterations = 3)//预热的迭代次数 
@Threads(2)//测试线程数量 
@State(Scope.Thread)//该状态为每个线程独享 
//度量:iterations进行测试的轮次,time每轮进行的时长,timeUnit时长单位,batchSize批次数量 
@Measurement(iterations = 2, time = -1, timeUnit = TimeUnit.SECONDS, batchSize = -1) 
public class InstructionsBenchmark{ 
  static int staticPos = 0; 
  //String src = "SELECT a FROM ab       , ee.ff AS f,(SELECT a FROM `schema_bb`.`tbl_bb`,(SELECT a FROM ccc AS c, `dddd`));"; 
  final byte[] srcBytes = {83, 69, 76, 69, 67, 84, 32, 97, 32, 70, 82, 79, 77, 32, 97, 98, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 44, 32, 101, 101, 46, 102, 102, 32, 65, 83, 32, 102, 44, 40, 83, 69, 76, 69, 67, 84, 32, 97, 32, 70, 82, 79, 77, 32, 96, 115, 99, 104, 101, 109, 97, 95, 98, 98, 96, 46, 96, 116, 98, 108, 95, 98, 98, 96, 44, 40, 83, 69, 76, 69, 67, 84, 32, 97, 32, 70, 82, 79, 77, 32, 99, 99, 99, 32, 65, 83, 32, 99, 44, 32, 96, 100, 100, 100, 100, 96, 41, 41, 59}; 
  int len = srcBytes.length; 
  byte[] array = new byte[8192]; 
  int memberVariable = 0; 
 
  //run 
  public static void main(String[] args) throws RunnerException { 
    Options opt = new OptionsBuilder() 
        .include(InstructionsBenchmark.class.getSimpleName()) 
        .forks(1) 
        //   使用之前要安装hsdis 
        //-XX:-TieredCompilation 关闭分层优化 -server 
        //-XX:+LogCompilation 运行之后项目路径会出现按照测试顺序输出hotspot_pid<PID>.log文件,可以使用JITWatch进行分析,可以根据最后运行的结果的顺序按文件时间找到对应的hotspot_pid<PID>.log文件 
        .jvmArgs("-XX:+UnlockDiagnosticVMOptions", "-XX:+LogCompilation", "-XX:+TraceClassLoading", "-XX:+PrintAssembly") 
        // .addProfiler(CompilerProfiler.class)  // report JIT compiler profiling via standard MBeans 
        // .addProfiler(GCProfiler.class)  // report GC time 
        // .addProfiler(StackProfiler.class) // report method stack execution profile 
        // .addProfiler(PausesProfiler.class) 
        /* 
        WinPerfAsmProfiler 
        You must install Windows Performance Toolkit. Once installed, locate directory with xperf.exe file 
        and either add it to PATH environment variable, or set it to jmh.perfasm.xperf.dir system property. 
         */ 
        //.addProfiler(WinPerfAsmProfiler.class) 
        //更多Profiler,请看JMH介绍 
        //.output("InstructionsBenchmark.log")//输出信息到文件 
        .build(); 
    new Runner(opt).run(); 
  } 
 
 
  //空循环 对照项 
  @Benchmark 
  public int emptyLoop() { 
    int pos = 0; 
    while (pos < len) { 
      ++pos; 
    } 
    return pos; 
  } 
 
  @Benchmark 
  public int increment() { 
    int pos = 0; 
    int result = 0; 
    while (pos < len) { 
      ++result; 
      ++pos; 
    } 
    return result; 
  } 
 
  @Benchmark 
  public int decrement() { 
    int pos = 0; 
    int result = 0; 
    while (pos < len) { 
      --result; 
      ++pos; 
    } 
    return result; 
  } 
 
  @Benchmark 
  public int ifElse() { 
    int pos = 0; 
    int result = 0; 
    while (pos < len) { 
      if (pos == 10) { 
        ++result; 
        ++pos; 
      } else { 
        ++pos; 
      } 
    } 
    return result; 
  } 
 
  @Benchmark 
  public int ifElse2() { 
    int pos = 0; 
    int result = 0; 
    while (pos < len) { 
      if (pos == 10) { 
        ++result; 
        ++pos; 
      } else if (pos == 20) { 
        ++result; 
        ++pos; 
      } else { 
        ++pos; 
      } 
    } 
    return result; 
  } 
 
  @Benchmark 
  public int ifnotElse() { 
    int pos = 0; 
    int result = 0; 
    while (pos < len) { 
      if (pos != 10) { 
        ++pos; 
      } else { 
        ++result; 
        ++pos; 
      } 
    } 
    return result; 
  } 
 
  @Benchmark 
  public int ifLessthanElse() { 
    int pos = 0; 
    int result = 0; 
    while (pos < len) { 
      if (pos < 10) { 
        ++pos; 
      } else { 
        ++result; 
        ++pos; 
      } 
    } 
    return result; 
  } 
 
  @Benchmark 
  public int ifGreaterthanElse() { 
    int pos = 0; 
    int result = 0; 
    while (pos < len) { 
      if (pos > 10) { 
        ++pos; 
      } else { 
        ++result; 
        ++pos; 
      } 
    } 
    return result; 
  } 
 
  @Benchmark 
  public int readMemberVariable_a_byteArray() { 
    int pos = 0; 
    int result = 0; 
    while (pos < len) { 
      result = srcBytes[pos]; 
      pos++; 
    } 
    return result; 
  } 
 
} 

 ops/time:标识每秒钟执行的次数

 依赖jar包:

<dependency> 
      <groupId>org.openjdk.jmh</groupId> 
      <artifactId>jmh-core</artifactId> 
      <version>${jmh.version}</version> 
    </dependency> 
    <dependency> 
      <groupId>org.openjdk.jmh</groupId> 
      <artifactId>jmh-generator-annprocess</artifactId> 
      <version>${jmh.version}</version> 
      <scope>provided</scope> 
    </dependency> 
<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> 

以上就是Java Benchmark 基准测试,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

更多精彩内容其他人还在看

Java数据类型的规则

这篇文章主要介绍了Java数据类型的规则的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Spring整合TimerTask实现定时任务调度

这篇文章主要介绍了Spring整合TimerTask实现定时任务调度的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

详解SpringMVC使用MultipartFile实现文件的上传

本篇文章主要介绍了SpringMVC使用MultipartFile实现文件的上传,本地的文件上传到资源服务器上,比较好的办法就是通过ftp上传。这里是结合SpringMVC+ftp的形式上传的,有兴趣的可以了解一下。
收藏 0 赞 0 分享

SpringMVC上传文件的三种实现方式

本篇文章主要介绍了SpringMVC上传文件的三种实现方式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

微信公众帐号开发-自定义菜单的创建及菜单事件响应的实例

本篇文章主要介绍了微信公众帐号开发-自定义菜单的创建及菜单事件响应的实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
收藏 0 赞 0 分享

浅析Java中的继承与组合

本文将介绍组合和继承的概念及区别,并从多方面分析在写代码时如何进行选择。文中通过示例代码介绍的很详细,有需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

利用反射获取Java类中的静态变量名及变量值的简单实例

下面小编就为大家带来一篇利用反射获取Java类中的静态变量名及变量值的简单实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java启动线程的3种方式对比分析

这篇文章主要为大家对比分析了java启动线程的3种方式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

SpringMVC上传和解析Excel方法

这篇文章主要介绍了SpringMVC上传和解析Excel方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

JAVA中String类与StringBuffer类的区别

这篇文章主要为大家详细介绍了JAVA中String类与StringBuffer类的区别,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多