mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add jmh
This commit is contained in:
parent
90de2c4d59
commit
0175f1922b
@ -30,7 +30,7 @@ import java.util.concurrent.TimeUnit;
|
||||
@Fork(1) //
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS) // 单位:纳秒
|
||||
@State(Scope.Benchmark) // 共享域
|
||||
public class ToJsonStrJmh {
|
||||
public class BeanToJsonStrJmh {
|
||||
|
||||
private JSONEngine jacksonEngine;
|
||||
private JSONEngine gsonEngine;
|
@ -0,0 +1,48 @@
|
||||
package org.dromara.hutool.json.engine;
|
||||
|
||||
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.openjdk.jmh.annotations.*;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@BenchmarkMode(Mode.AverageTime)//每次执行平均花费时间
|
||||
@Warmup(iterations = 1, time = 1) //预热5次调用
|
||||
@Measurement(iterations = 1, time = 5, 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();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void hutoolJmh() {
|
||||
final String jsonStr = hutoolJSON.toString();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void fastJSONJmh() {
|
||||
final String jsonStr = fastJSON.toString();
|
||||
}
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package org.dromara.hutool.json.engine;
|
||||
|
||||
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.openjdk.jmh.annotations.*;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
/**
|
||||
* JSON将字符串解析为树结构的性能对比测试
|
||||
*
|
||||
* @author looly
|
||||
*/
|
||||
@BenchmarkMode(Mode.AverageTime)//每次执行平均花费时间
|
||||
@Warmup(iterations = 1, time = 1) //预热5次调用
|
||||
@Measurement(iterations = 1, time = 5, 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);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user