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
0
投稿
猜你喜欢
- 目录wait-notifyjoin方式ReentrantLockReentrantLock+ConditionSemaphore三个线程T1
- 包含默认敏感词过滤和自定义敏感词过滤。导入依赖<dependency> <groupId>com.git
- 1.接口中的默认方法和静态方法Java 8中允许接口中包含具有具体实现的方法,该方法称为 “默认方法” ,默认方法使用 default 关键
- 前言很多时候,当你以为掌握了事实真相的时间,如果你能再深入一点,你可能会发现另外一些真相。比如面向切面编程的最佳编程实践是AOP,AOP的主
- 1. 数据构造索引2个文档到 hotel 索引中:PUT /hotel/_doc/1{ "title": &
- 问题现象今天在做一个需求:将存入数据库中的数据读到后解析成list遍历分析数据格式:"[1677660600000, 167766
- 假定存在这样一种情况多个用户对数据库进行写,我们的业务逻辑规定,每个用户只能写一次,大部分用户也只发一次请求。public void wri
- 一,栈1,概念在我们软件应用 ,栈这种后进先出数据结构的应用是非常普遍的。比如你用浏 览器上网时不管什么浏览器都有 个"后退&qu
- 前言Feign是一个声明式的Web服务客户端,是面向接口编程的。也就是说使用Feign,只需要创建一个接口并使用注解方式配置它,就可以完成对
- 一、导航栏UINavigationBar1、导航栏的使用在iOS开发中,我们通常会使用导航控制器,导航控制器中封装了一个UINavigati
- IDEA安装后找不到.vmoptions文件在安装IDEA后在C盘的C:\Users\你的电脑用户名.IntelliJIdea2019.1\
- 在开发Android应用程序中,经常会自定义View来实现各种各样炫酷的效果,在实现这吊炸天效果的同时,我们往往会定义很多attr属性,这样
- Object是类层次结构的根,每个类都可以将Object作为超类。所有类都直接或者间接的继承自该类构造方法:public Object()回
- springboot集成swagger3swagger3的springboot启动器jar包<!-- https://mvnrepos
- 详解HDFS多文件Join操作的实例最近在做HDFS文件处理之时,遇到了多文件Join操作,其中包括:All Join以及常用的Left J
- spring Cache注解和redis区别1.不支持TTL即不能设置过期时间 expires time,SpringCache 认为这是各
- 前言apollo配置经常使用的方式是@value,比较便捷,如果只出现在一个类中还行,但是如果多个类中并不是很方便,特别是如果出现配置值变化
- 在intellij中忽略提交文件,分两种情况,文件没有纳入版本管理第一种方法文件还没有纳入版本管理,这种通过 svn的ignore配置ver
- 本文实例为大家分享了java使用HashMap实现斗地主的具体代码,供大家参考,具体内容如下案例介绍按照斗地主的规则,完成洗牌发牌的动作。
- 题目要求思路一:模拟迭代依次判断每个节点是否合法:左子树判断是否>low,合法就向左下走,不合法往右下;右子树判断是否<high