跳至主要內容

JMH JSON 基准测试

大约 3 分钟

JMH JSON 基准测试

1 前言

阅读本文大约需要 3 分钟。

fastjson 究竟有多快?json 开源组件如何选项?为什么 SpringBoot2 json 组件选择了 jackson?

java-json-benchmark 项目open in new window 使用 JMHopen in new window 对各种 Java Json 库的吞吐性能进行了基准测试。它涵盖了以下库:

序号json 组件 (Mvnrepository 链接)版本 (2020.03)版本 (2021.05)对比 (+/=)
1jacksonopen in new window2.10.22.12.3+
2gensonopen in new window1.61.6=
3fastjsonopen in new window1.2.621.2.76+
4gsonopen in new window2.8.62.8.6=
5org.jsonopen in new window2009021120210307+
6javax.jsonopen in new window1.1.41.1.4=
7json-ioopen in new window4.12.04.12.0=
8flexjsonopen in new window3.33.3=
9boonopen in new window0.340.34=
10json-smartopen in new window2.32.4.7+
11johnzonopen in new window1.2.31.2.11+
12logansquareopen in new window1.3.71.3.7=
13dsl-jsonopen in new window1.9.51.9.8+
14json-simpleopen in new window1.1.11.1.1=
15nanojsonopen in new window1.41.7+
16jodd-jsonopen in new window5.1.36.0.3+
17moshiopen in new window1.9.21.12.0+
18tapestryopen in new window5.4.55.7.2+
19jsoniteropen in new window0.9.230.9.23=
20minimal-jsonopen in new window0.9.50.9.5=
21mjsonopen in new window1.4.11.4.1=
22underscoreopen in new window1.521.66+
23yassonopen in new window1.0.61.0.9+
24javassistopen in new window3.26.0-GA3.28.0-GA+
25purejsonopen in new window1.0.11.0.1=

1.1 TODO

New Jakarta JSON Binding 2.0.0 API with new jakarta.* namespace:

<!-- https://mvnrepository.com/artifact/org.glassfish/jakarta.json -->
<dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>jakarta.json</artifactId>
    <version>2.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/jakarta.json/jakarta.json-api -->
<dependency>
    <groupId>jakarta.json</groupId>
    <artifactId>jakarta.json-api</artifactId>
    <version>2.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/jakarta.json.bind/jakarta.json.bind-api -->
<dependency>
    <groupId>jakarta.json.bind</groupId>
    <artifactId>jakarta.json.bind-api</artifactId>
    <version>2.0.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.eclipse/yasson -->
<dependency>
    <groupId>org.eclipse</groupId>
    <artifactId>yasson</artifactId>
    <version>2.0.2</version>
    <scope>test</scope>
</dependency>

1.2 Users 对象

用例:基本类型,字符串,列表和简单 POJO。

1.3 Clients 对象

用例: 基本类型,字符串,列表和简单 POJO,数组,枚举,UUID,LocalDate。

注意: 由于缺少对某些评估类型的支持,因此使用此模型测试的库较少。

2 Benchmark 结果 (2021.05.13)

users-deser-1

users-ser-1

clients-deser-1

clients-ser-1

Benchmark 配置

  • CPU: Inter(R) Core(TM) i5-8250U CPU @1.60GHz 1.80GHz
  • RAM: 8.00GB
  • OS: Windows 10 2004
# JMH version: 1.29
# VM version: JDK 1.8.0_202, Java HotSpot(TM) 64-Bit Server VM, 25.202-b08
# VM invoker: C:\Program Files\Java\jre1.8.0_202\bin\java.exe
# VM options: -XX:+AggressiveOpts -Xms2g -Xmx2g
# Blackhole mode: full + dont-inline hint
# Warmup: 5 iterations, 10 s each
# Measurement: 10 iterations, 3 s each
# Timeout: 10 min per iteration
# Threads: 16 threads, will synchronize iterations
# Benchmark mode: Throughput, ops/time

3 Benchmark 结果 (2020.03.01)

用例:基本类型,字符串,列表和简单 POJO。

users-deser-1

users-ser-1

clients-deser-1

clients-ser-1

Benchmark 配置

测试是在 Amazon EC2 c5.xlargeopen in new window (4 vCPU, 8 GiB RAM) 上运行的,JMH 配置:

# JMH version: 1.23
# VM version: JDK 1.8.0_242, OpenJDK 64-Bit Server VM, 25.242-b08
# VM invoker: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.242.b08-0.amzn2.0.1.x86_64/jre/bin/java
# VM options: -XX:+AggressiveOpts -Xms2g -Xmx2g
# Warmup: 5 iterations, 10 s each
# Measurement: 10 iterations, 3 s each
# Timeout: 10 min per iteration
# Threads: 16 threads, will synchronize iterations
# Benchmark mode: Throughput, ops/time
  1. JSON-Bopen in new window: JSON-B is a standard binding layer for converting Java objects to/from JSON messages. It defines a default mapping algorithm for converting existing Java classes to JSON, while enabling developers to customize the mapping process through the use of Java annotations.
  2. JSON-Popen in new window: JSON Processing (JSON-P) is a Java API to process (for e.g. parse, generate, transform and query) JSON messages. It produces and consumes JSON text in a streaming fashion (similar to StAX API for XML) and allows to build a Java object model for JSON text using API classes (similar to DOM API for XML).
  3. Gradle Plugin shadowopen in new window: Gradle plugin to create fat/uber JARs, apply file transforms, and relocate packages for applications and libraries. Gradle version of Maven's Shade.
  4. Maven Plugin shadeopen in new window: Apache Maven Shade Plugin.
  5. LoganSquareopen in new window: Note that Gradle is the only supported build configuration for LoganSquare. To add the library to your app's build.gradle file.
  6. Javapoetopen in new window: A Java API for generating .java source files.
  7. Apache EChartsopen in new window: An Open Source JavaScript Visualization Library.
  8. JMHopen in new window: JMH is a Java harness for building, running, and analysing nano/micro/milli/macro benchmarks written in Java and other languages targetting the JVM.

(全文完)