mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
JsonPutJmh
This commit is contained in:
parent
00c01eedac
commit
9a753e3fd6
@ -0,0 +1,56 @@
|
|||||||
|
package org.dromara.hutool.json.jmh;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
|
||||||
|
@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();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public void gsonJmh() {
|
||||||
|
testData.forEach(gson::addProperty);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public void hutoolJmh() {
|
||||||
|
testData.forEach(hutoolJSON::putObj);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public void fastJSONJmh() {
|
||||||
|
testData.forEach(fastJSON::put);
|
||||||
|
}
|
||||||
|
}
|
@ -10,6 +10,9 @@ import org.openjdk.jmh.annotations.*;
|
|||||||
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试JSON树结构转JSON字符串性能
|
||||||
|
*/
|
||||||
@BenchmarkMode(Mode.AverageTime)//每次执行平均花费时间
|
@BenchmarkMode(Mode.AverageTime)//每次执行平均花费时间
|
||||||
@Warmup(iterations = 1, time = 1) //预热5次调用
|
@Warmup(iterations = 1, time = 1) //预热5次调用
|
||||||
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) // 执行5此,每次1秒
|
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) // 执行5此,每次1秒
|
||||||
|
Loading…
x
Reference in New Issue
Block a user