写点什么

Hessian Hessian2 JDK 序列化性能对比

作者:water
  • 2022-11-09
    北京
  • 本文字数:1265 字

    阅读完需:约 4 分钟

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 序列化


用户头像

water

关注

求知若饥,虚心若愚 2018-05-02 加入

结论先行,以上统下,归类分组,逻辑递进 观察、感受、需要、请求

评论

发布
暂无评论
Hessian Hessian2 JDK 序列化性能对比_water_InfoQ写作社区