Skip to content

fastjson_benchmark

温绍锦 edited this page Oct 5, 2022 · 37 revisions

1. 测试环境

1.1 各个库的版本

  • fastjson2 2.0.15
  • fastjson1 1.0.83
  • jackson 2.13.4
  • gson 2.9.1
  • kryo 5.3.0
  • hessian 4.0.66

都是截止测试时间(2022-09-10)各个库的最新版本

1.2 服务器环境

测试的服务器基于阿里云最新代ECS,包括x64架构的Intel和AMD处理器,ARM架构的AltraMax和阿里云平头哥的倚天处理器。

cs.c7a.xlarge
ecs.c7a.xlarge
ecs.c6r.xlarge
ecs.g8m.xlarge

ecs.c7.xlarge

这个是阿里云当前代标准型ECS,处理器型号 Intel Xeon(Ice Lake) Platinum 8369B,4核,8G内存 image

ecs.c7a.xlarge

这个是阿里云当前代标准型ECS,处理器型号 AMD EPYC™ Milan 7T83,4核,8G内存 image

ecs.c6r.xlarge

这个是阿里云上售卖ARM处理器ECS,处理器型号 Ampere Altra / AltraMax,4核,8G内存 image

ecs.g8m.xlarge

这个是阿里云上售卖ARM处理器,处理器型号 Yitian 710,4核,16G内存。这个是阿里云平头哥的倚天710处理器,需要联系客服才能购买。 image

1.3 JDK版本

基于Oracle最新版本的Linux x64/aarch64的JDK版本,下载地址 https://www.oracle.com/java/technologies/

  • oracle-jdk1.8.0_341
  • oracle-jdk-11.0.16
  • oracle-jdk-17.0.4
  • oracle-jdk-18.0.2
  • oracle-jdk-19

1.4 测试代码以及运行方式

git clone https://github.com/alibaba/fastjson2
cd fastjson2
git checkout 2.0.13
mvn clean install -Dmaven.test.skip
~/Install/jdk-1.8.0_341/bin/java -cp ~/git/fastjson2/benchmark/target/fastjson2-benchmarks.jar com.alibaba.fastjson2.benchmark.eishay.Eishay
~/Install/jdk-11.0.16/bin/java -cp ~/git/fastjson2/benchmark/target/fastjson2-benchmarks.jar com.alibaba.fastjson2.benchmark.eishay.Eishay
~/Install/jdk-17.0.4/bin/java -cp ~/git/fastjson2/benchmark/target/fastjson2-benchmarks.jar com.alibaba.fastjson2.benchmark.eishay.Eishay
~/Install/jdk-18.0.2/bin/java -cp ~/git/fastjson2/benchmark/target/fastjson2-benchmarks.jar com.alibaba.fastjson2.benchmark.eishay.Eishay
~/Install/jdk-19/bin/java -cp ~/git/fastjson2/benchmark/target/fastjson2-benchmarks.jar com.alibaba.fastjson2.benchmark.eishay.Eishay

2. 测试数据&结果分析

原始数据 https://github.com/alibaba/fastjson2/blob/2.0.15/docs/benchmark/benchmark_2.0.15_raw.md

2.1 EishayParseString

这个是最常用的场景,将JSON格式字符串反序列化为Java对象,这个场景在fastjson中的代码如下:

String str = "...";
Bean bean = JSON.parseJSONObject(str, Bean.class);

这个场景是fastjson1最强的场景;

在ecs.c7.xlarge-oracle-jdk1.8.0_341_x64环境中,只相当于只相当于fastjson2的76.07% ;jackson则只相当于fastjson2的39.15%; gson是fastjson2的33.18%,也就是说这个最常见的反序列化场景,fastjson2的性能差不多是jackson/gson的三倍。

性能排序如下:

// ecs.c7.xlarge-oracle-jdk1.8.0_341_x64

      fastjson2 > fastjson1 > jackson > gson
jdk8  100%        76.07%      39.15%    33.18%
jdk11 100%        73.15%      39.20%    34.59%
jdk17 100%        91.18%      35.55%    33.98%
jdk18 100%        85.52%      36.21%    28.54%
jdk19 100%        86.91%      37.48%    29.09%

fastjson2在JDK11和JDK8的算法不同,导致这个场景下,fastjson2在JDK8的表现比JDK11下好,JDK17/18/19由于JDK自身性能优化,使得JDK17/18/19性能更好。JDK 18/19比JDK 17性能显著提升。

下面是不同CPU不同JDK下的性能比较表格,第1列是阿里云机器规格,第2列是JDK的版本信息,第3列到第6列是不同库的JMH的Throughput数据,越大越好。 第4列到第6列中的百分比是Throughput相比fastjson2的比,比如第三列的百分比是fastjson1/fastjson2的百分数表示。

