Java Benchmark 基准测试的实例详解

作者:lqh 时间:2023-10-08 11:01:02 

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>

来源:http://hpgary.iteye.com/blog/2360981

标签:Java,Benchmark
0
投稿

猜你喜欢

  • java中方法递归的简单示例

    2022-11-25 11:45:12
  • JDK源码之Vector与HashSet解析

    2021-09-06 10:47:23
  • java使用Hashtable过滤数组中重复值的方法

    2023-10-22 06:24:08
  • 深入第K大数问题以及算法概要的详解

    2022-05-22 16:52:29
  • Android自定义view实现TextView方形输入框

    2021-09-16 17:09:33
  • C#串口通信模块使用方法示例

    2023-06-19 12:13:57
  • 解析Mybatis SqlSessionFactory初始化原理

    2022-07-09 04:24:05
  • java实现按层遍历二叉树

    2021-12-04 06:58:35
  • java按字节截取带有汉字的字符串的解法(推荐)

    2022-10-08 23:49:49
  • Java优先队列(PriorityQueue)重写compare操作

    2022-10-02 03:59:12
  • c#日志记录帮助类分享

    2021-07-06 16:45:03
  • C# FileStream实现大文件复制

    2021-06-14 11:00:53
  • 利用java开发简易版扫雷游戏

    2023-11-07 14:35:15
  • Java 如何调用long的最大值和最小值

    2021-07-24 02:27:18
  • 解决Spring Cloud feign GET请求无法用实体传参的问题

    2023-11-17 14:14:05
  • Java求最小生成树的两种算法详解

    2023-11-10 07:21:24
  • Android采取ContentObserver方式自动获取验证码

    2023-07-31 16:20:48
  • Java Swing实现让窗体居中显示的方法示例

    2023-11-06 02:59:07
  • 学习Java九大内置对象

    2023-10-13 01:10:10
  • Java 多线程并发LockSupport

    2022-04-07 23:59:57
  • asp之家 软件编程 m.aspxhome.com