应用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

相关文章

C#使用yield关键字实现提升迭代性能与效率

《C#使用yield关键字实现提升迭代性能与效率》yield关键字在C#中简化了数据迭代的方式,实现了按需生成数据,自动维护迭代状态,本文主要来聊聊如何使用yield关键字实现提升迭代性能与效率,感兴... 目录前言传统迭代和yield迭代方式对比yield延迟加载按需获取数据yield break显式示迭

java脚本使用不同版本jdk的说明介绍

《java脚本使用不同版本jdk的说明介绍》本文介绍了在Java中执行JavaScript脚本的几种方式,包括使用ScriptEngine、Nashorn和GraalVM,ScriptEngine适用... 目录Java脚本使用不同版本jdk的说明1.使用ScriptEngine执行javascript2.

将Python应用部署到生产环境的小技巧分享

《将Python应用部署到生产环境的小技巧分享》文章主要讲述了在将Python应用程序部署到生产环境之前,需要进行的准备工作和最佳实践,包括心态调整、代码审查、测试覆盖率提升、配置文件优化、日志记录完... 目录部署前夜:从开发到生产的心理准备与检查清单环境搭建:打造稳固的应用运行平台自动化流水线:让部署像

Linux中Curl参数详解实践应用

《Linux中Curl参数详解实践应用》在现代网络开发和运维工作中,curl命令是一个不可或缺的工具,它是一个利用URL语法在命令行下工作的文件传输工具,支持多种协议,如HTTP、HTTPS、FTP等... 目录引言一、基础请求参数1. -X 或 --request2. -d 或 --data3. -H 或

Java实现任务管理器性能网络监控数据的方法详解

《Java实现任务管理器性能网络监控数据的方法详解》在现代操作系统中,任务管理器是一个非常重要的工具,用于监控和管理计算机的运行状态,包括CPU使用率、内存占用等,对于开发者和系统管理员来说,了解这些... 目录引言一、背景知识二、准备工作1. Maven依赖2. Gradle依赖三、代码实现四、代码详解五

在Ubuntu上部署SpringBoot应用的操作步骤

《在Ubuntu上部署SpringBoot应用的操作步骤》随着云计算和容器化技术的普及,Linux服务器已成为部署Web应用程序的主流平台之一,Java作为一种跨平台的编程语言,具有广泛的应用场景,本... 目录一、部署准备二、安装 Java 环境1. 安装 JDK2. 验证 Java 安装三、安装 mys

Python中构建终端应用界面利器Blessed模块的使用

《Python中构建终端应用界面利器Blessed模块的使用》Blessed库作为一个轻量级且功能强大的解决方案,开始在开发者中赢得口碑,今天,我们就一起来探索一下它是如何让终端UI开发变得轻松而高... 目录一、安装与配置:简单、快速、无障碍二、基本功能:从彩色文本到动态交互1. 显示基本内容2. 创建链

Node.js 中 http 模块的深度剖析与实战应用小结

《Node.js中http模块的深度剖析与实战应用小结》本文详细介绍了Node.js中的http模块,从创建HTTP服务器、处理请求与响应,到获取请求参数,每个环节都通过代码示例进行解析,旨在帮... 目录Node.js 中 http 模块的深度剖析与实战应用一、引言二、创建 HTTP 服务器:基石搭建(一

锐捷和腾达哪个好? 两个品牌路由器对比分析

《锐捷和腾达哪个好?两个品牌路由器对比分析》在选择路由器时,Tenda和锐捷都是备受关注的品牌,各自有独特的产品特点和市场定位,选择哪个品牌的路由器更合适,实际上取决于你的具体需求和使用场景,我们从... 在选购路由器时,锐捷和腾达都是市场上备受关注的品牌,但它们的定位和特点却有所不同。锐捷更偏向企业级和专

java中VO PO DTO POJO BO DO对象的应用场景及使用方式

《java中VOPODTOPOJOBODO对象的应用场景及使用方式》文章介绍了Java开发中常用的几种对象类型及其应用场景,包括VO、PO、DTO、POJO、BO和DO等,并通过示例说明了它... 目录Java中VO PO DTO POJO BO DO对象的应用VO (View Object) - 视图对象