aliyun ecs spec jdk version fastjson2 fastjson1 jackson gson
ecs.c7.xlarge oracle-jdk1.8.0_341_x64 1320.023 1004.199 (76.07%) 516.82 (39.15%) 438.048 (33.18%)
ecs.c7.xlarge oracle-jdk-11.0.16_x64 1260.936 922.37 (73.15%) 494.333 (39.2%) 436.156 (34.59%)
ecs.c7.xlarge oracle-jdk-17.0.4_x64 1369.386 1248.583 (91.18%) 486.845 (35.55%) 465.337 (33.98%)
ecs.c7.xlarge oracle-jdk-18.0.2_x64 1395.646 1193.6 (85.52%) 505.428 (36.21%) 398.287 (28.54%)
ecs.c7.xlarge oracle-jdk-19_x64 1388.353 1206.599 (86.91%) 520.411 (37.48%) 403.886 (29.09%)
ecs.c7a.xlarge oracle-jdk1.8.0_341_x64 1229.526 937.784 (76.27%) 477.09 (38.8%) 419.502 (34.12%)
ecs.c7a.xlarge oracle-jdk-11.0.16_x64 1134.796 881.615 (77.69%) 470.933 (41.5%) 447.406 (39.43%)
ecs.c7a.xlarge oracle-jdk-17.0.4_x64 1230.035 1263.187 (102.7%) 519.04 (42.2%) 516.882 (42.02%)
ecs.c7a.xlarge oracle-jdk-18.0.2_x64 1244.473 1108.575 (89.08%) 457.552 (36.77%) 428.208 (34.41%)
ecs.c7a.xlarge oracle-jdk-19_x64 1185.852 1177.811 (99.32%) 427.541 (36.05%) 400.906 (33.81%)
ecs.c6r.xlarge oracle-jdk1.8.0_341_aarch64 868.197 712.0 (82.01%) 376.827 (43.4%) 312.498 (35.99%)
ecs.c6r.xlarge oracle-jdk-11.0.16_aarch64 849.48 736.375 (86.69%) 370.608 (43.63%) 316.399 (37.25%)
ecs.c6r.xlarge oracle-jdk-17.0.4_aarch64 877.366 1002.943 (114.31%) 380.077 (43.32%) 315.595 (35.97%)
ecs.c6r.xlarge oracle-jdk-18.0.2_aarch64 866.233 981.352 (113.29%) 364.075 (42.03%) 297.333 (34.32%)
ecs.c6r.xlarge oracle-jdk-19_aarch64 879.06 980.02 (111.48%) 355.808 (40.48%) 293.269 (33.36%)
ecs.g8m.xlarge oracle-jdk1.8.0_341_aarch64 984.462 771.687 (78.39%) 375.51 (38.14%) 354.297 (35.99%)
ecs.g8m.xlarge oracle-jdk-11.0.16_aarch64 1124.362 807.192 (71.79%) 413.95 (36.82%) 375.63 (33.41%)
ecs.g8m.xlarge oracle-jdk-17.0.4_aarch64 1128.561 1114.467 (98.75%) 408.066 (36.16%) 353.178 (31.29%)
ecs.g8m.xlarge oracle-jdk-18.0.2_aarch64 1136.232 1144.034 (100.69%) 391.845 (34.49%) 306.8 (27%)
ecs.g8m.xlarge oracle-jdk-19_aarch64 1144.135 1133.377 (99.06%) 407.734 (35.64%) 350.49 (30.63%)

2.2 EishayParseStringPretty

这个场景是将格式化过的JSON格式字符串反序列化为Java对象,这个场景fastjson1的parse算法不擅长的。这个场景fastjson2表现仍然非常好。 这个场景在ecs.c7.xlarge-oracle-jdk1.8.0_341_x64环境下,fastjson1的性能是fastjson2的27.15,jackson的性能是fastjson2的50.08%,gson是fastjson2的42.89%%.

这个场景在fastjson中的代码如下:

// 这里输入的是格式化过后的json字符串
String str = "{\n" +
        "\t\"id\":123\n" +
        "}";
Bean bean = JSON.parseJSONObject(str, Bean.class);

性能排序分别如下:

// ecs.c7.xlarge-oracle-jdk1.8.0_341_x64

      fastjson2 > jackson > gson  > fastjson1
jdk8  100%        50.08%    42.89%  28.58%
jdk11 100%        49.77%    45.50%  26.26%
jdk17 100%        48.29%    44.78%  31.33%
jdk18 100%        48.54%    42.21%  31.31%
jdk19 100%        46.65%    37.54%  28.57%

下面是不同CPU不同JDK下的性能比较表格,第1列是阿里云机器规格,第2列是JDK的版本信息,第3列到第6列是不同库的JMH的Throughput数据,越大越好。 第4列到第6列中的百分比是Throughput相比fastjson2的比,比如第三列的百分比是fastjson1/fastjson2的百分数表示。

aliyun ecs spec jdk version fastjson2 fastjson1 jackson gson
ecs.c7.xlarge oracle-jdk1.8.0_341_x64 940.464 268.822 (28.58%) 480.396 (51.08%) 403.352 (42.89%)
ecs.c7.xlarge oracle-jdk-11.0.16_x64 907.039 238.149 (26.26%) 451.425 (49.77%) 412.661 (45.5%)
ecs.c7.xlarge oracle-jdk-17.0.4_x64 946.804 296.672 (31.33%) 457.174 (48.29%) 423.958 (44.78%)
ecs.c7.xlarge oracle-jdk-18.0.2_x64 933.883 292.434 (31.31%) 453.311 (48.54%) 394.177 (42.21%)
ecs.c7.xlarge oracle-jdk-19_x64 988.807 282.481 (28.57%) 461.237 (46.65%) 371.236 (37.54%)
ecs.c7a.xlarge oracle-jdk1.8.0_341_x64 899.074 253.441 (28.19%) 437.011 (48.61%) 387.316 (43.08%)
ecs.c7a.xlarge oracle-jdk-11.0.16_x64 788.548 243.077 (30.83%) 450.466 (57.13%) 431.675 (54.74%)
ecs.c7a.xlarge oracle-jdk-17.0.4_x64 832.478 292.051 (35.08%) 465.183 (55.88%) 448.118 (53.83%)
ecs.c7a.xlarge oracle-jdk-18.0.2_x64 797.438 271.997 (34.11%) 423.528 (53.11%) 405.21 (50.81%)
ecs.c7a.xlarge oracle-jdk-19_x64 775.867 259.75 (33.48%) 412.325 (53.14%) 379.722 (48.94%)
ecs.c6r.xlarge oracle-jdk1.8.0_341_aarch64 685.863 208.277 (30.37%) 341.671 (49.82%) 280.223 (40.86%)
ecs.c6r.xlarge oracle-jdk-11.0.16_aarch64 666.075 208.054 (31.24%) 348.135 (52.27%) 295.71 (44.4%)
ecs.c6r.xlarge oracle-jdk-17.0.4_aarch64 662.112 240.315 (36.3%) 341.715 (51.61%) 289.012 (43.65%)
ecs.c6r.xlarge oracle-jdk-18.0.2_aarch64 663.461 228.336 (34.42%) 334.155 (50.37%) 279.273 (42.09%)
ecs.c6r.xlarge oracle-jdk-19_aarch64 665.034 221.462 (33.3%) 327.114 (49.19%) 271.69 (40.85%)
ecs.g8m.xlarge oracle-jdk1.8.0_341_aarch64 758.599 220.671 (29.09%) 349.148 (46.03%) 321.927 (42.44%)
ecs.g8m.xlarge oracle-jdk-11.0.16_aarch64 739.847 208.959 (28.24%) 275.784 (37.28%) 348.357 (47.09%)
ecs.g8m.xlarge oracle-jdk-17.0.4_aarch64 799.211 264.967 (33.15%) 372.393 (46.6%) 281.256 (35.19%)
ecs.g8m.xlarge oracle-jdk-18.0.2_aarch64 673.976 255.994 (37.98%) 363.218 (53.89%) 291.881 (43.31%)
ecs.g8m.xlarge oracle-jdk-19_aarch64 804.179 241.845 (30.07%) 360.28 (44.8%) 327.409 (40.71%)

