Java Benchmark 基准测试的实例详解

 更新时间:2017年08月16日 10:25:02   投稿:lqh  
这篇文章主要介绍了Java Benchmark 基准测试的实例详解的相关资料,这里提供实例帮助大家学习理解这部分内容,需要的朋友可以参考下

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 基准测试,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

相关文章

  • Spring内置定时任务调度@Scheduled使用详解

    Spring内置定时任务调度@Scheduled使用详解

    这篇文章主要介绍了Spring内置定时任务调度@Scheduled使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-12-12
  • maven配置多个镜像的实现方法

    maven配置多个镜像的实现方法

    这篇文章主要介绍了maven配置多个镜像的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-06-06
  • Java并发编程中的ReentrantLock类详解

    Java并发编程中的ReentrantLock类详解

    这篇文章主要介绍了Java并发编程中的ReentrantLock类详解,ReentrantLock是juc.locks包中的一个独占式可重入锁,相比synchronized,它可以创建多个条件等待队列,还支持公平/非公平锁、可中断、超时、轮询等特性,需要的朋友可以参考下
    2023-12-12
  • JavaWeb中HttpSession中表单的重复提交示例

    JavaWeb中HttpSession中表单的重复提交示例

    这篇文章主要介绍了JavaWeb中HttpSession中表单的重复提交,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2017-03-03
  • Java实现PDF转为Word文档的示例代码

    Java实现PDF转为Word文档的示例代码

    众所周知,PDF文档除了具有较强稳定性和兼容性外, 还具有较强的安全性,在工作中可以有效避免别人无意中对文档内容进行修改。本文将分为以下两部分介绍如何在保持布局的情况下将PDF转为Word文档,希望对大家有所帮助
    2023-01-01
  • Spring与Hibernate整合事务管理的理解

    Spring与Hibernate整合事务管理的理解

    这篇文章主要介绍了Spring与Hibernate整合事务管理的理解的相关资料,需要的朋友可以参考下
    2016-09-09
  • RocketMQ延迟消息超详细讲解

    RocketMQ延迟消息超详细讲解

    延时消息是指发送到 RocketMQ 后不会马上被消费者拉取到,而是等待固定的时间,才能被消费者拉取到。延时消息的使用场景很多,比如电商场景下关闭超时未支付的订单,某些场景下需要在固定时间后发送提示消息
    2023-02-02
  • SpringBoot深入讲解单元测试与热部署应用

    SpringBoot深入讲解单元测试与热部署应用

    这篇文章介绍了SpringBoot单元测试与热部署,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-06-06
  • springSecurity之AuthenticationProvider用法解析

    springSecurity之AuthenticationProvider用法解析

    这篇文章主要介绍了springSecurity之AuthenticationProvider用法解析,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-03-03
  • Java获取网络文件并插入数据库的代码

    Java获取网络文件并插入数据库的代码

    抓取各大网站的数据插入数据库,这样就不用为没有数据而烦恼了
    2010-06-06

最新评论