package com.lieve.serialize;
import com.google.gson.Gson;
import com.lieve.bean.Person;
import org.junit.Test;
import org.springframework.util.StopWatch;
public class SerializerTest {
@Test
public void testSerialize() {
Person person = new Person();
person.setName("zhangsan");
person.setAge(10);
HessianSerializer hessianSerializer = new HessianSerializer();
Hessian2Serializer hessian2Serializer = new Hessian2Serializer();
JDKSerializer jdkSerializer = new JDKSerializer();
StopWatch stopWatch = new StopWatch("st");
stopWatch.start("hessian");
byte[] hessianBytes = hessianSerializer.serialize2(person);
stopWatch.stop();
stopWatch.start("hessian2");
byte[] hessian2Bytes = hessian2Serializer.serialize2(person);
stopWatch.stop();
stopWatch.start("jdk");
byte[] jdkBytes = jdkSerializer.serialize2(person);
stopWatch.stop();
System.out.println();
System.out.printf("hessianBytes size : %d\n", hessianBytes.length);
System.out.printf("hessian2Bytes size : %d\n", hessian2Bytes.length);
System.out.printf("jdkBytes size : %d\n", jdkBytes.length);
stopWatch.start("d hessian");
Object hessianP = hessianSerializer.deserialize2(hessianBytes, Person.class);
stopWatch.stop();
stopWatch.start("d hessian2");
Object hessian2P = hessian2Serializer.deserialize2(hessian2Bytes, Person.class);
stopWatch.stop();
stopWatch.start("d jdk");
Object jdkP = jdkSerializer.deserialize2(jdkBytes, Person.class);
stopWatch.stop();
System.out.println("h: " + toStr(hessianP));
System.out.println("h2: " + toStr(hessian2P));
System.out.println("jdk: " + toStr(jdkP));
System.out.println(stopWatch.prettyPrint());
}
private static final Gson GSON = new Gson();
public static String toStr(Object o) {
return GSON.toJson(o);
}
}
复制代码
输出结果:
hessianBytes size : 55
hessian2Bytes size : 48
jdkBytes size : 91
h: {"name":"zhangsan","age":10}
h2: {"name":"zhangsan","age":10}
jdk: {"name":"zhangsan","age":10}
StopWatch 'st': running time = 30384791 ns
---------------------------------------------
ns % Task name
---------------------------------------------
014422916 047% hessian
000839042 003% hessian2
010496875 035% jdk
001346875 004% d hessian
000982417 003% d hessian2
002296666 008% d jdk
结论:Hessian 2 API > Hessian API > JDK 序列化
评论