2.3 EishayParseTreeString

这个场景是将JSON格式字符串反序列化为JSONObject,这也是一个最常见的场景之一,这个场景在fastjson中的代码如下:

String str = "...";
JSONObject jsonObject = JSON.parseJSONObject(str);

fastjson2在这个场景也表现了远超fastjson1/jackson/gson的性能。 在不同的环境中,fastjson1和jackson都只能相当于fastjson2性能的40%~60%,gson则更差一些。

性能排序如下:

// ecs.c7.xlarge-oracle-jdk1.8.0_341_x64

      fastjson2 > fastjson1 > jackson  > gson
jdk8  100%        49.11%      50.54%     33.08%
jdk11 100%        50.21%      53.41%     39.81%
jdk17 100%        50.87%      46.07%     28.98%
jdk18 100%        46.97%      43.04%     28.24%
jdk19 100%        49.03%      48.16%     30.10%

下面是不同CPU不同JDK下的性能比较表格,第1列是阿里云机器规格,第2列是JDK的版本信息,第3列到第6列是不同库的JMH的Throughput数据,越大越好。 第4列到第6列中的百分比是Throughput相比fastjson2的比,比如第三列的百分比是fastjson1/fastjson2的百分数表示。

aliyun ecs spec jdk version fastjson2 fastjson1 jackson gson
ecs.c7.xlarge oracle-jdk1.8.0_341_x64 1024.392 503.042 (49.11%) 517.736 (50.54%) 338.902 (33.08%)
ecs.c7.xlarge oracle-jdk-11.0.16_x64 805.647 404.521 (50.21%) 430.305 (53.41%) 320.71 (39.81%)
ecs.c7.xlarge oracle-jdk-17.0.4_x64 1091.463 555.186 (50.87%) 502.859 (46.07%) 316.269 (28.98%)
ecs.c7.xlarge oracle-jdk-18.0.2_x64 1129.728 530.668 (46.97%) 486.195 (43.04%) 319.082 (28.24%)
ecs.c7.xlarge oracle-jdk-19_x64 1049.116 514.414 (49.03%) 505.254 (48.16%) 315.743 (30.1%)
ecs.c7a.xlarge oracle-jdk1.8.0_341_x64 862.609 429.35 (49.77%) 504.816 (58.52%) 337.247 (39.1%)
ecs.c7a.xlarge oracle-jdk-11.0.16_x64 795.942 399.425 (50.18%) 463.128 (58.19%) 346.302 (43.51%)
ecs.c7a.xlarge oracle-jdk-17.0.4_x64 1046.206 524.425 (50.13%) 510.47 (48.79%) 385.776 (36.87%)
ecs.c7a.xlarge oracle-jdk-18.0.2_x64 1012.796 527.705 (52.1%) 479.785 (47.37%) 370.695 (36.6%)
ecs.c7a.xlarge oracle-jdk-19_x64 946.727 457.025 (48.27%) 487.359 (51.48%) 362.186 (38.26%)
ecs.c6r.xlarge oracle-jdk1.8.0_341_aarch64 585.244 300.751 (51.39%) 357.66 (61.11%) 260.348 (44.49%)
ecs.c6r.xlarge oracle-jdk-11.0.16_aarch64 547.117 315.049 (57.58%) 348.928 (63.78%) 262.462 (47.97%)
ecs.c6r.xlarge oracle-jdk-17.0.4_aarch64 614.709 357.832 (58.21%) 381.573 (62.07%) 259.427 (42.2%)
ecs.c6r.xlarge oracle-jdk-18.0.2_aarch64 622.427 338.896 (54.45%) 360.341 (57.89%) 254.512 (40.89%)
ecs.c6r.xlarge oracle-jdk-19_aarch64 603.085 337.843 (56.02%) 367.375 (60.92%) 253.413 (42.02%)
ecs.g8m.xlarge oracle-jdk1.8.0_341_aarch64 563.961 311.415 (55.22%) 334.18 (59.26%) 274.652 (48.7%)
ecs.g8m.xlarge oracle-jdk-11.0.16_aarch64 664.295 363.316 (54.69%) 378.628 (57%) 300.685 (45.26%)
ecs.g8m.xlarge oracle-jdk-17.0.4_aarch64 811.114 393.275 (48.49%) 428.312 (52.81%) 255.93 (31.55%)
ecs.g8m.xlarge oracle-jdk-18.0.2_aarch64 825.188 378.532 (45.87%) 407.692 (49.41%) 286.672 (34.74%)
ecs.g8m.xlarge oracle-jdk-19_aarch64 802.728 361.135 (44.99%) 432.181 (53.84%) 294.64 (36.7%)

