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

相关文章

分布式锁在Spring Boot应用中的实现过程

《分布式锁在SpringBoot应用中的实现过程》文章介绍在SpringBoot中通过自定义Lock注解、LockAspect切面和RedisLockUtils工具类实现分布式锁,确保多实例并发操作... 目录Lock注解LockASPect切面RedisLockUtils工具类总结在现代微服务架构中,分布

MySQL中EXISTS与IN用法使用与对比分析

《MySQL中EXISTS与IN用法使用与对比分析》在MySQL中,EXISTS和IN都用于子查询中根据另一个查询的结果来过滤主查询的记录,本文将基于工作原理、效率和应用场景进行全面对比... 目录一、基本用法详解1. IN 运算符2. EXISTS 运算符二、EXISTS 与 IN 的选择策略三、性能对比

Python标准库之数据压缩和存档的应用详解

《Python标准库之数据压缩和存档的应用详解》在数据处理与存储领域,压缩和存档是提升效率的关键技术,Python标准库提供了一套完整的工具链,下面小编就来和大家简单介绍一下吧... 目录一、核心模块架构与设计哲学二、关键模块深度解析1.tarfile:专业级归档工具2.zipfile:跨平台归档首选3.

使用IDEA部署Docker应用指南分享

《使用IDEA部署Docker应用指南分享》本文介绍了使用IDEA部署Docker应用的四步流程:创建Dockerfile、配置IDEADocker连接、设置运行调试环境、构建运行镜像,并强调需准备本... 目录一、创建 dockerfile 配置文件二、配置 IDEA 的 Docker 连接三、配置 Do

深入浅出SpringBoot WebSocket构建实时应用全面指南

《深入浅出SpringBootWebSocket构建实时应用全面指南》WebSocket是一种在单个TCP连接上进行全双工通信的协议,这篇文章主要为大家详细介绍了SpringBoot如何集成WebS... 目录前言为什么需要 WebSocketWebSocket 是什么Spring Boot 如何简化 We

Java Stream流之GroupBy的用法及应用场景

《JavaStream流之GroupBy的用法及应用场景》本教程将详细介绍如何在Java中使用Stream流的groupby方法,包括基本用法和一些常见的实际应用场景,感兴趣的朋友一起看看吧... 目录Java Stream流之GroupBy的用法1. 前言2. 基础概念什么是 GroupBy?Stream

python中列表应用和扩展性实用详解

《python中列表应用和扩展性实用详解》文章介绍了Python列表的核心特性:有序数据集合,用[]定义,元素类型可不同,支持迭代、循环、切片,可执行增删改查、排序、推导式及嵌套操作,是常用的数据处理... 目录1、列表定义2、格式3、列表是可迭代对象4、列表的常见操作总结1、列表定义是处理一组有序项目的

C#中的Converter的具体应用

《C#中的Converter的具体应用》C#中的Converter提供了一种灵活的类型转换机制,本文详细介绍了Converter的基本概念、使用场景,具有一定的参考价值,感兴趣的可以了解一下... 目录Converter的基本概念1. Converter委托2. 使用场景布尔型转换示例示例1:简单的字符串到

Linux系统中查询JDK安装目录的几种常用方法

《Linux系统中查询JDK安装目录的几种常用方法》:本文主要介绍Linux系统中查询JDK安装目录的几种常用方法,方法分别是通过update-alternatives、Java命令、环境变量及目... 目录方法 1:通过update-alternatives查询(推荐)方法 2:检查所有已安装的 JDK方

在macOS上安装jenv管理JDK版本的详细步骤

《在macOS上安装jenv管理JDK版本的详细步骤》jEnv是一个命令行工具,正如它的官网所宣称的那样,它是来让你忘记怎么配置JAVA_HOME环境变量的神队友,:本文主要介绍在macOS上安装... 目录前言安装 jenv添加 JDK 版本到 jenv切换 JDK 版本总结前言China编程在开发 Java