Java Benchmark 基准测试的实例详解
作者:lqh 发布时间:2023-10-08 11:01:02
标签: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>
来源:http://hpgary.iteye.com/blog/2360981


猜你喜欢
- 本文实例讲述了Android使用Theme自定义Activity进入退出动画的方法。分享给大家供大家参考,具体如下:有没有觉得Activit
- springboot static调用service为null@PostConstruct注解好多人以为是Spring提供的。其实是Java
- 许久不来 , 冒个泡 , 发一个刚做的声音波动的View吧 : 代码不多 , 没什么技术含量 , 权当给您省时间了 , 直接复制粘贴就能用
- dynamic是C#里面的动态类型,可在未知类型的情况访问对应的属性,非常灵活和方便。使用Json.Net可以把一个Json字符串转换成一个
- 前言 需要实现环(圆)形菜单。效果预览(更多效果请下载源码体验):实现代码1.CircularMenuItemCustomCont
- 目录背景原因分析setLoadWithOverviewMode端内其他地方排查总结WebView 字体常见的其他坑手机设置字体大小导致h5页
- 简单看一下描述,例子最重要。1、getPath():返回定义时的路径,(就是你写什么路径,他就返回什么路径)2、getAbsolutePat
- 冒泡排序:就是按索引逐次比较相邻的两个元素,如果大于/小于(取决于需要升序排还是降序排),则置换,否则不做改变这样一轮下来,比较了n-1次,
- 本文实例分析了JAVA反射机制。分享给大家供大家参考,具体如下:反射,当时经常听他们说,自己也看过一些资料,也可能在设计模式中使用过,但是感
- 本文实例讲述了Android编程实现自定义手势的方法。分享给大家供大家参考,具体如下:之前介绍过如何在Android程序中使用手势,主要是系
- 本文实例讲述了Android播放多张图片形成的一个动画。分享给大家供大家参考,具体如下:在Android里可以逐帧的播放图片,然后产生一种动
- 注意是maven的webapp:选择maven下一步下一步。maven下载过慢在setting中加入镜像。 我也有疑问这是什么鬼格式,但是证
- 一.代码实现1. “Activity_11\src\yan\activity_11\MainActivity.java”pack
- 本文实例为大家分享了Android仿大众点评星星评分控件的具体代码,供大家参考,具体内容如下话不多说,直接上代码,这里采用的是自定Viewp
- springboot微服务内置了tomcat,在工程目录下执行:mvn clean package,可以将项目打成jar,通过java -j
- 理解枚举类型枚举类型是Java 5中新增特性的一部分,它是一种特殊的数据类型,之所以特殊是因为它既是一种类(class)类型却又比类类型多了
- 1.多数据源配置类整体项目结构1).pom.xml 项目依赖<?xml version="1.0" encodin
- 本文实例为大家分享了C++实现幸运大抽奖的具体代码,供大家参考,具体内容如下程序效果:#ifndef DIALOG_H#define DIA
- 一、LinkedHashMap的类继承关系二、源码分析1.自己对LinkedHashMap的理解从继承关系上,我们看到LinkedHashM
- Interface Segregation Principle,ISP接口隔离原则主张使用多个专门的接口比使用单一的总接口要好。一个类对另外