mirror of
https://gitee.com/dromara/hutool.git
synced 2025-12-05 03:17:49 +08:00
add jmh
This commit is contained in:
80
hutool-jmh/README.md
Normal file
80
hutool-jmh/README.md
Normal file
@@ -0,0 +1,80 @@
|
||||
<p align="center">
|
||||
<a href="https://hutool.cn/"><img src="https://plus.hutool.cn/images/hutool.svg" width="45%"></a>
|
||||
</p>
|
||||
<p align="center">
|
||||
<strong>🍬Make Java Sweet Again.</strong>
|
||||
</p>
|
||||
<p align="center">
|
||||
👉 <a href="https://hutool.cn">https://hutool.cn/</a> 👈
|
||||
</p>
|
||||
|
||||
## 📚Hutool-jmh 模块介绍
|
||||
|
||||
`Hutool-jmh`提供性能对比测试
|
||||
|
||||
## 安装
|
||||
|
||||
### 引入依赖
|
||||
|
||||
```xml
|
||||
<!-- 基准性能测试 -->
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-core</artifactId>
|
||||
<version>${jmh.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${jmh.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
### 安装插件
|
||||
|
||||
IDEA安装`JMH Java Microbenchmark Harness`插件,主页见:https://plugins.jetbrains.com/plugin/7529-jmh-java-microbenchmark-harness
|
||||
|
||||
## 注解说明
|
||||
|
||||
### @BenchmarkMode
|
||||
使用哪种模式测试,JMH 提供了4种不同的模式:
|
||||
- `Mode.Throughput` 吞吐量, ops/time。单位时间内执行操作的平均次数,例如“1秒内可以执行多少次调用”
|
||||
- `Mode.AverageTime` 平均时间, time/op。 执行每次操作所需的平均时间,例如“每次调用平均耗时xxx毫秒”
|
||||
- `Mode.SampleTime` 采样时间, time/op。 同 AverageTime。区别在于会统计取样分布,例如“99%的调用在xxx毫秒以内,99.99%的调用在xxx毫秒以内”
|
||||
- `Mode.SingleShotTime` 单次时间, time。 同 AverageTime。区别在于只执行一次操作。这种模式的结果存在较大随机性,往往同时把 warmup 次数设为0,用于测试冷启动时的性能。
|
||||
|
||||
### @Warmup和 @Measurement
|
||||
@Warmup 和@Measurement分别用于配置预热迭代和测试迭代。其中:
|
||||
- iterations 用于指定迭代次数。
|
||||
- time 和 timeUnit 用于每个迭代的时间。
|
||||
- batchSize 表示执行多少次 Benchmark为一个invocation。
|
||||
|
||||
### @State
|
||||
类注解,JMH 测试类必须使用 @State 注解,它定义了一个类实例的生命周期,有三种类型:
|
||||
- `Scope.Thread`:每个线程一个实例,适合不共享数据的测试。
|
||||
- `Scope.Benchmark`:所有测试线程共享一个实例,用于测试有状态实例在多线程共享下的性能;
|
||||
- `Scope.Group`:每个线程组共享一个实例;
|
||||
|
||||
### @Fork
|
||||
进行 fork 的次数。如果 fork 数是2的话,则 JMH 会 fork 出两个进程来进行测试。
|
||||
|
||||
### @Setup 和 @TearDown
|
||||
方法注解。
|
||||
- `@Setup`会在执行 benchmark 之前被执行,主要用于初始化。
|
||||
- `@TearDown`会在执行 benchmark 之后被执行,主要用于资源的回收等。
|
||||
|
||||
### @Benchmark
|
||||
方法注解,表示该方法是需要进行 benchmark 的对象。
|
||||
|
||||
### @OutputTimeUnit
|
||||
输出的时间单位。
|
||||
|
||||
### @Param
|
||||
成员注解,可以用来指定某项参数的多种情况。特别适合用来测试一个函数在不同的参数输入的情况下的性能。@Param 注解接收一个String数组,在 @Setup 方法执行前转化为为对应的数据类型。
|
||||
多个 @Param 注解的成员之间是乘积关系,譬如有两个用 @Param 注解的字段,第一个有5个值,第二个字段有2个值,那么每个测试方法会跑5*2=10次。
|
||||
|
||||
## 参考:
|
||||
|
||||
[如何在 Java 中使用 JMH 进行基准测试](https://segmentfault.com/a/1190000039902797)
|
||||
101
hutool-jmh/pom.xml
Normal file
101
hutool-jmh/pom.xml
Normal file
@@ -0,0 +1,101 @@
|
||||
<?xml version='1.0' encoding='utf-8'?>
|
||||
<!--
|
||||
~ Copyright (c) 2024 Hutool Team and hutool.cn
|
||||
~
|
||||
~ Licensed under the Apache License, Version 2.0 (the "License");
|
||||
~ you may not use this file except in compliance with the License.
|
||||
~ You may obtain a copy of the License at
|
||||
~
|
||||
~ http://www.apache.org/licenses/LICENSE-2.0
|
||||
~
|
||||
~ Unless required by applicable law or agreed to in writing, software
|
||||
~ distributed under the License is distributed on an "AS IS" BASIS,
|
||||
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
~ See the License for the specific language governing permissions and
|
||||
~ limitations under the License.
|
||||
-->
|
||||
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>org.dromara.hutool</groupId>
|
||||
<artifactId>hutool-parent</artifactId>
|
||||
<version>6.0.0-M17</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>hutool-jmh</artifactId>
|
||||
<name>${project.artifactId}</name>
|
||||
<description>Hutool 性能测试</description>
|
||||
|
||||
<properties>
|
||||
<Automatic-Module-Name>org.dromara.hutool.jmh</Automatic-Module-Name>
|
||||
<!-- 此模块不部署 -->
|
||||
<maven.deploy.skip>true</maven.deploy.skip>
|
||||
<jmh.version>1.37</jmh.version>
|
||||
|
||||
<jackson.version>2.17.2</jackson.version>
|
||||
<gson.version>2.11.0</gson.version>
|
||||
<fastjson2.version>2.0.41</fastjson2.version>
|
||||
<moshi.version>1.15.1</moshi.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.dromara.hutool</groupId>
|
||||
<artifactId>hutool-bom</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- 基准性能测试 -->
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-core</artifactId>
|
||||
<version>${jmh.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${jmh.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- JSON 引擎实现 -->
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.datatype</groupId>
|
||||
<artifactId>jackson-datatype-jsr310</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>${gson.version}</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.fastjson2</groupId>
|
||||
<artifactId>fastjson2</artifactId>
|
||||
<version>${fastjson2.version}</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup.moshi</groupId>
|
||||
<artifactId>moshi</artifactId>
|
||||
<version>1.15.1</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,41 @@
|
||||
package org.dromara.hutool.jmh.core;
|
||||
|
||||
import org.dromara.hutool.core.io.buffer.FastCharBuffer;
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@BenchmarkMode(Mode.AverageTime)//每次执行平均花费时间
|
||||
@Warmup(iterations = 1, time = 1, timeUnit = TimeUnit.SECONDS) //预热1次调用
|
||||
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) // 执行5此,每次1秒
|
||||
@Threads(1) //单线程
|
||||
@Fork(1) //
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS) // 单位:纳秒
|
||||
@State(Scope.Benchmark) // 共享域
|
||||
public class CharBufferJmh {
|
||||
|
||||
private final int appendCount = 10000;
|
||||
private String str;
|
||||
|
||||
@Setup
|
||||
public void setup() {
|
||||
str = "abc123你好";
|
||||
}
|
||||
|
||||
@SuppressWarnings("MismatchedQueryAndUpdateOfStringBuilder")
|
||||
@Benchmark
|
||||
public void stringBuilderJmh() {
|
||||
final StringBuilder stringBuilder = new StringBuilder(1024);
|
||||
for (int i = 0; i < appendCount; i++) {
|
||||
stringBuilder.append(str);
|
||||
}
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void fastCharBufferJmh() {
|
||||
final FastCharBuffer fastCharBuffer = new FastCharBuffer(1024);
|
||||
for (int i = 0; i < appendCount; i++) {
|
||||
fastCharBuffer.append(str);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
/**
|
||||
* 核心包性能测试
|
||||
*/
|
||||
package org.dromara.hutool.jmh.core;
|
||||
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* Copyright (c) 2024 Hutool Team and hutool.cn
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.dromara.hutool.jmh.json;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.json.engine.JSONEngine;
|
||||
import org.dromara.hutool.json.engine.JSONEngineFactory;
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@BenchmarkMode(Mode.AverageTime)//每次执行平均花费时间
|
||||
@Warmup(iterations = 5, time = 1) //预热5次调用
|
||||
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) // 执行5此,每次1秒
|
||||
@Threads(1) //单线程
|
||||
@Fork(1) //
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS) // 单位:纳秒
|
||||
@State(Scope.Benchmark) // 共享域
|
||||
public class BeanToJsonStrJmh {
|
||||
|
||||
private JSONEngine jacksonEngine;
|
||||
private JSONEngine gsonEngine;
|
||||
private JSONEngine fastJSONEngine;
|
||||
private JSONEngine moshiEngine;
|
||||
private JSONEngine hutoolEngine;
|
||||
|
||||
private TestBean testBean;
|
||||
|
||||
@Setup
|
||||
public void setup() {
|
||||
jacksonEngine = JSONEngineFactory.createEngine("jackson");
|
||||
gsonEngine = JSONEngineFactory.createEngine("gson");
|
||||
fastJSONEngine = JSONEngineFactory.createEngine("fastjson");
|
||||
moshiEngine = JSONEngineFactory.createEngine("moshi");
|
||||
hutoolEngine = JSONEngineFactory.createEngine("hutool");
|
||||
|
||||
testBean = new TestBean("张三", 18, true, new Date(), null);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void jacksonJmh() {
|
||||
jacksonEngine.toJsonString(testBean);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void gsonJmh() {
|
||||
gsonEngine.toJsonString(testBean);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void fastJSONJmh() {
|
||||
fastJSONEngine.toJsonString(testBean);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void moshiJSONJmh() {
|
||||
moshiEngine.toJsonString(testBean);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void hutoolJSONJmh() {
|
||||
hutoolEngine.toJsonString(testBean);
|
||||
}
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
static class TestBean {
|
||||
private String name;
|
||||
private int age;
|
||||
private boolean gender;
|
||||
private Date createDate;
|
||||
private Object nullObj;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Copyright (c) 2024 Hutool Team and hutool.cn
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.dromara.hutool.jmh.json;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.google.gson.JsonElement;
|
||||
import org.dromara.hutool.json.JSON;
|
||||
import org.dromara.hutool.json.engine.JSONEngine;
|
||||
import org.dromara.hutool.json.engine.JSONEngineFactory;
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@BenchmarkMode(Mode.AverageTime)//每次执行平均花费时间
|
||||
@Warmup(iterations = 5, time = 1) //预热5次调用
|
||||
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) // 执行5此,每次1秒
|
||||
@Threads(1) //单线程
|
||||
@Fork(1) //
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS) // 单位:纳秒
|
||||
@State(Scope.Benchmark) // 共享域
|
||||
public class FromJsonStringStrJmh {
|
||||
|
||||
private JSONEngine jacksonEngine;
|
||||
private JSONEngine gsonEngine;
|
||||
private JSONEngine fastJSONEngine;
|
||||
private JSONEngine hutoolEngine;
|
||||
|
||||
private String jsonStr;
|
||||
|
||||
@Setup
|
||||
public void setup() {
|
||||
jsonStr = "{\"name\":\"张三\",\"age\":18,\"birthday\":\"2020-01-01\"}";
|
||||
|
||||
jacksonEngine = JSONEngineFactory.createEngine("jackson");
|
||||
gsonEngine = JSONEngineFactory.createEngine("gson");
|
||||
fastJSONEngine = JSONEngineFactory.createEngine("fastjson");
|
||||
hutoolEngine = JSONEngineFactory.createEngine("hutool");
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void jacksonJmh() {
|
||||
jacksonEngine.fromJsonString(jsonStr, JsonNode.class);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void gsonJmh() {
|
||||
gsonEngine.fromJsonString(jsonStr, JsonElement.class);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void fastJSONJmh() {
|
||||
fastJSONEngine.fromJsonString(jsonStr, com.alibaba.fastjson2.JSON.class);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void hutoolJSONJmh() {
|
||||
hutoolEngine.fromJsonString(jsonStr, JSON.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package org.dromara.hutool.jmh.json;
|
||||
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
||||
import com.google.gson.JsonArray;
|
||||
import org.dromara.hutool.core.lang.Console;
|
||||
import org.dromara.hutool.core.util.RandomUtil;
|
||||
import org.dromara.hutool.json.JSONArray;
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 测试JSON树结构转JSON字符串性能
|
||||
*/
|
||||
@BenchmarkMode(Mode.AverageTime)//每次执行平均花费时间
|
||||
@Warmup(iterations = 1, time = 1) //预热5次调用
|
||||
@Measurement(iterations = 1, time = 1, timeUnit = TimeUnit.SECONDS) // 执行5此,每次1秒
|
||||
@Threads(1) //单线程
|
||||
@Fork(1) //
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS) // 单位:纳秒
|
||||
@State(Scope.Benchmark) // 共享域
|
||||
public class JsonAddJmh {
|
||||
|
||||
List<String> testData;
|
||||
private JSONArray hutoolJSON;
|
||||
private JsonArray gson;
|
||||
private com.alibaba.fastjson2.JSONArray fastJSON;
|
||||
private ArrayNode jackson;
|
||||
|
||||
|
||||
@Setup
|
||||
public void setup() {
|
||||
Console.log("准备数据。。。");
|
||||
testData = new ArrayList<>(10);
|
||||
for (int i = 0; i < 10; i++) {
|
||||
testData.add(RandomUtil.randomString(20));
|
||||
}
|
||||
|
||||
hutoolJSON = new JSONArray();
|
||||
gson = new JsonArray();
|
||||
fastJSON = new com.alibaba.fastjson2.JSONArray();
|
||||
jackson = JsonNodeFactory.instance.arrayNode();
|
||||
Console.log("数据完毕");
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void gsonJmh() {
|
||||
testData.forEach(gson::add);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void hutoolJmh() {
|
||||
testData.forEach(hutoolJSON::addValue);
|
||||
//hutoolJSON.putAllObj(testData);
|
||||
}
|
||||
|
||||
@SuppressWarnings("UseBulkOperation")
|
||||
@Benchmark
|
||||
public void fastJSONJmh() {
|
||||
testData.forEach(fastJSON::add);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void jacksonJmh(){
|
||||
testData.forEach(jackson::add);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package org.dromara.hutool.jmh.json;
|
||||
|
||||
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import com.google.gson.JsonObject;
|
||||
import org.dromara.hutool.core.util.RandomUtil;
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 测试JSON树结构转JSON字符串性能
|
||||
*/
|
||||
@BenchmarkMode(Mode.AverageTime)//每次执行平均花费时间
|
||||
@Warmup(iterations = 1, time = 1) //预热5次调用
|
||||
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) // 执行5此,每次1秒
|
||||
@Threads(1) //单线程
|
||||
@Fork(1) //
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS) // 单位:纳秒
|
||||
@State(Scope.Benchmark) // 共享域
|
||||
public class JsonPutJmh {
|
||||
|
||||
Map<String, String> testData;
|
||||
private JSONObject hutoolJSON;
|
||||
private JsonObject gson;
|
||||
private com.alibaba.fastjson2.JSONObject fastJSON;
|
||||
private ObjectNode jackson;
|
||||
|
||||
|
||||
@Setup
|
||||
public void setup() {
|
||||
testData = new HashMap<>(100, 1);
|
||||
for (int i = 0; i < 100; i++) {
|
||||
testData.put(RandomUtil.randomString(10), RandomUtil.randomString(20));
|
||||
}
|
||||
|
||||
hutoolJSON = new JSONObject();
|
||||
gson = new JsonObject();
|
||||
fastJSON = new com.alibaba.fastjson2.JSONObject();
|
||||
jackson = JsonNodeFactory.instance.objectNode();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void gsonJmh() {
|
||||
testData.forEach(gson::addProperty);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void hutoolJmh() {
|
||||
testData.forEach(hutoolJSON::putValue);
|
||||
//hutoolJSON.putAllObj(testData);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void fastJSONJmh() {
|
||||
fastJSON.putAll(testData);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void jacksonJmh(){
|
||||
testData.forEach(jackson::put);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package org.dromara.hutool.jmh.json;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonParser;
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 测试JSON树结构转JSON字符串性能
|
||||
*/
|
||||
@BenchmarkMode(Mode.AverageTime)//每次执行平均花费时间
|
||||
@Warmup(iterations = 1, time = 1) //预热5次调用
|
||||
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) // 执行5此,每次1秒
|
||||
@Threads(1) //单线程
|
||||
@Fork(1) //
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS) // 单位:纳秒
|
||||
@State(Scope.Benchmark) // 共享域
|
||||
public class JsonToStringJmh {
|
||||
|
||||
private JSONObject hutoolJSON;
|
||||
private JsonElement gson;
|
||||
private com.alibaba.fastjson2.JSONObject fastJSON;
|
||||
|
||||
|
||||
@Setup
|
||||
public void setup() {
|
||||
final String jsonStr = "{\"name\":\"张三\",\"age\":18,\"birthday\":\"2020-01-01\"}";
|
||||
hutoolJSON = JSONUtil.parseObj(jsonStr);
|
||||
gson = JsonParser.parseString(jsonStr);
|
||||
fastJSON = JSON.parseObject(jsonStr);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void gsonJmh() {
|
||||
final String jsonStr = gson.toString();
|
||||
Assertions.assertNotNull(jsonStr);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void hutoolJmh() {
|
||||
final String jsonStr = hutoolJSON.toString();
|
||||
Assertions.assertNotNull(jsonStr);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void fastJSONJmh() {
|
||||
final String jsonStr = fastJSON.toString();
|
||||
Assertions.assertNotNull(jsonStr);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package org.dromara.hutool.jmh.json;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonParser;
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
/**
|
||||
* JSON将字符串解析为树结构的性能对比测试
|
||||
*
|
||||
* @author looly
|
||||
*/
|
||||
@BenchmarkMode(Mode.AverageTime)//每次执行平均花费时间
|
||||
@Warmup(iterations = 5, time = 1) //预热5次调用
|
||||
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) // 执行5此,每次1秒
|
||||
@Threads(1) //单线程
|
||||
@Fork(1) //
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS) // 单位:纳秒
|
||||
@State(Scope.Benchmark) // 共享域
|
||||
public class ParseTreeJmh {
|
||||
|
||||
private String jsonStr;
|
||||
|
||||
@Setup
|
||||
public void setup() {
|
||||
jsonStr = "{\"name\":\"张三\",\"age\":18,\"birthday\":\"2020-01-01\"}";
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void gsonJmh() {
|
||||
final JsonElement jsonElement = JsonParser.parseString(jsonStr);
|
||||
assertNotNull(jsonElement);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void hutoolJmh() {
|
||||
final JSONObject parse = JSONUtil.parseObj(jsonStr);
|
||||
assertNotNull(parse);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void fastJSONJmh() {
|
||||
final com.alibaba.fastjson2.JSONObject jsonObject = JSON.parseObject(jsonStr);
|
||||
assertNotNull(jsonObject);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void jacksonJmh() throws JsonProcessingException {
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
final JsonNode jsonNode = mapper.readTree(jsonStr);
|
||||
assertNotNull(jsonNode);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
/**
|
||||
* JSON性能测试
|
||||
*/
|
||||
package org.dromara.hutool.jmh.json;
|
||||
Reference in New Issue
Block a user