应用Kryo Pool来改善性能,并与JDK/Jackson性能对比

2023-10-11 18:18

本文主要是介绍应用Kryo Pool来改善性能,并与JDK/Jackson性能对比,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

pom.xml先加入引用:

<dependency><groupId>com.esotericsoftware</groupId><artifactId>kryo</artifactId><version>5.0.0-RC1</version>
</dependency>

先上KryoUtils工具类.

package com.freestyle.common.utils;import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.esotericsoftware.kryo.util.Pool;/***** Kryo序列化工具* * @author dgmislrh**/
public class KryoUtils {private static Pool<Kryo> mKryoPool = new Pool<Kryo>(true, false, 8) {protected Kryo create() {Kryo kryo = new Kryo();kryo.setRegistrationRequired(false);kryo.setReferences(false);// Configure the Kryo instance.return kryo;}};private static Pool<Output> mOutputPool = new Pool<Output>(true, false, 16) {protected Output create() {return new Output(1024, -1);}};private static Pool<Input> mInputPool = new Pool<Input>(true, false, 16) {protected Input create() {return new Input(1024);}};public KryoUtils() {// TODO Auto-generated constructor stub}public static byte[] serialize(Object object) {Kryo lvKryo = mKryoPool.obtain();Output lvOutput = mOutputPool.obtain();try {lvOutput.reset();lvKryo.writeObject(lvOutput, object);return lvOutput.getBuffer();} finally {mKryoPool.free(lvKryo);mOutputPool.free(lvOutput);}}public static <T> T unserialize(byte[] pvBytes,Class<T> pvClass) {Kryo lvKryo = mKryoPool.obtain();Input lvInput= mInputPool.obtain();try {lvInput.setBuffer(pvBytes);return lvKryo.readObject(lvInput, pvClass);			} finally {mKryoPool.free(lvKryo);mInputPool.free(lvInput);}}}

 

写一个junit测试,循环10W次对一个比较复杂的TableResponseBean对象进行序列/反序列, 对比性能:

package test.package1;import org.junit.Test;import com.fasterxml.jackson.core.type.TypeReference;
import com.freestyle.common.protocols.TableResponseBean;
import com.freestyle.common.utils.JRedisUtils;
import com.freestyle.common.utils.JsonUtils;
import com.freestyle.common.utils.KryoUtils;public class TestSerializable {public TestSerializable() {// TODO Auto-generated constructor stub}final String m_jsonStr="{\"errCode\":0,\"errMsg\":\"\",\"errRef\":\"\",\"result\":{\"pageSize\":10,\"rows\":[{\"fa_name\":\"管理员\",\"fa_update_dt\":1476431255312,\"fa_status\":\"A\",\"fa_update_by\":\"supuser1\",\"fa_email\":\"\",\"fa_type\":\"A\",\"fa_create_by\":\"test1\",\"fa_login\":\"admin\",\"fa_passwd\":\"d1841df9a9ead353f339dd239a1b4676\",\"fa_last_notify\":9711,\"fa_remark\":\"\",\"fa_create_dt\":1474269880594,\"fa_staff_id\":\"\"},{\"fa_name\":\"胡飞\",\"fa_update_dt\":1490155596768,\"fa_status\":\"A\",\"fa_login\":\"hufei\",\"fa_passwd\":\"ab257d341f5d5afe96bc489a2534b7d8\",\"fa_remark\":\"\",\"fa_create_dt\":1490060383903,\"fa_update_by\":\"00001\",\"fa_email\":\"\",\"fa_staff_id\":\"\",\"fa_type\":\"N\",\"fa_create_by\":\"00001\"}],\"cols\":null,\"sortorder\":null,\"sortByColumn\":null,\"totalRecords\":2,\"currentPage\":1,\"totalPageCount\":1}}";@Testpublic void testPerformances() throws Exception {TableResponseBean lvRet=JsonUtils.readValue(m_jsonStr, new TypeReference<TableResponseBean>() {});		System.out.println(lvRet);int c_times=100000;System.out.print("正在测试JDK序列化器...");long lvTm=System.currentTimeMillis();byte[]  lvBytes=null;		for (int i=1;i<=c_times;i++) {lvBytes=JRedisUtils.serialize(lvRet);}System.out.println("Byte array length:"+lvBytes.length);System.out.println("Serialize by ObjectOutputStream.writeObject, use:"+(System.currentTimeMillis()-lvTm));TableResponseBean lvNewPage=null;		lvTm=System.currentTimeMillis();for (int i=1;i<=c_times;i++) {lvNewPage=JRedisUtils.unSerialize(lvBytes);}System.out.println("UNSerialize by ObjectInputStream.readObject, use:"+ (System.currentTimeMillis()-lvTm));System.out.println("------------------------");System.out.print("正在测试jackson序列化器...");String lvTmp=null;lvTm=System.currentTimeMillis();for (int i=1;i<=c_times;i++) {lvTmp=JRedisUtils.O2Json(lvRet);}System.out.println("Serialize by JasonTool, Use:"+(System.currentTimeMillis()-lvTm));System.out.println("Json String length:"+lvTmp.getBytes().length);lvTm=System.currentTimeMillis();for (int i=1;i<=c_times;i++) {lvNewPage= JRedisUtils.Json2O(lvTmp, TableResponseBean.class);}System.out.println("UNSerialize by JasonTool, Use:"+(System.currentTimeMillis()-lvTm));System.out.println(lvNewPage);System.out.println("------------------------");System.out.print("正在测试Kryo序列化器...");lvTmp=null;lvTm=System.currentTimeMillis();for (int i=1;i<=c_times;i++) {lvBytes=KryoUtils.serialize(lvRet);}System.out.println("Serialize by Kryo, Use:"+(System.currentTimeMillis()-lvTm));System.out.println("Bytes length:"+lvBytes.length);lvTm=System.currentTimeMillis();for (int i=1;i<=c_times;i++) {lvNewPage= KryoUtils.unserialize(lvBytes, TableResponseBean.class);}System.out.println("UNSerialize by Kryo, Use:"+(System.currentTimeMillis()-lvTm));System.out.println(lvNewPage);				}}

 

测试结果:

{"errCode":0,"errMsg":"","errRef":"","result":{"pageSize":10,"rows":[{"fa_name":"管理员","fa_update_dt":1476431255312,"fa_status":"A","fa_update_by":"supuser1","fa_email":"","fa_type":"A","fa_create_by":"test1","fa_login":"admin","fa_passwd":"d1841df9a9ead353f339dd239a1b4676","fa_last_notify":9711,"fa_remark":"","fa_create_dt":1474269880594,"fa_staff_id":""},{"fa_name":"胡飞","fa_update_dt":1490155596768,"fa_status":"A","fa_login":"hufei","fa_passwd":"ab257d341f5d5afe96bc489a2534b7d8","fa_remark":"","fa_create_dt":1490060383903,"fa_update_by":"00001","fa_email":"","fa_staff_id":"","fa_type":"N","fa_create_by":"00001"}],"cols":null,"sortorder":null,"sortByColumn":null,"totalRecords":2,"totalPageCount":1,"currentPage":1}}
正在测试JDK序列化器...Byte array length:1285
Serialize by ObjectOutputStream.writeObject, use:1211
UNSerialize by ObjectInputStream.readObject, use:3488
------------------------
正在测试jackson序列化器...Serialize by JasonTool, Use:414
Json String length:734
UNSerialize by JasonTool, Use:631
{"errCode":0,"errMsg":"","errRef":"","result":{"pageSize":10,"rows":[{"fa_name":"管理员","fa_update_dt":1476431255312,"fa_status":"A","fa_update_by":"supuser1","fa_email":"","fa_type":"A","fa_create_by":"test1","fa_login":"admin","fa_passwd":"d1841df9a9ead353f339dd239a1b4676","fa_last_notify":9711,"fa_remark":"","fa_create_dt":1474269880594,"fa_staff_id":""},{"fa_name":"胡飞","fa_update_dt":1490155596768,"fa_status":"A","fa_login":"hufei","fa_passwd":"ab257d341f5d5afe96bc489a2534b7d8","fa_remark":"","fa_create_dt":1490060383903,"fa_update_by":"00001","fa_email":"","fa_staff_id":"","fa_type":"N","fa_create_by":"00001"}],"cols":null,"sortorder":null,"sortByColumn":null,"totalRecords":2,"totalPageCount":1,"currentPage":1}}
------------------------
正在测试Kryo序列化器...Serialize by Kryo, Use:328
Bytes length:1024
UNSerialize by Kryo, Use:398
{"errCode":0,"errMsg":"","errRef":"","result":{"pageSize":10,"rows":[{"fa_name":"管理员","fa_update_dt":1476431255312,"fa_status":"A","fa_update_by":"supuser1","fa_email":"","fa_type":"A","fa_create_by":"test1","fa_login":"admin","fa_passwd":"d1841df9a9ead353f339dd239a1b4676","fa_last_notify":9711,"fa_remark":"","fa_create_dt":1474269880594,"fa_staff_id":""},{"fa_name":"胡飞","fa_update_dt":1490155596768,"fa_status":"A","fa_login":"hufei","fa_passwd":"ab257d341f5d5afe96bc489a2534b7d8","fa_remark":"","fa_create_dt":1490060383903,"fa_update_by":"00001","fa_email":"","fa_staff_id":"","fa_type":"N","fa_create_by":"00001"}],"cols":null,"sortorder":null,"sortByColumn":null,"totalRecords":2,"totalPageCount":1,"currentPage":1}}

 

 

用上了缓冲, 最快的还是Kryo, 比Jackson json还要快近1倍.

这篇关于应用Kryo Pool来改善性能,并与JDK/Jackson性能对比的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/189932

相关文章

PostgreSQL的扩展dict_int应用案例解析

《PostgreSQL的扩展dict_int应用案例解析》dict_int扩展为PostgreSQL提供了专业的整数文本处理能力,特别适合需要精确处理数字内容的搜索场景,本文给大家介绍PostgreS... 目录PostgreSQL的扩展dict_int一、扩展概述二、核心功能三、安装与启用四、字典配置方法

Python中re模块结合正则表达式的实际应用案例

《Python中re模块结合正则表达式的实际应用案例》Python中的re模块是用于处理正则表达式的强大工具,正则表达式是一种用来匹配字符串的模式,它可以在文本中搜索和匹配特定的字符串模式,这篇文章主... 目录前言re模块常用函数一、查看文本中是否包含 A 或 B 字符串二、替换多个关键词为统一格式三、提

Java MQTT实战应用

《JavaMQTT实战应用》本文详解MQTT协议,涵盖其发布/订阅机制、低功耗高效特性、三种服务质量等级(QoS0/1/2),以及客户端、代理、主题的核心概念,最后提供Linux部署教程、Sprin... 目录一、MQTT协议二、MQTT优点三、三种服务质量等级四、客户端、代理、主题1. 客户端(Clien

关于MyISAM和InnoDB对比分析

《关于MyISAM和InnoDB对比分析》:本文主要介绍关于MyISAM和InnoDB对比分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录开篇:从交通规则看存储引擎选择理解存储引擎的基本概念技术原理对比1. 事务支持:ACID的守护者2. 锁机制:并发控制的艺

Mac系统下卸载JAVA和JDK的步骤

《Mac系统下卸载JAVA和JDK的步骤》JDK是Java语言的软件开发工具包,它提供了开发和运行Java应用程序所需的工具、库和资源,:本文主要介绍Mac系统下卸载JAVA和JDK的相关资料,需... 目录1. 卸载系统自带的 Java 版本检查当前 Java 版本通过命令卸载系统 Java2. 卸载自定

CSS中的Static、Relative、Absolute、Fixed、Sticky的应用与详细对比

《CSS中的Static、Relative、Absolute、Fixed、Sticky的应用与详细对比》CSS中的position属性用于控制元素的定位方式,不同的定位方式会影响元素在页面中的布... css 中的 position 属性用于控制元素的定位方式,不同的定位方式会影响元素在页面中的布局和层叠关

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

使用jenv工具管理多个JDK版本的方法步骤

《使用jenv工具管理多个JDK版本的方法步骤》jenv是一个开源的Java环境管理工具,旨在帮助开发者在同一台机器上轻松管理和切换多个Java版本,:本文主要介绍使用jenv工具管理多个JD... 目录一、jenv到底是干啥的?二、jenv的核心功能(一)管理多个Java版本(二)支持插件扩展(三)环境隔

Python使用Tkinter打造一个完整的桌面应用

《Python使用Tkinter打造一个完整的桌面应用》在Python生态中,Tkinter就像一把瑞士军刀,它没有花哨的特效,却能快速搭建出实用的图形界面,作为Python自带的标准库,无需安装即可... 目录一、界面搭建:像搭积木一样组合控件二、菜单系统:给应用装上“控制中枢”三、事件驱动:让界面“活”

如何确定哪些软件是Mac系统自带的? Mac系统内置应用查看技巧

《如何确定哪些软件是Mac系统自带的?Mac系统内置应用查看技巧》如何确定哪些软件是Mac系统自带的?mac系统中有很多自带的应用,想要看看哪些是系统自带,该怎么查看呢?下面我们就来看看Mac系统内... 在MAC电脑上,可以使用以下方法来确定哪些软件是系统自带的:1.应用程序文件夹打开应用程序文件夹