【優雅程式碼】12-hessian、kryo、json序列化對比
阿新 • • 發佈:2022-01-10
平常我們在使用rpc呼叫或者將其持久化到資料庫的時候則需要將物件或者檔案或者圖片等資料將其轉為二進位制位元組資料,那麼各自的優劣是什麼呢。
【優雅程式碼】12-hessian、kryo、json序列化對比
歡迎關注b站賬號/公眾號【六邊形戰士夏寧】,一個要把各項指標拉滿的男人。該文章已在github目錄收錄。
螢幕前的大帥比和大漂亮如果有幫助到你的話請順手點個贊、加個收藏這對我真的很重要。別下次一定了,都不關注上哪下次一定。
- 視訊講解
- 可直接執行的完整程式碼
- 上一篇stream精選/@functional懶載入示例
- 下一篇linkedList插入真的比arrayList快麼
1.背景
平常我們在使用rpc呼叫或者將其持久化到資料庫的時候則需要將物件或者檔案或者圖片等資料將其轉為二進位制位元組資料,那麼各自的優劣是什麼呢。
2.常見的序列化方式
java自帶的序列化,平常用的最多的json序列化(也可以叫http資料傳輸的序列化),dubbo預設的序列化hessian2,其優勢在於跨語言(不過跨語言序列化一般還是json和xml範圍更廣些),目前公認穩定且最快的序列化方式Kryo
2.1 Hessian使用
2.1導包
<dependency> <groupId>com.alibaba</groupId> <artifactId>hessian-lite</artifactId> <version>3.2.9</version> </dependency>
2.2工具類
/** * JavaBean序列化. * * @param javaBean Java物件. * @throws Exception 異常資訊. */ public static <T> byte[] serialize(T javaBean) { try { @Cleanup ByteArrayOutputStream baos = new ByteArrayOutputStream(); @Cleanup Hessian2Output ho = new Hessian2Output(baos); ho.writeObject(javaBean); ho.flush(); return baos.toByteArray(); } catch (IOException e) { throw new RuntimeException(e); } } /** * JavaBean反序列化. * * @param serializeData 序列化資料. * @throws Exception 異常資訊. */ public static <T> T deserialize(byte[] serializeData) { try { @Cleanup ByteArrayInputStream bais = new ByteArrayInputStream(serializeData); @Cleanup Hessian2Input hi = new Hessian2Input(bais); return (T) hi.readObject(); } catch (IOException e) { throw new RuntimeException(e); } }
2.2kryo
2.2.1導包
<dependency>
<groupId>com.esotericsoftware</groupId>
<artifactId>kryo-shaded</artifactId>
<version>4.0.2</version>
</dependency>
2.2.2工具類
// 這個東西執行緒不安全
private static final ThreadLocal<Kryo> KRYO = ThreadLocal.withInitial(() -> {
Kryo kryo = new Kryo();
kryo.setInstantiatorStrategy(new Kryo.DefaultInstantiatorStrategy());
return kryo;
});
// newKryoPool() {
// return new KryoPool.Builder(() -> {
// final Kryo kryo = new Kryo();
// kryo.setInstantiatorStrategy(new Kryo.DefaultInstantiatorStrategy(
// new StdInstantiatorStrategy()));
// return kryo;
// }).softReferences().build();
// }
public static <T extends Serializable> byte[] serialization(T obj) {
KRYO.get().register(obj.getClass());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Output output = new Output(baos);
// KRYO.get().writeClassAndObject(output, obj);
KRYO.get().writeObject(output, obj);
output.close();
return baos.toByteArray();
}
public static <T extends Serializable> T deserialization(byte[] b, Class<T> clazz) {
KRYO.get().register(clazz);
ByteArrayInputStream bais = new ByteArrayInputStream(b);
Input input = new Input(bais);
// return (T) KRYO.get().readClassAndObject(input);
return (T) KRYO.get().readObject(input, clazz);
}
3.比較
3.1長度比較
public static void compareLength() {
// 輸出129,88,4,12,可以看出json格式的優秀,還有Kryo針對java格式的優秀
SerializeTestObject serializeTestObject = new SerializeTestObject();
serializeTestObject.name = "1";
byte[] serialize = SerializationUtils.serialize(serializeTestObject);
SerializeTestObject deserialize = SerializationUtils.<SerializeTestObject>deserialize(serialize);
System.out.println("deserialize");
System.out.println(serialize.length);
byte[] serializeHe = Hessian2Utils.serialize(serializeTestObject);
SerializeTestObject deserializeHe = Hessian2Utils.<SerializeTestObject>deserialize(serializeHe);
System.out.println("deserializeHe");
System.out.println(serializeHe.length);
byte[] serializeHeKryo = KryoUtils.serialization(serializeTestObject);
SerializeTestObject deserializeKryo = KryoUtils.deserialization(serializeHeKryo, SerializeTestObject.class);
System.out.println("deserializeKryo");
System.out.println(serializeHeKryo.length);
System.out.println("json");
System.out.println(JSON.toJSONString(serializeTestObject).getBytes(StandardCharsets.UTF_8).length);
}
3.2序列化速度比較
public static void compareSerialize() {
SerializeTestObject serializeTestObject = new SerializeTestObject();
serializeTestObject.name = "1";
StopWatch sw = new StopWatch();
sw.start("serialize");
for (int i = 0; i < 10000; i++) {
SerializationUtils.serialize(serializeTestObject);
}
sw.stop();
sw.start("He");
for (int i = 0; i < 10000; i++) {
Hessian2Utils.serialize(serializeTestObject);
}
sw.stop();
sw.start("Kryo");
for (int i = 0; i < 10000; i++) {
KryoUtils.serialization(serializeTestObject);
}
sw.stop();
sw.start("fastJson");
for (int i = 0; i < 10000; i++) {
JSON.toJSONString(serializeTestObject);
}
sw.stop();
sw.start("jackson");
// 執行緒安全
ObjectMapper objectMapper = new ObjectMapper();
for (int i = 0; i < 10000; i++) {
objectMapper.writeValueAsString(serializeTestObject);
}
sw.stop();
// 執行緒安全
Gson gson = new Gson();
sw.start("Gson");
for (int i = 0; i < 10000; i++) {
gson.toJson(serializeTestObject);
}
sw.stop();
System.out.println(sw.prettyPrint());
}
輸出資訊如下,fastjson比GSON慢,bug還多些,除了Hessian2明顯比較慢,其它的速度都差不多
098443444 004% serialize
1897660738 075% He
107569186 004% Kryo
159348852 006% fastJson
240632483 010% jackson
028160603 001% Gson
3.3反序列化速度比較
private static void compareDeSerialize() {
SerializeTestObject serializeTestObject = new SerializeTestObject();
serializeTestObject.name = "1";
String jsonStr = JSON.toJSONString(serializeTestObject);
byte[] serialize = SerializationUtils.serialize(serializeTestObject);
byte[] serializeHe = Hessian2Utils.serialize(serializeTestObject);
byte[] serializeHeKryo = KryoUtils.serialization(serializeTestObject);
// 這裡統一採用複雜json的處理方式
TypeReference<SerializeTestObject> fastJsonType = new TypeReference<SerializeTestObject>() {
};
com.fasterxml.jackson.core.type.TypeReference<SerializeTestObject> jackJsonType = new com.fasterxml.jackson.core.type.TypeReference<SerializeTestObject>() {
};
Type gsonType = new TypeToken<SerializeTestObject>() {
}.getType();
StopWatch sw = new StopWatch();
sw.start("serialize");
for (int i = 0; i < 10000; i++) {
SerializeTestObject deserialize = SerializationUtils.<SerializeTestObject>deserialize(serialize);
}
sw.stop();
sw.start("He");
for (int i = 0; i < 10000; i++) {
SerializeTestObject deserializeHe = Hessian2Utils.<SerializeTestObject>deserialize(serializeHe);
}
sw.stop();
sw.start("Kryo");
for (int i = 0; i < 10000; i++) {
SerializeTestObject deserializeKryo = KryoUtils.deserialization(serializeHeKryo, SerializeTestObject.class);
}
sw.stop();
sw.start("fastJson");
for (int i = 0; i < 10000; i++) {
SerializeTestObject fastJsonObject = JSON.parseObject(jsonStr, fastJsonType);
}
sw.stop();
sw.start("jackson");
// 執行緒安全
ObjectMapper objectMapper = new ObjectMapper();
for (int i = 0; i < 10000; i++) {
SerializeTestObject jacksonObject = objectMapper.readValue(jsonStr, jackJsonType);
}
sw.stop();
// 執行緒安全
Gson gson = new Gson();
sw.start("Gson");
for (int i = 0; i < 10000; i++) {
SerializeTestObject gsonObject = gson.fromJson(jsonStr, gsonType);
}
sw.stop();
System.out.println(sw.prettyPrint());
}
輸出結果如下,可以看到Kryo、fastJson、Gson搖搖領先
143722925 021% serialize
128508821 018% He
046400438 007% Kryo
052540898 008% fastJson
292601294 042% jackson
032188332 005% Gson