Skip to content

序列化、反序列化太慢 #10

Open
@HiwayChe

Description

@HiwayChe
private Person getPerson() {
    Person person = new Person();
    person.setId(123L);
    person.setName("好汉");
    List<Person> friends = new ArrayList<>(10000);
    for (int i = 0; i < 10000; i++) {
        Person friend = new Person();
        friend.setId(Long.valueOf(i));
        friend.setName(String.valueOf("name" + i));
        friends.add(friend);
    }
    person.setFriends(friends);
    return person;
}
public static byte[] serializeWithHessianLite(Object object) {
    try {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1 << 10);
        com.alibaba.com.caucho.hessian.io.Hessian2Output hessian2Output = new com.alibaba.com.caucho.hessian.io.Hessian2Output(byteArrayOutputStream);
        hessian2Output.startMessage();
        hessian2Output.writeObject(object);
        hessian2Output.completeMessage();
        hessian2Output.close();
        return byteArrayOutputStream.toByteArray();
    } catch (IOException e) {
        throw new OperationException("serialize with hessian2 fail: " + e.getMessage());
    }
}

public static Object deserializeWithHessianLite(byte[] bytes) {
    try {
        com.alibaba.com.caucho.hessian.io.Hessian2Input hessian2Input = new com.alibaba.com.caucho.hessian.io.Hessian2Input(new ByteArrayInputStream(bytes));
        hessian2Input.startMessage();
        Object o = hessian2Input.readObject();
        hessian2Input.completeMessage();
        hessian2Input.close();
        return o;
    } catch (IOException e) {
        throw new OperationException("deserialize with hessian2 fail: " + e.getMessage());
    }
}

然后对getPerson()返回的数据进行10000次序列化和反序列化:

  private void hessian2PerformanceTest(Person person) {
    //预热
    for (int i = 0; i < 10; i++) {
        byte[] bytes3 = SerializeTestUtil.serializeWithHessianLite(person);
        Object o = SerializeTestUtil.deserializeWithHessianLite(bytes3);
        if (i == 0) {
            logger.warn("hessian2 序列化后长度={} 反序列化再序列化后长度={}", bytes3.length, SerializeTestUtil.serializeWithHessianLite(o).length);
        }
    }

    long start = System.currentTimeMillis();
    for (int i = 0; i < 10000; i++) {
        byte[] bytes = SerializeTestUtil.serializeWithHessianLite(person);
        Object o = SerializeTestUtil.deserializeWithHessianLite(bytes);
    }
    logger.warn("hessian2 序列化、反序列化10000次耗时:{}", System.currentTimeMillis() - start);
}

2019-03-21 19:25:15,652 WARN [main]
c.y.o.s.SerializeBenchmarkTest2.hessian2PerformanceTest(116) - hessian2 序列化后长度=146917 反序列化再序列化后长度=146917
2019-03-21 19:25:56,922 WARN [main] c.y.o.s.SerializeBenchmarkTest2.hessian2PerformanceTest(125) - hessian2 序列化、反序列化10000次耗时:41066
2019-03-21 19:25:57,010 WARN [main] c.y.o.s.SerializeBenchmarkTest2.kryoPerformanceTestWithThreadLocal(134) - kryo(pooling+compatible) 序列化后长度=130718, 反序列化再序列化后长度=130718
2019-03-21 19:26:16,020 WARN [main] c.y.o.s.SerializeBenchmarkTest2.kryoPerformanceTestWithThreadLocal(143) - kryo序列化、反序列化(pooling+compatible)10000次耗时:18958

结果hessian-lite需要40多秒,我也用了kryo做对比,只要18秒,kryo通过ThreadLocal做了缓存。
是我hessian-lite写的不对吗?还有其他提高性能的方法不?

Activity

lovepoem

lovepoem commented on Mar 22, 2019

@lovepoem
Member
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @lovepoem@HiwayChe

        Issue actions

          序列化、反序列化太慢 · Issue #10 · apache/dubbo-hessian-lite