最近有场景需要数据支撑json的toJsonString方法和java原生的toString方法的运行速度,因此选用了JMH测试工具。
以下代码大致意思是:初始化一个list集合,放入100个对象,然后遍历这个集合,调用fastjson的toJsonString方法和对象的toString方法。
这100个对象属性值拼接了list的下标,防止字符串常量池中存在相同的字符。
实体类代码:
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;import java.util.Date;@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class TestDto {private Integer id;private String name;private String address;private Date date;private boolean aBoolean;private double aDouble;private float aFloat;}
jmh测试代码如下:
import com.alibaba.fastjson.JSON;
import org.openjdk.jmh.annotations.*;
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;
import org.openjdk.jmh.runner.options.TimeValue;import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@State(Scope.Thread)
public class JmhTest {private List<TestDto> list;/*** 初始化准备工作*/@Setup(Level.Iteration)public void setUp() {this.list = new ArrayList<>();for (int i = 0; i < 100; i++) {TestDto testDto = TestDto.builder().id(i).name("xiaoming" + i).address("beijing" + i).date(new Date()).aDouble(i).aBoolean(true).aFloat(i).build();list.add(testDto);}}/*** 方法1*/@Benchmarkpublic void toJSONStringTest() {for (TestDto testDto : list) {String res = JSON.toJSONString(testDto);}}/*** 方法2*/@Benchmarkpublic void toStringTest() {for (TestDto testDto : list) {String res = testDto.toString();}}public static void main(String[] args) throws RunnerException {final Options opts = new OptionsBuilder().include(JmhTest.class.getSimpleName()).forks(1)// 多少次测量迭代.measurementIterations(10)// 每次测量迭代需要多长时间.measurementTime(TimeValue.microseconds(1000000L))// 进行多少次预热迭代.warmupIterations(1)// 每次预热迭代需要多长时间.warmupTime(TimeValue.microseconds(1000000L)).build();new Runner(opts).run();}
}
运行结果如下:
# JMH version: 1.19
# VM version: JDK 1.8.0_282, VM 25.282-b08
# VM invoker: D:\corretto-1.8.0_282\jre\bin\java.exe
# VM options: -javaagent:D:\IntelliJ IDEA 2020.3.3\lib\idea_rt.jar=56086:D:\IntelliJ IDEA 2020.3.3\bin -Dfile.encoding=UTF-8
# Warmup: 1 iterations, 1000000 us each
# Measurement: 10 iterations, 1000000 us each
# Timeout: 10 min per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Average time, time/op
# Benchmark: com.honor.wpshowdemo.serverone.test.JmhTest.toJSONStringTest# Run progress: 0.00% complete, ETA 00:00:22
# Fork: 1 of 1
# Warmup Iteration 1: 977.533 us/op
Iteration 1: 66.566 us/op
Iteration 2: 61.137 us/op
Iteration 3: 66.597 us/op
Iteration 4: 54.270 us/op
Iteration 5: 54.927 us/op
Iteration 6: 53.950 us/op
Iteration 7: 54.212 us/op
Iteration 8: 54.328 us/op
Iteration 9: 54.574 us/op
Iteration 10: 54.343 us/opResult "com.honor.wpshowdemo.serverone.test.JmhTest.toJSONStringTest":57.490 ±(99.9%) 7.924 us/op [Average](min, avg, max) = (53.950, 57.490, 66.597), stdev = 5.241CI (99.9%): [49.566, 65.414] (assumes normal distribution)# JMH version: 1.19
# VM version: JDK 1.8.0_282, VM 25.282-b08
# VM invoker: D:\corretto-1.8.0_282\jre\bin\java.exe
# VM options: -javaagent:D:\IntelliJ IDEA 2020.3.3\lib\idea_rt.jar=56086:D:\IntelliJ IDEA 2020.3.3\bin -Dfile.encoding=UTF-8
# Warmup: 1 iterations, 1000000 us each
# Measurement: 10 iterations, 1000000 us each
# Timeout: 10 min per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Average time, time/op
# Benchmark: com.honor.wpshowdemo.serverone.test.JmhTest.toStringTest# Run progress: 50.00% complete, ETA 00:00:13
# Fork: 1 of 1
# Warmup Iteration 1: 105.282 us/op
Iteration 1: 80.594 us/op
Iteration 2: 64.168 us/op
Iteration 3: 63.157 us/op
Iteration 4: 63.548 us/op
Iteration 5: 63.345 us/op
Iteration 6: 64.605 us/op
Iteration 7: 63.592 us/op
Iteration 8: 63.363 us/op
Iteration 9: 64.166 us/op
Iteration 10: 63.580 us/opResult "com.honor.wpshowdemo.serverone.test.JmhTest.toStringTest":65.412 ±(99.9%) 8.094 us/op [Average](min, avg, max) = (63.157, 65.412, 80.594), stdev = 5.353CI (99.9%): [57.318, 73.505] (assumes normal distribution)# Run complete. Total time: 00:00:25Benchmark Mode Cnt Score Error Units
JmhTest.toJSONStringTest avgt 10 57.490 ± 7.924 us/op
JmhTest.toStringTest avgt 10 65.412 ± 8.094 us/opProcess finished with exit code 0
结论如下:
对象属性只有7个的时候,fastjson的toJsonString方法明显优于对像的toString方法。
ps:
不过,当对象属性更少时,toString方法是优于toJosnString方法,这个验证过程没有发出来,大家也可以自行验证。