Java Benchmark 基准测试的实例详解

所属分类: 软件编程 / java 阅读数: 22
收藏 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 分享

Eclipse下编写java程序突然不会自动生成R.java文件和包的解决办法

这篇文章主要介绍了Eclipse下编写java程序突然不会自动生成R.java文件和包的解决办法 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

基于Java实现杨辉三角 LeetCode Pascal's Triangle

这篇文章主要介绍了基于Java实现杨辉三角 LeetCode Pascal's Triangle的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Java中Spring获取bean方法小结

Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架,如何在程序中获取Spring配置的bean呢?下面通过本文给大家介绍Java中Spring获取bean方法小结,对spring获取bean方法相关知识感兴趣的朋友一起学习吧
收藏 0 赞 0 分享

如何计算Java对象占用了多少空间?

在Java中没有sizeof运算符,所以没办法知道一个对象到底占用了多大的空间,但是在分配对象的时候会有一些基本的规则,我们根据这些规则大致能判断出来对象大小,需要的朋友可以参考下
收藏 0 赞 0 分享

剖析Java中的事件处理与异常处理机制

这篇文章主要介绍了Java中的事件处理与异常处理机制,讲解Java是如何对事件或者异常作出响应以及定义异常的一些方法,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Java的Struts2框架的结构及其数据转移方式

这篇文章主要介绍了详解Java的Struts2框架的结构及其数据转移方式,Struts框架是Java的SSH三大web开发框架之一,需要的朋友可以参考下
收藏 0 赞 0 分享

Java封装好的mail包发送电子邮件的类

本文给大家分享了2个java封装好的mail包发送电子邮件的类,并附上使用方法,小伙伴们可以根据自己的需求自由选择。
收藏 0 赞 0 分享

在Java的Struts中判断是否调用AJAX及用拦截器对其优化

这篇文章主要介绍了在Java的Struts中判断是否调用AJAX及用拦截器对其优化的方法,Struts框架是Java的SSH三大web开发框架之一,需要的朋友可以参考下
收藏 0 赞 0 分享

java多线程Future和Callable类示例分享

JAVA多线程实现方式主要有三种:继承Thread类、实现Runnable接口、使用ExecutorService、Callable、Future实现有返回结果的多线程。其中前两种方式线程执行完后都没有返回值,只有最后一种是带返回值的。今天我们就来研究下Future和Callab
收藏 0 赞 0 分享
查看更多