当前位置: 移动技术网 > IT编程>开发语言>Java > Java Benchmark 基准测试的实例详解

Java Benchmark 基准测试的实例详解

2019年07月19日  | 移动技术网IT编程  | 我要评论

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网