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

相关文章

SpringShell命令行之交互式Shell应用开发方式

《SpringShell命令行之交互式Shell应用开发方式》本文将深入探讨SpringShell的核心特性、实现方式及应用场景,帮助开发者掌握这一强大工具,具有很好的参考价值,希望对大家有所帮助,如... 目录引言一、Spring Shell概述二、创建命令类三、命令参数处理四、命令分组与帮助系统五、自定

SpringBoot应用中出现的Full GC问题的场景与解决

《SpringBoot应用中出现的FullGC问题的场景与解决》这篇文章主要为大家详细介绍了SpringBoot应用中出现的FullGC问题的场景与解决方法,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录Full GC的原理与触发条件原理触发条件对Spring Boot应用的影响示例代码优化建议结论F

MySQL 分区与分库分表策略应用小结

《MySQL分区与分库分表策略应用小结》在大数据量、复杂查询和高并发的应用场景下,单一数据库往往难以满足性能和扩展性的要求,本文将详细介绍这两种策略的基本概念、实现方法及优缺点,并通过实际案例展示如... 目录mysql 分区与分库分表策略1. 数据库水平拆分的背景2. MySQL 分区策略2.1 分区概念

Spring Shell 命令行实现交互式Shell应用开发

《SpringShell命令行实现交互式Shell应用开发》本文主要介绍了SpringShell命令行实现交互式Shell应用开发,能够帮助开发者快速构建功能丰富的命令行应用程序,具有一定的参考价... 目录引言一、Spring Shell概述二、创建命令类三、命令参数处理四、命令分组与帮助系统五、自定义S

如何配置Spring Boot中的Jackson序列化

《如何配置SpringBoot中的Jackson序列化》在开发基于SpringBoot的应用程序时,Jackson是默认的JSON序列化和反序列化工具,本文将详细介绍如何在SpringBoot中配置... 目录配置Spring Boot中的Jackson序列化1. 为什么需要自定义Jackson配置?2.

C语言函数递归实际应用举例详解

《C语言函数递归实际应用举例详解》程序调用自身的编程技巧称为递归,递归做为一种算法在程序设计语言中广泛应用,:本文主要介绍C语言函数递归实际应用举例的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录前言一、递归的概念与思想二、递归的限制条件 三、递归的实际应用举例(一)求 n 的阶乘(二)顺序打印

使用Jackson进行JSON生成与解析的新手指南

《使用Jackson进行JSON生成与解析的新手指南》这篇文章主要为大家详细介绍了如何使用Jackson进行JSON生成与解析处理,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 核心依赖2. 基础用法2.1 对象转 jsON(序列化)2.2 JSON 转对象(反序列化)3.

Linux系统中卸载与安装JDK的详细教程

《Linux系统中卸载与安装JDK的详细教程》本文详细介绍了如何在Linux系统中通过Xshell和Xftp工具连接与传输文件,然后进行JDK的安装与卸载,安装步骤包括连接Linux、传输JDK安装包... 目录1、卸载1.1 linux删除自带的JDK1.2 Linux上卸载自己安装的JDK2、安装2.1

Linux卸载自带jdk并安装新jdk版本的图文教程

《Linux卸载自带jdk并安装新jdk版本的图文教程》在Linux系统中,有时需要卸载预装的OpenJDK并安装特定版本的JDK,例如JDK1.8,所以本文给大家详细介绍了Linux卸载自带jdk并... 目录Ⅰ、卸载自带jdkⅡ、安装新版jdkⅠ、卸载自带jdk1、输入命令查看旧jdkrpm -qa

Python如何使用__slots__实现节省内存和性能优化

《Python如何使用__slots__实现节省内存和性能优化》你有想过,一个小小的__slots__能让你的Python类内存消耗直接减半吗,没错,今天咱们要聊的就是这个让人眼前一亮的技巧,感兴趣的... 目录背景:内存吃得满满的类__slots__:你的内存管理小助手举个大概的例子:看看效果如何?1.