天天看點

fastjson 字元串轉map_序列化與反序列化——FastJSON、Jackson、Gson性能測試

背景

起因是公司原先用的是阿裡開源的FastJSON,大家用的也比較順手,但是在出現了兩次嚴重的漏洞後,公司決定放棄FastJSON,使用其他序列化/反序列化工具。考慮大家常用的無非就是FastJSON、Jackson和Gson這三種,是以上司讓我調研一下到底是使用Gson還是Jackson。

關于漏洞這裡我多說一句,建議大家還真得把這個事情當一個事情。我之前就被漏洞坑了一把,在一台linux伺服器上部署了6.5版本的confluence,後來阿裡雲也發緊急通知了,告知趕緊更新,然而我并沒有當一回事,過了沒兩天我就中招了,這台機器被挖礦了,什麼都幹不了,隻能是初始化系統,好一頓折騰~

秉持着嚴謹且負責的精神,這個事情還是要好好做一下子的~

測試結果預告

fastjson 字元串轉map_序列化與反序列化——FastJSON、Jackson、Gson性能測試

FastJSON、Gson、Jackson序列化性能比較.png

fastjson 字元串轉map_序列化與反序列化——FastJSON、Jackson、Gson性能測試

FastJSON、Gson、Jackson反序列化性能比較.png

前置準備工作

為了營造一個相對準确、互不影響的測試環境,我們需要有以下限制(要求):

同一台機器,即我的mac:

MacBook Pro (16-inch, 2019) 處理器 2.3 GHz 八核Intel Core i9 記憶體16 GB 2667 MHz DDR4硬碟 1T
           

JVM相關參數配置:

#JDK版本jdk1.8.0_151#運作參數-Xms4g -Xmx4g -XX:+UseG1GC
           
  • 三種JSON引擎版本:
com.fasterxml.jackson.core.jackson:2.11.1com.google.code.gson:2.8.6com.alibaba.fastjson:1.2.72
           

序列化與反序列化

搞事情之前,我們先來複習一下什麼是序列化與反序列化:

序列化:把Java對象轉換為位元組序列的過程。

反序列化:把位元組序列恢複為Java對象的過程。

對象的序列化主要有兩種用途:

持久化對象:把對象的位元組序列永久地儲存到硬碟上,通常存放在一個檔案中;

網絡傳輸對象:在網絡上傳送對象的位元組序列。

代碼測試

建立一個對象,包含Boolean、Integer、Long、Double、Date、String、ArrayList、HashMap等資料類型,構造方法即初始化好對象,友善後面使用:

package com.performance.json.bean;import com.performance.json.DataBuilder;import lombok.Data;import java.io.Serializable;import java.util.Date;import java.util.List;import java.util.Map;import java.util.Random;/** * 資料對象 * @author java架構設計 * @date 2020/7/17 5:19 下午 **/@Datapublic class DataBean implements Serializable {    private static final long serialVersionUID = 6453520211539229613L;    private Boolean aBoolean;    private Integer integer;    private Long aLong;    private Double aDouble;    private Date date;    private String string;    private List list;    private Map objectMap;    public DataBean() {        Random random = new Random();        this.aBoolean = random.nextBoolean();        this.integer = random.nextInt();        this.aLong = random.nextLong();        this.aDouble = random.nextDouble();        this.date = new Date();        this.string = DataBuilder.randomString();        this.list = DataBuilder.randomList();        this.objectMap = DataBuilder.randomMap();    }}
           

其中randomString()、randomList()、randomMap()方法如下:

