mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add jmh
This commit is contained in:
@@ -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