Kryo序列化和ProroStruff序列化性能比较

来源:互联网 发布:码字赚钱的软件 编辑:程序博客网 时间:2024/05/17 01:06

JavaBean类

package SeriazleCompare;/** * Created by yang on 16-12-13. */public class Student {    private String name;    private int age;    private int time;    private String xupt;    public Student(){    }    public Student(String name, int age, int time, String xupt) {        this.name = name;        this.age = age;        this.time = time;        this.xupt = xupt;    }    @Override    public String toString() {        return "Student{" +                "name='" + name + '\'' +                ", age=" + age +                ", time=" + time +                ", xupt='" + xupt + '\'' +                '}';    }}

KryoSerialization序列化

package SeriazleCompare;import com.esotericsoftware.kryo.Kryo;import com.esotericsoftware.kryo.Registration;import com.esotericsoftware.kryo.io.Input;import com.esotericsoftware.kryo.io.Output;/** * Created by yang on 16-11-22. */public class KryoSerialization {    private Kryo kryo;    private Registration registration = null;    private Class<?> t;    public KryoSerialization(){        kryo = new Kryo();        kryo.setReferences(true);    }    public void register(Class<?> T){        //注册类        t = T;        registration = kryo.register(t);    }    public byte[] Serialize(Object object){        Output output = null;        output = new Output(1,4096);        kryo.writeObject(output,object);        byte[] bt = output.toBytes();        output.flush();        return bt;    }    public <t> t Deserialize(byte[] bt){        Input input = null;        input = new Input(bt);        t res = (t)kryo.readObject(input,registration.getType());        input.close();        return res;    }}

ProtoStruff

package SeriazleCompare;import com.dyuproject.protostuff.LinkedBuffer;import com.dyuproject.protostuff.ProtostuffIOUtil;import com.dyuproject.protostuff.Schema;import com.dyuproject.protostuff.runtime.RuntimeSchema;import org.objenesis.Objenesis;import org.objenesis.ObjenesisStd;import java.util.Map;import java.util.concurrent.ConcurrentHashMap;public class ProstruffSerialization {    private static Map<Class<?>,Schema<?>> cachedSchema = new ConcurrentHashMap<>();    private static Objenesis objenesis = new ObjenesisStd(true);    public ProstruffSerialization(){}    @SuppressWarnings("unchecked")    private static <T> Schema<T> getSchema(Class<T> cls){        Schema<T> schema = (Schema<T>) cachedSchema.get(cls);        if(schema == null){            schema = RuntimeSchema.createFrom(cls);            if(schema != null){                cachedSchema.put(cls,schema);            }        }        return schema;    }    //序列化    @SuppressWarnings("unchecked")    public static <T> byte[] serializer(T obj){        Class<T> cls = (Class<T>) obj.getClass();        LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);        try{            Schema<T> schema = getSchema(cls);            return ProtostuffIOUtil.toByteArray(obj, schema, buffer);        }catch (Exception e) {            throw new IllegalStateException(e.getMessage(), e);        }finally{            buffer.clear();        }    }    //反序列化    public static <T> T deserializer(byte[] data, Class<T>cls){        try{            T message = (T)objenesis.newInstance(cls);            Schema<T> schema = getSchema(cls);            ProtostuffIOUtil.mergeFrom(data, message, schema);            return message;        }catch (Exception e) {            throw new IllegalStateException(e.getMessage(), e);        }    }}

测试类

package SeriazleCompare;import java.util.ArrayList;import java.util.List;/** * Created by yang on 16-12-13. */public class KryoProstruffTest {    private static ProstruffSerialization protostruff = new ProstruffSerialization();    private static KryoSerialization kryo = new KryoSerialization();    public static int SIZE=10;    public static void main(String[] args) {        List<byte[]> kryoList = new ArrayList<>();        List<byte[]> protoStruffList = new ArrayList<>();        kryo.register(Student.class);        long start = System.currentTimeMillis();        byte[] data=null;        for(int i = 0;i < SIZE; ++i){            Student student = new Student("yang"+i,21,32,"There is no such thing as a great talent without great will - power.There is no such thing as a great talent without great will - power.There is no such thing as a great talent without great will - power.There is no such thing as a great talent without great will - power.");            data = kryo.Serialize(student);            kryoList.add(data);        }        long end = System.currentTimeMillis() - start;        System.out.println("kryo序列化的时间是:"+end+" ,大小是:"+data.length);        long Dstart = System.currentTimeMillis();        Student temp=null;        for(byte[] a:kryoList){            temp = kryo.Deserialize(a);        }        long Dend = System.currentTimeMillis()-Dstart;        System.out.println("Kryo反序列化的时间是:"+Dend+" "+ temp.toString());        long Pstart = System.currentTimeMillis();        for(int i=0; i<SIZE;++i){            Student student = new Student("long"+i,21,32,"There is no such thing as a great talent without great will - power.There is no such thing as a great talent without great will - power.There is no such thing as a great talent without great will - power.There is no such thing as a great talent without great will - power.");            data =  ProstruffSerialization.serializer(student);            protoStruffList.add(data);        }        long Pend = System.currentTimeMillis()-Pstart;        System.out.println("protoStruff序列化的时间是:"+Pend+ " ,大小是:"+ data.length);        long PDstart = System.currentTimeMillis();        for(byte[] a:protoStruffList){            temp = protostruff.deserializer(a,Student.class);        }        long PDend =System.currentTimeMillis()-PDstart;        System.out.println("protostruff反序列化的时间是:"+PDend+" "+temp.toString());    }}

SIZE=10:
测试结果:

kryo序列化的时间是:3 ,大小是:284Kryo反序列化的时间是:22protoStruff序列化的时间是:86 ,大小是:286protostruff反序列化的时间是:10

SIZE = 100
测试结果:

kryo序列化的时间是:8 ,大小是:285Kryo反序列化的时间是:27protoStruff序列化的时间是:88 ,大小是:287protostruff反序列化的时间是:12

SIZE = 1000
测试结果:

kryo序列化的时间是:37 ,大小是:286Kryo反序列化的时间是:60protoStruff序列化的时间是:145 ,大小是:288protostruff反序列化的时间是:28

SIZE = 10000;
测试结果:

kryo序列化的时间是:165 ,大小是:287Kryo反序列化的时间是:138protoStruff序列化的时间是:185 ,大小是:289protostruff反序列化的时间是:65

SIZE =100000;
测试结果:

kryo序列化的时间是:427 ,大小是:288Kryo反序列化的时间是:332protoStruff序列化的时间是:173 ,大小是:290protostruff反序列化的时间是:154

SIZE = 1000000
测试结果:

kryo序列化的时间是:8383 ,大小是:289Kryo反序列化的时间是:6668protoStruff序列化的时间是:5783 ,大小是:291protostruff反序列化的时间是:1303

结论:
序列化:
在序列化对象数量较少的情况下,Kryo序列化比protostruff 快.(SIZE 10000左右) 
在序列化对象数在100000个情况下,Kryo序列化速度迅速上升,protostruff比Kryo快.
反序列化:
不管在对象数量少与多的情况下,Kyro的反序列化的速度都低于protostruff.
大小:
同样属性大小的JavaBean序列化后,Kryo序列化后的size小于protostruff.

0 0