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、结果
在这里插入图片描述

如需使用 码流小、性能高的序列化框架,可采用Google出品的Protobuf数据协议 https://blog.csdn.net/qq_21033663/article/details/106171711

猜你喜欢

转载自blog.csdn.net/qq_21033663/article/details/105747361
今日推荐