java序列化框架碼流、性能對比

本文對序列化框架:JDK、Kryo、Hession、FST、Fastjson、Gson進行對比,對比維度包括序列化後碼流大小、10w次序列化反序列化性能耗時。
1、依賴jar包

		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.18.8</version>
			<scope>provided</scope>
		</dependency>

		<!-- 序列化框架 -->
		<dependency>
			<groupId>hessian</groupId>
			<artifactId>hessian</artifactId>
			<version>3.0.1</version>
		</dependency>
		<dependency>
			<groupId>io.protostuff</groupId>
			<artifactId>protostuff-core</artifactId>
			<version>1.6.0</version>
		</dependency>
		<dependency>
			<groupId>io.protostuff</groupId>
			<artifactId>protostuff-collectionschema</artifactId>
			<version>1.6.0</version>
		</dependency>
		<dependency>
			<groupId>io.protostuff</groupId>
			<artifactId>protostuff-runtime</artifactId>
			<version>1.6.0</version>
		</dependency>
		<dependency>
			<groupId>io.protostuff</groupId>
			<artifactId>protostuff-api</artifactId>
			<version>1.6.0</version>
		</dependency>
		<dependency>
			<groupId>com.esotericsoftware</groupId>
			<artifactId>kryo</artifactId>
			<version>4.0.0</version>
		</dependency>

		<dependency>
			<groupId>de.ruedigermoeller</groupId>
			<artifactId>fst</artifactId>
			<version>2.57</version>
		</dependency>
		<dependency>
			<groupId>com.google.code.gson</groupId>
			<artifactId>gson</artifactId>
			<version>2.8.5</version>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.62</version>
		</dependency>

2、測試實體

package com.zhanghao.test.serialize;

import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

@Getter
@Setter
public class SerializeBO implements Serializable {
    private static final long serialVersionUID = -3109861179106944873L;
    private Integer a = 1;
    private Long b = 2L;
    private Byte c = 3;
    private Short d = 4;
    private Double e = 5.0;
    private Float f = 6.0F;
    private Boolean g  = true;
//    private Character h = 'h';
}

3、測試

package com.zhanghao.test.serialize;