2.4 EishayParseUTF8Bytes

这个是将UTF8格式的byte数组反序列化为Java对象,这个场景在缓存和RPC场景中常用。

这个场景在fastjson中的代码如下:

byte[] utf8Bytes = ...;
Bean bean = JSON.parseJSONObject(utf8Bytes, Bean.class);

这个场景fastjson2同样表现出了卓越的性能;在JDK8下,fastjson1和jackson的性能分别之后fastjson2的71.23%和53.76%; gson不直接支持输入utf8Bytes,需要先构造字符串,性能只有fastjson2的28.3%。

性能排序如下:

// ecs.c7.xlarge-oracle-jdk1.8.0_341_x64

      fastjson2 > fastjson1 > jackson  > gson
jdk8  100%        70.10%      52.33%     27.40%
jdk11 100%        68.61%      51.24%     28.40%
jdk17 100%        80.81%      48.80%     26.27%
jdk18 100%        78.10%      47.57%     27.35%
jdk19 100%        77.79%      46.37%     26.03%

下面是不同CPU不同JDK下的性能比较表格,第1列是阿里云机器规格,第2列是JDK的版本信息,第3列到第6列是不同库的JMH的Throughput数据,越大越好。 第4列到第6列中的百分比是Throughput相比fastjson2的比,比如第三列的百分比是fastjson1/fastjson2的百分数表示。

aliyun ecs spec jdk version fastjson2 fastjson1 jackson gson
ecs.c7.xlarge oracle-jdk1.8.0_341_x64 1122.673 787.029 (70.1%) 587.517 (52.33%) 307.605 (27.4%)
ecs.c7.xlarge oracle-jdk-11.0.16_x64 1095.4 751.501 (68.61%) 561.254 (51.24%) 311.108 (28.4%)
ecs.c7.xlarge oracle-jdk-17.0.4_x64 1186.373 958.667 (80.81%) 578.934 (48.8%) 311.608 (26.27%)
ecs.c7.xlarge oracle-jdk-18.0.2_x64 1164.931 909.776 (78.1%) 554.179 (47.57%) 318.601 (27.35%)
ecs.c7.xlarge oracle-jdk-19_x64 1193.509 928.43 (77.79%) 553.385 (46.37%) 310.644 (26.03%)
ecs.c7a.xlarge oracle-jdk1.8.0_341_x64 865.765 735.685 (84.98%) 548.45 (63.35%) 313.526 (36.21%)
ecs.c7a.xlarge oracle-jdk-11.0.16_x64 1035.644 696.77 (67.28%) 534.159 (51.58%) 356.966 (34.47%)
ecs.c7a.xlarge oracle-jdk-17.0.4_x64 1044.923 873.254 (83.57%) 605.101 (57.91%) 358.599 (34.32%)
ecs.c7a.xlarge oracle-jdk-18.0.2_x64 902.821 860.388 (95.3%) 569.01 (63.03%) 357.377 (39.58%)
ecs.c7a.xlarge oracle-jdk-19_x64 948.099 813.541 (85.81%) 536.198 (56.56%) 345.395 (36.43%)
ecs.c6r.xlarge oracle-jdk1.8.0_341_aarch64 741.939 550.488 (74.2%) 457.982 (61.73%) 238.885 (32.2%)
ecs.c6r.xlarge oracle-jdk-11.0.16_aarch64 764.826 583.751 (76.32%) 439.756 (57.5%) 252.718 (33.04%)
ecs.c6r.xlarge oracle-jdk-17.0.4_aarch64 787.438 757.793 (96.24%) 425.687 (54.06%) 251.758 (31.97%)
ecs.c6r.xlarge oracle-jdk-18.0.2_aarch64 781.739 737.323 (94.32%) 425.827 (54.47%) 247.366 (31.64%)
ecs.c6r.xlarge oracle-jdk-19_aarch64 790.354 737.685 (93.34%) 419.229 (53.04%) 248.95 (31.5%)
ecs.g8m.xlarge oracle-jdk1.8.0_341_aarch64 840.027 658.393 (78.38%) 408.892 (48.68%) 244.654 (29.12%)
ecs.g8m.xlarge oracle-jdk-11.0.16_aarch64 1026.276 684.56 (66.7%) 475.162 (46.3%) 283.144 (27.59%)
ecs.g8m.xlarge oracle-jdk-17.0.4_aarch64 997.658 509.233 (51.04%) 429.713 (43.07%) 238.001 (23.86%)
ecs.g8m.xlarge oracle-jdk-18.0.2_aarch64 1014.895 793.172 (78.15%) 459.629 (45.29%) 282.807 (27.87%)
ecs.g8m.xlarge oracle-jdk-19_aarch64 1063.483 741.682 (69.74%) 453.99 (42.69%) 236.084 (22.2%)

2.5 EishayWriteString

这个是Java对象序列化成字符串, 这个是序列化最常用的场景之一。这个场景在fastjson中的代码如下:

Bean bean = ...;
String str = JSON.toJSONString(bean);

在这个最常用的序列化场景,fastjson2表现出远超fastjson1/jackson/gson的性能;这个场景gson表现很差,只有fastjson2的1/5

性能排序如下:

// ecs.c7.xlarge-oracle-jdk1.8.0_341_x64

      fastjson2 > jackson >   fastjson1  > gson
jdk8  100%        59.92%      37.64%       19.58%
jdk11 100%        60.15%      38.98%       18.50%
jdk17 100%        62.52%      37.94%       12.44%
jdk18 100%        53.64%      35.57%       12.10%
jdk19 100%        55.36%      36.74%       12.35%

