alibaba / fastjson2

🚄 FASTJSON2 is a Java JSON library with excellent performance.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

将String解析成JSONObject这个场景,fastjson2表现没有两倍于fastjson1的性能[QUESTION]

chxuan opened this issue · comments

你好,温少,我在测试将String解析成JSONObject这个场景,fastjson2表现没有两倍于fastjson1的性能,测试环境:MacBook Pro (13-inch, 2017, Four Thunderbolt 3 Ports)
测试json使用的eishay.json
image

https://github.com/alibaba/fastjson2/blob/main/core/src/test/java/com/alibaba/fastjson_perf/eishay/EishayParseTreeStringPretty.java

你跑下这个类的main方法,我刚测试的结果:

Benchmark                               Mode  Cnt     Score   Error   Units
EishayParseTreeStringPretty.fastjson1  thrpt    5   647.006 ± 7.090  ops/ms
EishayParseTreeStringPretty.fastjson2  thrpt    5  1093.328 ± 7.012  ops/ms
EishayParseTreeStringPretty.jackson    thrpt    5   708.005 ± 1.783  ops/ms

结果类似,这个场景都是快60%多一些

@wenshao 温少,如果测试json里面还有嵌套json字符串,在进行解析的时候会反转义,那么fastjson2相较于fastjson的性能提升就更不明显了(提升百分之几到百分之二十),如果json字符串更大(比如10k+)fastjson2还要比fastjson慢百分之几到百分之三十左右,我目前使用fastjson的场景是解析json日志(日志场景特点:1.json字符串较大,3k左右;2.日志里面会有大量转义字符)

如果方便提供脱敏后的样本数据给我,我做下性能验证。我的邮箱 shaojin.wensj(at)alibaba-inc.com

@wenshao 温少,邮箱格式不正确吗?邮件发不了,json测试样例子:https://github.com/chxuan/test/blob/master/test.json

我这边的测试结果是fastjson2的性能略低于fastjson

https://github.com/alibaba/fastjson2/blob/main/core/src/test/java/com/alibaba/fastjson_perf/eishay/Issue206ParseTreeStringPretty.java

Benchmark                                 Mode  Cnt    Score   Error   Units
Issue206ParseTreeStringPretty.fastjson1  thrpt    5  105.304 ± 0.815  ops/ms
Issue206ParseTreeStringPretty.fastjson2  thrpt    5  105.256 ± 0.474  ops/ms
Issue206ParseTreeStringPretty.jackson    thrpt    5  103.431 ± 1.330  ops/ms

看这个测试代码,用jmh测试,都差不多,主要是其中message转义会导致较慢。我本地环境fastjson2还是略快一点点,我后续会看下怎么优化你这个场景。

@wenshao 好的,温少,就是在处理json转义时不管是fastjson还是fastjson2的性能都会急剧下降,温少辛苦了,开源不易。

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.3-SNAPSHOT/

@chxuan 做了一些优化,已经能比fastjson 1.x快大约30%,你用上面链接的2.0.3-SNAPSHOT版本试试看

下面是我在zulu JDK 8下的测试数据

Benchmark                                 Mode  Cnt    Score   Error   Units
Issue206ParseTreeStringPretty.fastjson1  thrpt    5   97.443 ± 0.364  ops/ms
Issue206ParseTreeStringPretty.fastjson2  thrpt    5  130.543 ± 0.859  ops/ms
Issue206ParseTreeStringPretty.jackson    thrpt    5   95.572 ± 2.637  ops/ms

zulu JDK 11的测试数据

Benchmark                                 Mode  Cnt    Score   Error   Units
Issue206ParseTreeStringPretty.fastjson1  thrpt    5   74.009 ± 0.226  ops/ms
Issue206ParseTreeStringPretty.fastjson2  thrpt    5  118.881 ± 2.171  ops/ms