1、运行结果
先看运行结果,本机自行测试,非官方数据,仅作参考 :
初始化数据============================
Student{id='00011', name='Y', age=18, grade='100', teacher=Teacher{id='00001', name='X', age=18}}
原生API序列化============================
序列化后数据长度:239
初始化时间:1ms
序列化10000次花费时间(包含初始化时间):17ms
序列化10000次花费时间(不包含初始化时间):16ms
反序列化后数据:Student{id='00011', name='Y', age=18, grade='100', teacher=Teacher{id='00001', name='X', age=18}}
jackson序列化============================
序列化后数据长度:93
初始化时间:383ms
序列化10000次花费时间(包含初始化时间):538ms
序列化10000次花费时间(不包含初始化时间):155ms
反序列化后数据:Student{id='00011', name='Y', age=18, grade='100', teacher=Teacher{id='00001', name='X', age=18}}
FastJson序列化============================
序列化后数据长度:93
初始化时间:0ms
序列化10000次花费时间(包含初始化时间):204ms
序列化10000次花费时间(不包含初始化时间):204ms
反序列化后数据:Student{id='00011', name='Y', age=18, grade='100', teacher=Teacher{id='00001', name='X', age=18}}
Hessian序列化============================
序列化后数据长度:146
初始化时间:28ms
序列化10000次花费时间(包含初始化时间):33ms
序列化10000次花费时间(不包含初始化时间):5ms
反序列化后数据:Student{id='00011', name='Y', age=18, grade='100', teacher=Teacher{id='00001', name='X', age=18}}
ProtoBuf序列化============================
序列化后数据长度:31
初始化时间:1913ms
序列化10000次花费时间(包含初始化时间):7018ms
序列化10000次花费时间(不包含初始化时间):5105ms
反序列化后数据:Student{id='00011', name='Y', age=18, grade='100', teacher=Teacher{id='00001', name='X', age=18}}
Kyro序列化============================
序列化后数据长度:49
初始化时间:49ms
序列化10000次花费时间(包含初始化时间):725ms
序列化10000次花费时间(不包含初始化时间):676ms
反序列化后数据:Student{id='00011', name='Y', age=18, grade='100', teacher=Teacher{id='00001', name='X', age=18}}
2、代码
pom.xml文件依赖:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.52</version>
</dependency>
<dependency>
<groupId>com.baidu</groupId><!-- 百度封装了google protobuf的实现 -->
<artifactId>jprotobuf</artifactId>
<version>2.2.6</version>
</dependency>
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.51</version>
</dependency>
<dependency>
<groupId>com.esotericsoftware</groupId>
<artifactId>kryo</artifactId>
<version>4.0.2</version>
</dependency>
</dependencies>
实例化对象:
Student:
public class Teacher implements Serializable{
private static final long serialVersionUID = 1L;
@Protobuf(required = true, order = 1, fieldType = FieldType.STRING)
private String id;
@Protobuf(required = true, order = 2, fieldType = FieldType.STRING)
private String name;
@Protobuf(required = true, order = 3, fieldType = FieldType.INT32)
private int age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Teacher{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
Teacher:
public class Student implements Serializable{
private static final long serialVersionUID = 1L;
@Protobuf(required = true, order = 1, fieldType = FieldType.STRING)
private String id;
@Protobuf(required = true, order = 2, fieldType = FieldType.STRING)
private String name;
@Protobuf(required = true, order = 3, fieldType = FieldType.INT32)
private int age;
@Protobuf(required = true, order = 4, fieldType = FieldType.STRING)
private String grade;
@Protobuf(required = true, order = 5, fieldType = FieldType.OBJECT)
private Teacher teacher;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
@Override
public String toString() {
return "Student{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", age=" + age +
", grade='" + grade + '\'' +
", teacher=" + teacher.toString() +
'}';
}
}
序列化实现:
public class SerializableDemo {
//初始化
private final static int SERIALIZABLE_NUM = 10000;
public static void main(String[] args) throws Exception {
System.out.println("初始化数据============================\n" + init().toString() + "\n");
//java原生
excuteWithNative();
//jackson
excuteWithJackSon();
//fastJson
excuteWithFastJson();
//hessian
excuteWithHessian();
//protobuf,baidu公司开发,底层给予protobuf,不用proto文件,通过注解即可实现序列化反序列化
excuteWithProtoBuf();
//kryo
excuteWithKryo();
//主流序列化方式JSON/Hessian(2) /xml/protobuf/kryo/MsgPack/FST/thrift/protostuff/Avro
//更多序列化方式查看网络资料即可
}
private static void excuteWithNative() throws IOException, ClassNotFoundException {
Student student = init();
System.out.println("原生API序列化============================");
Long start1=System.currentTimeMillis();//初始化时间
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
Long start2=System.currentTimeMillis();//序列化时间
for(int i=0;i<SERIALIZABLE_NUM;i++){
oos.writeObject(student);
//因为是累加的,只取第一次序列化后的数据
if(i==0){
System.out.println("序列化后数据长度:" + bos.toByteArray().length);
}
}
Long end = System.currentTimeMillis();
System.out.println("初始化时间:" +(start2-start1)+"ms");
System.out.println("序列化" + SERIALIZABLE_NUM + "次花费时间(包含初始化时间):" +(end-start1)+"ms");
System.out.println("序列化" + SERIALIZABLE_NUM + "次花费时间(不包含初始化时间):" +(end-start2)+"ms");
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
Student student1 = (Student) ois.readObject();
System.out.println("反序列化后数据:" + student1.toString() + "\n");
}
private static void excuteWithJackSon() throws IOException {
Student student = init();
System.out.println("jackson序列化============================");
Long start1=System.currentTimeMillis();//初始化时间
ObjectMapper mapper=new ObjectMapper();
byte[] writeBytes=null;
Long start2=System.currentTimeMillis();//序列化时间
for(int i=0;i<SERIALIZABLE_NUM;i++){
writeBytes=mapper.writeValueAsBytes(student);
}
Long end = System.currentTimeMillis();
System.out.println("序列化后数据长度:" + writeBytes.length);
System.out.println("初始化时间:" +(start2-start1)+"ms");
System.out.println("序列化" + SERIALIZABLE_NUM + "次花费时间(包含初始化时间):" +(end-start1)+"ms");
System.out.println("序列化" + SERIALIZABLE_NUM + "次花费时间(不包含初始化时间):" +(end-start2)+"ms");
Student student1 = mapper.readValue(writeBytes,Student.class);
System.out.println("反序列化后数据:" + student1.toString() + "\n");
}
private static void excuteWithFastJson() throws IOException {
Student student = init();
System.out.println("FastJson序列化============================");
Long start1=System.currentTimeMillis();//初始化时间
String text=null;
Long start2=System.currentTimeMillis();//序列化时间
for(int i=0;i<SERIALIZABLE_NUM;i++){
text=JSON.toJSONString(student);
}
Long end = System.currentTimeMillis();
System.out.println("序列化后数据长度:" + text.getBytes().length);
System.out.println("初始化时间:" +(start2-start1)+"ms");
System.out.println("序列化" + SERIALIZABLE_NUM + "次花费时间(包含初始化时间):" +(end-start1)+"ms");
System.out.println("序列化" + SERIALIZABLE_NUM + "次花费时间(不包含初始化时间):" +(end-start2)+"ms");
Student student1 = JSON.parseObject(text,Student.class);
System.out.println("反序列化后数据:" + student1.toString() + "\n");
}
private static void excuteWithHessian() throws IOException {
Student student = init();
System.out.println("Hessian序列化============================");
Long start1=System.currentTimeMillis();//初始化时间
ByteArrayOutputStream bos=new ByteArrayOutputStream();
HessianOutput ho=new HessianOutput(bos);
Long start2=System.currentTimeMillis();//序列化时间
for(int i=0;i<SERIALIZABLE_NUM;i++){
ho.writeObject(student);
if(i==0){
System.out.println("序列化后数据长度:" + bos.toByteArray().length);
}
}
Long end = System.currentTimeMillis();
System.out.println("初始化时间:" +(start2-start1)+"ms");
System.out.println("序列化" + SERIALIZABLE_NUM + "次花费时间(包含初始化时间):" +(end-start1)+"ms");
System.out.println("序列化" + SERIALIZABLE_NUM + "次花费时间(不包含初始化时间):" +(end-start2)+"ms");
HessianInput hi=new HessianInput(new ByteArrayInputStream(bos.toByteArray()));
Student student1=(Student)hi.readObject();
System.out.println("反序列化后数据:" + student1.toString() + "\n");
}
private static void excuteWithProtoBuf() throws IOException {
Student student = init();
System.out.println("ProtoBuf序列化============================");
Long start1=System.currentTimeMillis();//初始化时间
Codec<Student> studentCodec= ProtobufProxy.create(Student.class,false);
byte[] bytes=null;
Long start2=System.currentTimeMillis();//序列化时间
for(int i=0;i<SERIALIZABLE_NUM;i++){
bytes=studentCodec.encode(student);
}
Long end = System.currentTimeMillis();
System.out.println("序列化后数据长度:" + bytes.length);
System.out.println("初始化时间:" +(start2-start1)+"ms");
System.out.println("序列化" + SERIALIZABLE_NUM + "次花费时间(包含初始化时间):" +(end-start1)+"ms");
System.out.println("序列化" + SERIALIZABLE_NUM + "次花费时间(不包含初始化时间):" +(end-start2)+"ms");
Student student1 = studentCodec.decode(bytes);;
System.out.println("反序列化后数据:" + student1.toString() + "\n");
}
private static void excuteWithKryo() throws IOException {
Student student = init();
System.out.println("Kyro序列化============================");
Long start1=System.currentTimeMillis();//初始化时间
Kryo kryo = new Kryo();
Output output = new Output();
byte[] bytes = new byte[60 * SERIALIZABLE_NUM];//这个buffer需要可以保留所有序列化后的对象
output.setBuffer(bytes);
Long start2=System.currentTimeMillis();//序列化时间
for(int i=0;i<SERIALIZABLE_NUM;i++){
kryo.writeObject(output, student);
output.toBytes();
if(i==0){
System.out.println("序列化后数据长度:" + output.toBytes().length);
}
}
Long end = System.currentTimeMillis();
System.out.println("初始化时间:" +(start2-start1)+"ms");
System.out.println("序列化" + SERIALIZABLE_NUM + "次花费时间(包含初始化时间):" +(end-start1)+"ms");
System.out.println("序列化" + SERIALIZABLE_NUM + "次花费时间(不包含初始化时间):" +(end-start2)+"ms");
Input input = new Input(output.toBytes());
Student student1 = kryo.readObject(input, Student.class);
System.out.println("反序列化后数据:" + student1.toString() + "\n");
}
/**
* 序列化bean初始化
* @return
*/
private static Student init(){
Teacher teacher = new Teacher();
teacher.setId("00001");
teacher.setName("X");
teacher.setAge(18);
Student student = new Student();
student.setId("00011");
student.setName("Y");
student.setAge(18);
student.setGrade("100");
student.setTeacher(teacher);
return student;
}
}