下面是不同CPU不同JDK下的性能比较表格,第1列是阿里云机器规格,第2列是JDK的版本信息,第3列到第6列是不同库的JMH的Throughput数据,越大越好。 第4列到第6列中的百分比是Throughput相比fastjson2的比,比如第三列的百分比是fastjson1/fastjson2的百分数表示。

aliyun ecs spec jdk version fastjson2 fastjson1 jackson gson
ecs.c7.xlarge oracle-jdk1.8.0_341_x64 1628.025 612.727 (37.64%) 975.517 (59.92%) 318.714 (19.58%)
ecs.c7.xlarge oracle-jdk-11.0.16_x64 1498.846 584.242 (38.98%) 901.584 (60.15%) 277.215 (18.5%)
ecs.c7.xlarge oracle-jdk-17.0.4_x64 1657.325 628.848 (37.94%) 1036.213 (62.52%) 206.112 (12.44%)
ecs.c7.xlarge oracle-jdk-18.0.2_x64 1681.715 598.113 (35.57%) 902.056 (53.64%) 203.571 (12.1%)
ecs.c7.xlarge oracle-jdk-19_x64 1648.782 605.79 (36.74%) 912.733 (55.36%) 203.599 (12.35%)
ecs.c7a.xlarge oracle-jdk1.8.0_341_x64 1547.95 583.256 (37.68%) 905.673 (58.51%) 339.203 (21.91%)
ecs.c7a.xlarge oracle-jdk-11.0.16_x64 1507.674 616.329 (40.88%) 900.552 (59.73%) 283.74 (18.82%)
ecs.c7a.xlarge oracle-jdk-17.0.4_x64 1505.885 614.163 (40.78%) 916.059 (60.83%) 323.794 (21.5%)
ecs.c7a.xlarge oracle-jdk-18.0.2_x64 1431.697 556.693 (38.88%) 813.363 (56.81%) 299.256 (20.9%)
ecs.c7a.xlarge oracle-jdk-19_x64 1399.599 558.493 (39.9%) 803.304 (57.4%) 292.435 (20.89%)
ecs.c6r.xlarge oracle-jdk1.8.0_341_aarch64 1075.257 442.939 (41.19%) 569.243 (52.94%) 269.937 (25.1%)
ecs.c6r.xlarge oracle-jdk-11.0.16_aarch64 1126.647 415.963 (36.92%) 612.196 (54.34%) 251.168 (22.29%)
ecs.c6r.xlarge oracle-jdk-17.0.4_aarch64 1110.947 436.534 (39.29%) 634.159 (57.08%) 189.407 (17.05%)
ecs.c6r.xlarge oracle-jdk-18.0.2_aarch64 1116.855 398.796 (35.71%) 575.919 (51.57%) 190.152 (17.03%)
ecs.c6r.xlarge oracle-jdk-19_aarch64 1110.623 392.086 (35.3%) 597.186 (53.77%) 187.808 (16.91%)
ecs.g8m.xlarge oracle-jdk1.8.0_341_aarch64 1279.804 470.294 (36.75%) 637.485 (49.81%) 327.978 (25.63%)
ecs.g8m.xlarge oracle-jdk-11.0.16_aarch64 1272.129 480.478 (37.77%) 680.737 (53.51%) 300.405 (23.61%)
ecs.g8m.xlarge oracle-jdk-17.0.4_aarch64 1284.791 489.293 (38.08%) 620.568 (48.3%) 178.196 (13.87%)
ecs.g8m.xlarge oracle-jdk-18.0.2_aarch64 1218.32 347.043 (28.49%) 554.791 (45.54%) 176.069 (14.45%)
ecs.g8m.xlarge oracle-jdk-19_aarch64 1294.147 437.114 (33.78%) 528.188 (40.81%) 197.685 (15.28%)

2.6 EishayWriteUTF8Bytes

这个是Java对象序列化成UTF格式的byte数组, 这个是最常用的场景之一,在缓存和RPC场景常用。gson不直接支持,需要先序列化为String再转换为UTF8格式的byte数组。

这个场景在fastjson中的代码如下:

Bean bean = ...;
byte[] utf8Bytes = JSON.toJSONBytes(bean);

在这个最常用的序列化场景,fastjson2表现出远超fastjson1/jackson/gson的性能;这个场景gson表现很差,只有fastjson2的1/5不到,在JDK 17下差距更大。

性能排序如下:

// ecs.c7.xlarge-oracle-jdk1.8.0_341_x64

      fastjson2 > jackson >   fastjson1  > gson
jdk8  100%        58.10%      36.75%       19.04%
jdk11 100%        50.67%      32.61%       16.69%
jdk17 100%        49.10%      28.96%       10.49%
jdk18 100%        48.27%      27.80%       10.47%
jdk19 100%        49.35%      27.67%       10.34%

下面是不同CPU不同JDK下的性能比较表格,第1列是阿里云机器规格,第2列是JDK的版本信息,第3列到第6列是不同库的JMH的Throughput数据,越大越好。 第4列到第6列中的百分比是Throughput相比fastjson2的比,比如第三列的百分比是fastjson1/fastjson2的百分数表示。