import com.alibaba.fastjson.JSON;
import com.caucho.hessian.io.HessianInput;
import com.caucho.hessian.io.HessianOutput;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.google.gson.Gson;
import org.nustaq.serialization.FSTConfiguration;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class SeralizeTest {

    private static FSTConfiguration configuration = FSTConfiguration.createDefaultConfiguration();

    public static void main(String[] args) throws Exception {
        SerializeBO serializeBO = new SerializeBO();
        System.out.println("JDK:" + countJdkByte(serializeBO) + " Byte");
        System.out.println("Kryo:" + countKryo(serializeBO) + " Byte");
        System.out.println("Hession:" + countHessian(serializeBO) + " Byte");
        System.out.println("FST:" + countFST(serializeBO) + " Byte");
        System.out.println("Fastjson:" + countFastjson(serializeBO) + " Byte");
        System.out.println("Gson:" + countGson(serializeBO) + " Byte");

        long jdkTime = System.currentTimeMillis();
        for (int i=0; i<100000; i++) {
            byte[] bytes = seralizeJdkByte(serializeBO);
            unSeralizeJdkByte(bytes);
        }
        System.out.println("JDK 耗時:" + (System.currentTimeMillis() - jdkTime) + "ms");

        long kyroTime = System.currentTimeMillis();
        for (int i=0; i<100000; i++) {
            byte[] bytes = serializeKryo(serializeBO);
            unSerializeKryo(bytes);
        }
        System.out.println("Kryo 耗時:" + (System.currentTimeMillis() - kyroTime) + "ms");

        long hessionTime = System.currentTimeMillis();
        for (int i=0; i<100000; i++) {
            byte[] bytes = serializeHessian(serializeBO);
            unSerializeHessian(bytes);
        }
        System.out.println("Hessian 耗時:" + (System.currentTimeMillis() - hessionTime) + "ms");

        long fstTime = System.currentTimeMillis();
        for (int i=0; i<100000; i++) {
            byte[] bytes = serializeFST(serializeBO);
            unSerializeFST(bytes);
        }
        System.out.println("FST 耗時:" + (System.currentTimeMillis() - fstTime) + "ms");

        long fastjsonTime = System.currentTimeMillis();
        for (int i=0; i<100000; i++) {
            String str = serializeFastjson(serializeBO);
            unSerializeFastjson(str);
        }
        System.out.println("Fastjson 耗時:" + (System.currentTimeMillis() - fastjsonTime) + "ms");

        long gsonTime = System.currentTimeMillis();
        for (int i=0; i<100000; i++) {
            String str = serializeGson(serializeBO);
            unSerializeGson(str);
        }
        System.out.println("Gson 耗時:" + (System.currentTimeMillis() - gsonTime) + "ms");
    }

    /** JDK */
    private static int countJdkByte(Object obj) throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream os = new ObjectOutputStream(baos);
        os.writeObject(obj);
        int count = baos.toByteArray().length;
        os.close();
        baos.close();
        return count;
    }

    private static byte[] seralizeJdkByte(Object obj) throws Exception {
        byte[] bytes = null;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream os = new ObjectOutputStream(baos);
        os.writeObject(obj);
        bytes = baos.toByteArray();
        os.close();
        baos.close();
        return bytes;
    }

    private static Object unSeralizeJdkByte(byte[] bytes) throws Exception {
        ObjectInputStream oit = new ObjectInputStream(new ByteArrayInputStream(bytes));
        Object object = oit.readObject();
        oit.close();
        return object;
    }

    /** Kryo */
    private static int countKryo(Object obj) {
        Kryo kryo = new Kryo();
        kryo.setReferences(true);
        Output oOutput = new Output(1, 102400);
        kryo.writeObject(oOutput, obj);
        byte[] buff = oOutput.toBytes();
        oOutput.close();
        return buff.length;
    }

    private static byte[] serializeKryo(Object obj) {
        Kryo kryo = new Kryo();
        kryo.setReferences(true);
        Output oOutput = new Output(1, 102400);
        kryo.writeObject(oOutput, obj);
        oOutput.close();
        return oOutput.toBytes();
    }

    private static Object unSerializeKryo(byte[] bytes) {
        Kryo kryo = new Kryo();
        Input input = new Input(bytes);
        Object obj = kryo.readObject(input, ArrayList.class);
        input.close();
        return obj;
    }

    /** Hessian */
    private static int countHessian(Object obj) throws Exception {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        HessianOutput ho = new HessianOutput(os);
        ho.writeObject(obj);
        byte[] buffer = os.toByteArray();
        os.close();
        return buffer.length;
    }

    private static byte[] serializeHessian(Object obj) throws Exception {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        HessianOutput ho = new HessianOutput(os);
        ho.writeObject(obj);
        os.close();
        return os.toByteArray();
    }

    private static Object unSerializeHessian(byte[] bytes) throws Exception {
        ByteArrayInputStream is = new ByteArrayInputStream(bytes);
        HessianInput hi = new HessianInput(is);
        Object obj = hi.readObject();
        is.close();
        return obj;
    }

    /** FST */
    private static int countFST(Object obj) throws Exception {
        return configuration.asByteArray(obj).length;
    }

    private static byte[] serializeFST(Object obj) throws Exception {
        return configuration.asByteArray(obj);
    }

    private static Object unSerializeFST(byte[] bytes) throws Exception {
        return configuration.asObject(bytes);
    }

    /** Fastjson */
    private static int countFastjson(Object obj) {
        String jsonStr = JSON.toJSONString(obj);
        return jsonStr.getBytes().length;
    }

    private static String serializeFastjson(Object obj) {
        return JSON.toJSONString(obj);
    }

    private static Object unSerializeFastjson(String str) {
        return JSON.parseObject(str, Object.class);
    }

    /** Gson */
    private static int countGson(Object obj) throws Exception {
        Gson gson = new Gson();
        return gson.toJson(obj).getBytes().length;
    }

    private static String serializeGson(Object obj) throws Exception {
        Gson gson = new Gson();
        return gson.toJson(obj);
    }

    private static Object unSerializeGson(String json) throws Exception {
        Gson gson = new Gson();
        return gson.fromJson(json, Object.class);
    }

}

4、結果
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章