本文主要是介绍应用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性能对比的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!