aliyun ecs spec jdk version fastjson2 fastjson1 jackson gson
ecs.c7.xlarge oracle-jdk1.8.0_341_x64 1542.518 566.808 (36.75%) 896.168 (58.1%) 293.629 (19.04%)
ecs.c7.xlarge oracle-jdk-11.0.16_x64 1659.306 541.071 (32.61%) 840.729 (50.67%) 277.005 (16.69%)
ecs.c7.xlarge oracle-jdk-17.0.4_x64 1964.89 569.104 (28.96%) 964.704 (49.1%) 206.146 (10.49%)
ecs.c7.xlarge oracle-jdk-18.0.2_x64 1940.193 539.343 (27.8%) 936.592 (48.27%) 203.104 (10.47%)
ecs.c7.xlarge oracle-jdk-19_x64 1940.835 537.066 (27.67%) 957.826 (49.35%) 200.666 (10.34%)
ecs.c7a.xlarge oracle-jdk1.8.0_341_x64 1441.81 556.694 (38.61%) 796.929 (55.27%) 302.503 (20.98%)
ecs.c7a.xlarge oracle-jdk-11.0.16_x64 1664.37 525.595 (31.58%) 805.813 (48.42%) 279.377 (16.79%)
ecs.c7a.xlarge oracle-jdk-17.0.4_x64 1740.044 549.792 (31.6%) 846.86 (48.67%) 320.707 (18.43%)
ecs.c7a.xlarge oracle-jdk-18.0.2_x64 1679.533 482.787 (28.75%) 772.376 (45.99%) 276.5 (16.46%)
ecs.c7a.xlarge oracle-jdk-19_x64 1601.005 508.874 (31.78%) 792.242 (49.48%) 283.559 (17.71%)
ecs.c6r.xlarge oracle-jdk1.8.0_341_aarch64 1143.247 395.229 (34.57%) 564.72 (49.4%) 232.465 (20.33%)
ecs.c6r.xlarge oracle-jdk-11.0.16_aarch64 1240.914 377.907 (30.45%) 557.564 (44.93%) 246.246 (19.84%)
ecs.c6r.xlarge oracle-jdk-17.0.4_aarch64 1191.594 393.589 (33.03%) 590.895 (49.59%) 184.782 (15.51%)
ecs.c6r.xlarge oracle-jdk-18.0.2_aarch64 1180.948 392.129 (33.2%) 583.529 (49.41%) 185.145 (15.68%)
ecs.c6r.xlarge oracle-jdk-19_aarch64 1173.616 377.516 (32.17%) 554.243 (47.23%) 189.422 (16.14%)
ecs.g8m.xlarge oracle-jdk1.8.0_341_aarch64 1368.141 469.883 (34.34%) 660.369 (48.27%) 292.09 (21.35%)
ecs.g8m.xlarge oracle-jdk-11.0.16_aarch64 1436.204 420.938 (29.31%) 432.712 (30.13%) 304.352 (21.19%)
ecs.g8m.xlarge oracle-jdk-17.0.4_aarch64 1431.837 471.682 (32.94%) 676.724 (47.26%) 201.686 (14.09%)
ecs.g8m.xlarge oracle-jdk-18.0.2_aarch64 1477.482 436.408 (29.54%) 519.773 (35.18%) 207.275 (14.03%)
ecs.g8m.xlarge oracle-jdk-19_aarch64 1434.942 455.268 (31.73%) 617.044 (43%) 182.911 (12.75%)

2.7 EishayWriteStringTree

这个长江是JSONObject序列化成字符串, 这个是序列化最常用的场景之一。这个场景在fastjson中的代码如下:

JSONObject jsonObject = ...;
String str = jsonObject.toJSONString();

在这个最常用的序列化场景,fastjson2表现出远超fastjson1/jackson/gson的性能;这个场景gson表现很差,只有fastjson2的1/3

性能排序如下:

// ecs.c7.xlarge-oracle-jdk1.8.0_341_x64

      fastjson2 > jackson >   fastjson1  > gson
jdk8  100%        70.53%      62.26%       36.59%
jdk11 100%        76.55%      59.97%       29.99%
jdk17 100%        67.88%      64.60%       20.00%
jdk18 100%        71.11%      66.11%       20.59%
jdk19 100%        68.75%      64.34%       20.59%
aliyun ecs spec jdk version fastjson2 fastjson1 jackson gson
ecs.c7.xlarge oracle-jdk1.8.0_341_x64 1319.448 821.463 (62.26%) 930.632 (70.53%) 482.848 (36.59%)
ecs.c7.xlarge oracle-jdk-11.0.16_x64 1278.101 766.471 (59.97%) 978.442 (76.55%) 383.311 (29.99%)
ecs.c7.xlarge oracle-jdk-17.0.4_x64 1248.264 806.4 (64.6%) 847.335 (67.88%) 250.808 (20.09%)
ecs.c7.xlarge oracle-jdk-18.0.2_x64 1214.845 803.076 (66.11%) 863.923 (71.11%) 254.775 (20.97%)
ecs.c7.xlarge oracle-jdk-19_x64 1232.079 792.473 (64.32%) 847.075 (68.75%) 253.668 (20.59%)
ecs.c7a.xlarge oracle-jdk1.8.0_341_x64 1168.834 796.983 (68.19%) 818.824 (70.05%) 426.338 (36.48%)
ecs.c7a.xlarge oracle-jdk-11.0.16_x64 1103.669 760.369 (68.89%) 866.86 (78.54%) 354.722 (32.14%)
ecs.c7a.xlarge oracle-jdk-17.0.4_x64 1094.031 819.069 (74.87%) 866.802 (79.23%) 428.089 (39.13%)
ecs.c7a.xlarge oracle-jdk-18.0.2_x64 1050.412 789.896 (75.2%) 834.016 (79.4%) 409.174 (38.95%)
ecs.c7a.xlarge oracle-jdk-19_x64 1083.27 719.327 (66.4%) 846.663 (78.16%) 406.81 (37.55%)
ecs.c6r.xlarge oracle-jdk1.8.0_341_aarch64 717.419 492.895 (68.7%) 574.373 (80.06%) 331.246 (46.17%)
ecs.c6r.xlarge oracle-jdk-11.0.16_aarch64 768.666 505.407 (65.75%) 577.801 (75.17%) 281.126 (36.57%)
ecs.c6r.xlarge oracle-jdk-17.0.4_aarch64 766.132 507.299 (66.22%) 545.404 (71.19%) 213.026 (27.81%)
ecs.c6r.xlarge oracle-jdk-18.0.2_aarch64 765.887 514.326 (67.15%) 532.837 (69.57%) 215.826 (28.18%)
ecs.c6r.xlarge oracle-jdk-19_aarch64 770.845 499.984 (64.86%) 564.606 (73.25%) 210.631 (27.32%)
ecs.g8m.xlarge oracle-jdk1.8.0_341_aarch64 967.262 584.524 (60.43%) 647.759 (66.97%) 413.261 (42.72%)
ecs.g8m.xlarge oracle-jdk-11.0.16_aarch64 1044.727 626.387 (59.96%) 736.406 (70.49%) 360.298 (34.49%)
ecs.g8m.xlarge oracle-jdk-17.0.4_aarch64 1010.276 628.604 (62.22%) 689.518 (68.25%) 213.635 (21.15%)
ecs.g8m.xlarge oracle-jdk-18.0.2_aarch64 995.901 629.639 (63.22%) 685.909 (68.87%) 226.797 (22.77%)
ecs.g8m.xlarge oracle-jdk-19_aarch64 991.672 619.929 (62.51%) 687.333 (69.31%) 226.537 (22.84%)

