Kryo序列化和ProroStruff序列化性能比较
Posted 杨龙飞的博客
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Kryo序列化和ProroStruff序列化性能比较相关的知识,希望对你有一定的参考价值。
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 ,大小是:284
Kryo反序列化的时间是:22
protoStruff序列化的时间是:86 ,大小是:286
protostruff反序列化的时间是:10
SIZE = 100
测试结果:
kryo序列化的时间是:8 ,大小是:285
Kryo反序列化的时间是:27
protoStruff序列化的时间是:88 ,大小是:287
protostruff反序列化的时间是:12
SIZE = 1000
测试结果:
kryo序列化的时间是:37 ,大小是:286
Kryo反序列化的时间是:60
protoStruff序列化的时间是:145 ,大小是:288
protostruff反序列化的时间是:28
SIZE = 10000;
测试结果:
kryo序列化的时间是:165 ,大小是:287
Kryo反序列化的时间是:138
protoStruff序列化的时间是:185 ,大小是:289
protostruff反序列化的时间是:65
SIZE =100000;
测试结果:
kryo序列化的时间是:427 ,大小是:288
Kryo反序列化的时间是:332
protoStruff序列化的时间是:173 ,大小是:290
protostruff反序列化的时间是:154
SIZE = 1000000
测试结果:
kryo序列化的时间是:8383 ,大小是:289
Kryo反序列化的时间是:6668
protoStruff序列化的时间是:5783 ,大小是:291
protostruff反序列化的时间是:1303
结论:
序列化:
在序列化对象数量较少的情况下,Kryo序列化比protostruff 快.(SIZE 10000左右)
在序列化对象数在100000个情况下,Kryo序列化速度迅速上升,protostruff比Kryo快.
反序列化:
不管在对象数量少与多的情况下,Kyro的反序列化的速度都低于protostruff.
大小:
同样属性大小的JavaBean序列化后,Kryo序列化后的size小于protostruff.
以上是关于Kryo序列化和ProroStruff序列化性能比较的主要内容,如果未能解决你的问题,请参考以下文章
深入浅出Spring原理及实战「开发实战系列」采用protostuff和kryo高性能序列化框架实现RedisTemplate的序列化组件