From 12f7f49b8fe9672cb44b5bf80ca910c72f49794f Mon Sep 17 00:00:00 2001 From: YuCheng Hu Date: Sat, 10 Aug 2019 13:37:53 -0400 Subject: [PATCH] =?UTF-8?q?MessagePack=20Jackson=20Data=20Size=20=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E6=BA=90=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../demo/serialize/MessagePackDataTest.java | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/test/java/com/insight/demo/serialize/MessagePackDataTest.java diff --git a/src/test/java/com/insight/demo/serialize/MessagePackDataTest.java b/src/test/java/com/insight/demo/serialize/MessagePackDataTest.java new file mode 100644 index 0000000..f726dda --- /dev/null +++ b/src/test/java/com/insight/demo/serialize/MessagePackDataTest.java @@ -0,0 +1,56 @@ +package com.insight.demo.serialize; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.insight.demo.serialize.model.msgpack.MessageData; +import com.insight.demo.utils.MockDataUtils; +import org.apache.commons.io.FileUtils; +import org.junit.Test; +import org.msgpack.jackson.dataformat.JsonArrayFormat; +import org.msgpack.jackson.dataformat.MessagePackFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.xerial.snappy.Snappy; + +import java.util.List; + +public class MessagePackDataTest { + + final Logger logger = LoggerFactory.getLogger(MessagePackDataTest.class); + + @Test + public void testDataSize() { + logger.debug("Test Data Size"); + + byte[] raw; + byte[] rawJsonArray; + + List dataList = MockDataUtils.getMessageDataList(600000); + + try { + ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory()); + raw = objectMapper.writeValueAsBytes(dataList); + + FileUtils.byteCountToDisplaySize(raw.length); + logger.debug("Raw Size: [{}]", FileUtils.byteCountToDisplaySize(raw.length)); + + + objectMapper = new ObjectMapper(new MessagePackFactory()); + objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); + objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); + objectMapper.setAnnotationIntrospector(new JsonArrayFormat()); + + rawJsonArray = objectMapper.writeValueAsBytes(dataList); + logger.debug("rawJsonArray Size: [{}]", FileUtils.byteCountToDisplaySize(rawJsonArray.length)); + + logger.debug("RawrawJsonArray Snappy Size: [{}]", FileUtils.byteCountToDisplaySize(Snappy.compress(rawJsonArray).length)); + + } catch (Exception e) { + e.printStackTrace(); + } + + } +}