2.8 EishayWriteBinary

这个场景是将Java对象序列化为二进制的byte数组,是缓存序列化和RPC场景使用序列化协议的典型场景。

fastson2内置支持二进制格式jsonb,jsonb是fastjson2为了RPC场景设计的序列化和反序列化设置的高性能序列化协议。

jsonb有两种映射方式,将Java对象映射为类似JSONObject的KV格式,也可以映射类似JSONArray的数组格式。

fastjson2JSONBArrayMapping的代码如下:

Bean bean = ...;
byte[] jsonbBytes = JSONB.toBytes(bean, JSONWriter.Feature.BeanToArray);

fastjson2JSONB的代码如下:

Bean bean = ...;
byte[] jsonbBytes = JSONB.toBytes(bean);

fastjson2UTF8Bytes的代码如下:

Bean bean = ...;
byte[] utf8Bytes = JSON.toJSONBytes(bean);

这个场景下,jsonb的性能非常好,在JDK 11/17下性能更好,hessina和java内置序列化都表现很差。

性能排序如下:

// ecs.c7.xlarge-oracle-jdk1.8.0_341_x64

      fastjson2JSONB > fastjson2UTF8Bytes> hessian > javaSerialize
jdk8  100%             71.28%              15.78%    10.34%
jdk11 100%             63.92%              13.19%     7.75%
jdk17 100%             53.13%               9.44%     6.63%
jdk18 100%             56.16%               9.50%     6.93%
jdk19 100%             56.09%               9.50%     6.23%

下面是不同CPU不同JDK下的性能比较表格,第1列是阿里云机器规格,第2列是JDK的版本信息,第3列到第6列是不同库的JMH的Throughput数据,越大越好。 第4列到第6列中的百分比是Throughput相比fastjson2的比,比如第三列的百分比是fastjson1/fastjson2的百分数表示。

aliyun ecs spec jdk version fastjson2JSONB fastjson2UTF8Bytes hessian javaSerialize
ecs.c7.xlarge oracle-jdk1.8.0_341_x64 2148.347 1531.363 (71.28%) 339.071 (15.78%) 222.241 (10.34%)
ecs.c7.xlarge oracle-jdk-11.0.16_x64 2536.022 1620.997 (63.92%) 334.581 (13.19%) 196.662 (7.75%)
ecs.c7.xlarge oracle-jdk-17.0.4_x64 3375.077 1894.283 (56.13%) 318.638 (9.44%) 223.779 (6.63%)
ecs.c7.xlarge oracle-jdk-18.0.2_x64 3419.643 1920.304 (56.16%) 324.877 (9.5%) 236.868 (6.93%)
ecs.c7.xlarge oracle-jdk-19_x64 3393.854 1903.59 (56.09%) 322.527 (9.5%) 211.602 (6.23%)
ecs.c7a.xlarge oracle-jdk1.8.0_341_x64 1862.576 1387.351 (74.49%) 382.54 (20.54%) 215.459 (11.57%)
ecs.c7a.xlarge oracle-jdk-11.0.16_x64 2466.077 1708.984 (69.3%) 387.238 (15.7%) 224.947 (9.12%)
ecs.c7a.xlarge oracle-jdk-17.0.4_x64 2779.975 1774.61 (63.84%) 385.193 (13.86%) 249.072 (8.96%)
ecs.c7a.xlarge oracle-jdk-18.0.2_x64 2727.657 1624.962 (59.57%) 375.871 (13.78%) 238.437 (8.74%)
ecs.c7a.xlarge oracle-jdk-19_x64 2735.581 1594.441 (58.29%) 353.076 (12.91%) 252.787 (9.24%)
ecs.c6r.xlarge oracle-jdk1.8.0_341_aarch64 1356.178 1128.731 (83.23%) 295.093 (21.76%) 180.776 (13.33%)
ecs.c6r.xlarge oracle-jdk-11.0.16_aarch64 1951.14 1216.836 (62.37%) 312.456 (16.01%) 184.829 (9.47%)
ecs.c6r.xlarge oracle-jdk-17.0.4_aarch64 1947.389 1210.047 (62.14%) 302.615 (15.54%) 181.923 (9.34%)
ecs.c6r.xlarge oracle-jdk-18.0.2_aarch64 1906.848 1157.47 (60.7%) 300.276 (15.75%) 188.655 (9.89%)
ecs.c6r.xlarge oracle-jdk-19_aarch64 1926.694 1194.592 (62%) 304.289 (15.79%) 180.769 (9.38%)
ecs.g8m.xlarge oracle-jdk1.8.0_341_aarch64 1654.976 1369.491 (82.75%) 346.682 (20.95%) 207.901 (12.56%)
ecs.g8m.xlarge oracle-jdk-11.0.16_aarch64 2430.439 1458.663 (60.02%) 347.231 (14.29%) 219.179 (9.02%)
ecs.g8m.xlarge oracle-jdk-17.0.4_aarch64 2306.656 1377.041 (59.7%) 347.787 (15.08%) 208.211 (9.03%)
ecs.g8m.xlarge oracle-jdk-18.0.2_aarch64 2494.483 1444.006 (57.89%) 328.193 (13.16%) 220.988 (8.86%)
ecs.g8m.xlarge oracle-jdk-19_aarch64 1617.92 1495.444 (92.43%) 340.406 (21.04%) 221.413 (13.69%)