package com.performance.json;import java.util.*;/** * 資料構造器 * * @author java架構設計 * @date 2020/7/17 5:30 下午 **/public class DataBuilder {    private static final String[] chars = new String[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b",            "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w",            "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",            "S", "T", "U", "V", "W", "X", "Y", "Z" };    private static final Random random = new Random();    /**     * 生成100以内随機長度的字元串     * @return String     */    public static String randomString(){        int len = random.nextInt(100);        StringBuilder sb = new StringBuilder(len);        for (int i = 0; i < len; i++) {            sb.append(chars[random.nextInt(62)]);        }        return sb.toString();    }    /**     * 生成100以内的字元串集合     * @return List     */    public static List randomList() {        int len = random.nextInt(100);        ArrayList list = new ArrayList<>(len);        for (int i = 0; i < len; i++) {            list.add(randomString());        }        return list;    }/**     * 生成100以内的Map     * @return Map     */    public static Map randomMap() {        int len = random.nextInt(100);        Map map = new HashMap<>(len);        for (int i = 0; i < len; i++) {            switch (i % 6) {                case 0:                    map.put("key" + i, random.nextBoolean());                    break;                case 1:                    map.put("key" + i, random.nextInt());                    break;                case 2:                    map.put("key" + i, random.nextLong());                    break;                case 3:                    map.put("key" + i, random.nextDouble());                    break;                case 4:                    map.put("key" + i, new Date());                    break;                case 5:                    map.put("key" + i, randomString());                    break;                default:                    break;            }        }        return map;    }}
           

序列化測試代碼:

fastjson 字元串轉map_序列化與反序列化——FastJSON、Jackson、Gson性能測試

測試對象數分别為1、100、1000、10000、100000個,對象都是預先生成好,然後再依次執行三種JSON引擎執行序列化操作,輸出結果:

#1個對象FastJSON耗時:84msGson耗時:13msJackson耗時:51ms#10個對象FastJSON耗時:87msGson耗時:21msJackson耗時:54ms#100個對象FastJSON耗時:110msGson耗時:43msJackson耗時:69ms#1000個對象FastJSON耗時:138msGson耗時:114msJackson耗時:98ms#10000個對象FastJSON耗時:290msGson耗時:467msJackson耗時:230ms#10000個對象FastJSON耗時:1657msGson耗時:3340msJackson耗時:1456ms
           

生成條形圖:

fastjson 字元串轉map_序列化與反序列化——FastJSON、Jackson、Gson性能測試

FastJSON、Gson、Jackson序列化性能比較.png

從上圖來分析,可以知道的是,在資料量較少(1、10、100)的時候,Gson的性能最優,且優勢較明顯,當對象數量在1000的時候,Jackson的性能開始上來了,是以在對象數量在1~1000的時候,性能比拼:Gson>Jackson>FastJSON。

但是當數量達到10000、100000級别的時候,Gson的性能下降的比較厲害,而FastJson和Jackson依舊保持着它們的快,性能比較:Jackson>FastJSON>Gson。

反序列化測試代碼:

fastjson 字元串轉map_序列化與反序列化——FastJSON、Jackson、Gson性能測試
public static void main(String[] args) {        testDeSerialize(1);//        testDeSerialize(10);//        testDeSerialize(100);//        testDeSerialize(1000);//        testDeSerialize(10000);//        testDeSerialize(100000);}
           

測試對象數分别為1、100、1000、10000、100000個,對象都是預先序列化好,然後再依次執行三種JSON引擎執行反序列化操作,輸出結果:

#1次FastJSON耗時:20msGson耗時:5msJackson耗時:28ms#10次FastJSON耗時:29msGson耗時:15msJackson耗時:34ms#100次FastJSON耗時:48msGson耗時:46msJackson耗時:51ms#1000次FastJSON耗時:131msGson耗時:214msJackson耗時:146ms#10000次FastJSON耗時:652msGson耗時:893msJackson耗時:720ms#100000次FastJSON耗時:5985msGson耗時:7357msJackson耗時:5232ms
           

生成條形圖:

fastjson 字元串轉map_序列化與反序列化——FastJSON、Jackson、Gson性能測試

FastJSON、Gson、Jackson反序列化性能比較.png

反序列化性能測試,

在對象數量為1、10、100的時候,Gson的性能最好,Jackson次之,性能排序為:Gson>Jackson>FastJson;

在對象數量為1000、10000的時候,Gson的性能下降比較明顯,這個量級下性能排序為:FastJson>Jackson>Gson。

在對象數量為10w的時候,Jackson反超FastJson,性能排序為:Jackson>FastJson>Gson。

總結

當資料量較小的時候(1~100),建議使用 Gson;當資料量較大的時候,建議使用Jackson;在大資料量的時候,雖然FastJson優勢上來了,但是因為有漏洞,不得不放棄。

在做這個性能測試之前,也是在網上查找了一下大家是怎麼做這個性能測試的,有的很複雜,有的卻也簡單,最終我還是選擇了一種簡單的測試方式,在盡可能的規避其他因素的影響,比如:提前生成好資料、三種引擎測試的樣本資料一緻、1~10w次的次數測試的時候都是重新運作main方法,盡量避免虛拟機的影響。

最終得出的性能測試結果與網上參考文章的測試結果一緻。

如果您覺得上述測試過程有問題,歡迎在評論區指正~

繼續閱讀