2.9 EishayParseBinary

这个场景是将二进制的byte数组反序列化为Java对象,是缓存序列化和RPC场景使用序列化协议的典型场景。

fastjson2JSONBArrayMapping的代码如下:

byte[] jsonbBytes = JSONB.toBytes(bean, JSONWriter.Feature.BeanToArray);
Bean bean = JSONB.parseObject(jsonbBytes, Bean.class, JSONReader.Feature.SupportArrayToBean);

fastjson2JSONB的代码如下:

byte[] jsonbBytes = JSONB.toBytes(bean);
Bean bean = JSONB.parseObject(jsonbBytes, Bean.class);

fastjson2UTF8Bytes的代码如下:

byte[] utf8Bytes = JSON.toJSONBytes(bean);
Bean bean = JSON.parseObject(utf8Bytes, Bean.class);

性能排序如下:

// ecs.c7.xlarge-oracle-jdk1.8.0_341_x64

这个场景下,jsonb的性能非常好,在JDK 11/17下性能更好,hessina表现很差,java内置序列化都表现极差。

      fastjson2JSONB > fastjson2UTF8Bytes> hessian > javaSerialize
jdk8  100%             69.38%              17.00%    2.73%
jdk11 100%             48.46%              10.13%    1.94%
jdk17 100%             45.45%               9.43%    1.96%
jdk18 100%             43.51%               8.76%    1.83%
jdk19 100%             45.83%               8.47%    1.75%

下面是不同CPU不同JDK下的性能比较表格,第1列是阿里云机器规格,第2列是JDK的版本信息,第3列到第6列是不同库的JMH的Throughput数据,越大越好。 第4列到第6列中的百分比是Throughput相比fastjson2的比,比如第三列的百分比是fastjson1/fastjson2的百分数表示。

aliyun ecs spec jdk version fastjson2JSONB fastjson2UTF8Bytes hessian javaSerialize
ecs.c7.xlarge oracle-jdk1.8.0_341_x64 1670.725 1159.104 (69.38%) 283.954 (17%) 45.549 (2.73%)
ecs.c7.xlarge oracle-jdk-11.0.16_x64 2495.092 1209.058 (48.46%) 252.867 (10.13%) 48.526 (1.94%)
ecs.c7.xlarge oracle-jdk-17.0.4_x64 2722.632 1237.478 (45.45%) 256.64 (9.43%) 53.233 (1.96%)
ecs.c7.xlarge oracle-jdk-18.0.2_x64 2848.273 1239.177 (43.51%) 249.642 (8.76%) 52.094 (1.83%)
ecs.c7.xlarge oracle-jdk-19_x64 2775.256 1271.933 (45.83%) 235.048 (8.47%) 48.616 (1.75%)
ecs.c7a.xlarge oracle-jdk1.8.0_341_x64 1223.559 855.714 (69.94%) 228.647 (18.69%) 38.273 (3.13%)
ecs.c7a.xlarge oracle-jdk-11.0.16_x64 2152.441 1077.133 (50.04%) 245.918 (11.43%) 44.802 (2.08%)
ecs.c7a.xlarge oracle-jdk-17.0.4_x64 2767.375 1058.6 (38.25%) 246.159 (8.9%) 52.289 (1.89%)
ecs.c7a.xlarge oracle-jdk-18.0.2_x64 2684.85 1033.888 (38.51%) 238.553 (8.89%) 48.603 (1.81%)
ecs.c7a.xlarge oracle-jdk-19_x64 2550.169 965.844 (37.87%) 227.919 (8.94%) 38.476 (1.51%)
ecs.c6r.xlarge oracle-jdk1.8.0_341_aarch64 1291.709 755.823 (58.51%) 188.712 (14.61%) 37.185 (2.88%)
ecs.c6r.xlarge oracle-jdk-11.0.16_aarch64 1683.948 795.127 (47.22%) 177.161 (10.52%) 36.409 (2.16%)
ecs.c6r.xlarge oracle-jdk-17.0.4_aarch64 1849.821 819.537 (44.3%) 205.808 (11.13%) 39.014 (2.11%)
ecs.c6r.xlarge oracle-jdk-18.0.2_aarch64 1799.521 816.559 (45.38%) 172.351 (9.58%) 39.019 (2.17%)
ecs.c6r.xlarge oracle-jdk-19_aarch64 1788.926 811.842 (45.38%) 172.142 (9.62%) 35.206 (1.97%)
ecs.g8m.xlarge oracle-jdk1.8.0_341_aarch64 1478.455 857.263 (57.98%) 204.802 (13.85%) 42.098 (2.85%)
ecs.g8m.xlarge oracle-jdk-11.0.16_aarch64 2075.398 1065.187 (51.32%) 194.874 (9.39%) 39.475 (1.9%)
ecs.g8m.xlarge oracle-jdk-17.0.4_aarch64 2294.572 1086.885 (47.37%) 209.535 (9.13%) 38.386 (1.67%)
ecs.g8m.xlarge oracle-jdk-18.0.2_aarch64 2220.858 1105.781 (49.79%) 189.986 (8.55%) 43.005 (1.94%)
ecs.g8m.xlarge oracle-jdk-19_aarch64 2206.528 1150.675 (52.15%) 205.672 (9.32%) 39.964 (1.81%)
Clone this wiki locally