当前位置: 移动技术网 > IT编程>开发语言>Java > java原生序列化和Kryo序列化性能实例对比分析

java原生序列化和Kryo序列化性能实例对比分析

2019年07月19日  | 移动技术网IT编程  | 我要评论

简介

最近几年,各种新的高效序列化方式层出不穷,不断刷新序列化性能的上限,最典型的包括:

专门针对java语言的:kryo,fst等等

跨语言的:protostuff,protobuf,thrift,avro,msgpack等等

这些序列化方式的性能多数都显著优于hessian2(甚至包括尚未成熟的dubbo序列化)。有鉴于此,我们为dubbo引入kryo和fst这 两种高效java序列化实现,来逐步取代hessian2。其中,kryo是一种非常成熟的序列化实现,已经在twitter、groupon、 yahoo以及多个著名开源项目(如hive、storm)中广泛的使用。而fst是一种较新的序列化实现,目前还缺乏足够多的成熟使用案例,但它还是非 常有前途的,下面我们比较下,java原生序列化kryo序列化性能比较

1、实体类 simple.java

package bhz.entity;
import java.io.serializable;
import java.util.map;
public class simple implements serializable
{ 
   private static final long serialversionuid = -4914434736682797743l; 
   private string name; 
   private int age; 
   private map<string,integer> map; 
   public simple(){ 
   } 
   public simple(string name,int age,map<string,integer> map){ 
     this.name = name; 
     this.age = age; 
     this.map = map; 
   } 
   public string getname() { 
    return name; 
   } 
   public void setname(string name) { 
    this.name = name; 
   } 
   public int getage() { 
    return age; 
   } 
   public void setage(int age) { 
    this.age = age; 
   } 
   public map<string, integer> getmap() { 
    return map; 
   } 
   public void setmap(map<string, integer> map) { 
    this.map = map; 
   } 
} 

2、java原生序列化 originalserializable.java

package bhz.test;
import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.objectinputstream;
import java.io.objectoutputstream;
import java.util.hashmap;
import java.util.map;
import bhz.entity.simple;
public class originalserializable { 
  public static void main(string[] args) throws ioexception, classnotfoundexception { 
    long start = system.currenttimemillis(); 
    setserializableobject(); 
    system.out.println("java原生序列化时间:" + (system.currenttimemillis() - start) + " ms" );  
    start = system.currenttimemillis(); 
    getserializableobject(); 
    system.out.println("java原生反序列化时间:" + (system.currenttimemillis() - start) + " ms"); 
  } 
  public static void setserializableobject() throws ioexception{ 
    fileoutputstream fo = new fileoutputstream("d:/file2.bin"); 
    objectoutputstream so = new objectoutputstream(fo); 
    for (int i = 0; i < 100000; i++) { 
      map<string,integer> map = new hashmap<string, integer>(2); 
      map.put("zhang0", i); 
      map.put("zhang1", i); 
      so.writeobject(new simple("zhang"+i,(i+1),map)); 
    } 
    so.flush(); 
    so.close(); 
  } 
  public static void getserializableobject(){ 
     fileinputstream fi; 
    try { 
      fi = new fileinputstream("d:/file2.bin"); 
      objectinputstream si = new objectinputstream(fi); 
      simple simple =null; 
      while((simple=(simple)si.readobject()) != null){ 
        //system.out.println(simple.getage() + " " + simple.getname()); 
      } 
      fi.close(); 
      si.close(); 
    } catch (filenotfoundexception e) { 
      e.printstacktrace(); 
    } catch (ioexception e) { 
      //e.printstacktrace(); 
    } catch (classnotfoundexception e) { 
      e.printstacktrace(); 
    } 
  } 
} 

3、kyro序列化 kyroserializable.java

package bhz.test;
import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.util.hashmap;
import java.util.map;
import org.objenesis.strategy.stdinstantiatorstrategy;
import bhz.entity.simple;
import com.esotericsoftware.kryo.kryo;
import com.esotericsoftware.kryo.kryoexception;
import com.esotericsoftware.kryo.io.input;
import com.esotericsoftware.kryo.io.output;
public class kyroserializable { 
  public static void main(string[] args) throws ioexception { 
    long start = system.currenttimemillis(); 
    setserializableobject(); 
    system.out.println("kryo 序列化时间:" + (system.currenttimemillis() - start) + " ms" ); 
    start = system.currenttimemillis(); 
    getserializableobject(); 
    system.out.println("kryo 反序列化时间:" + (system.currenttimemillis() - start) + " ms"); 
  } 
  public static void setserializableobject() throws filenotfoundexception{ 
    kryo kryo = new kryo(); 
    kryo.setreferences(false); 
    kryo.setregistrationrequired(false); 
    kryo.setinstantiatorstrategy(new stdinstantiatorstrategy()); 
    kryo.register(simple.class); 
    output output = new output(new fileoutputstream("d:/file1.bin")); 
    for (int i = 0; i < 100000; i++) { 
      map<string,integer> map = new hashmap<string, integer>(2); 
      map.put("zhang0", i); 
      map.put("zhang1", i); 
      kryo.writeobject(output, new simple("zhang"+i,(i+1),map)); 
    } 
    output.flush(); 
    output.close(); 
  } 
  public static void getserializableobject(){ 
    kryo kryo = new kryo(); 
    kryo.setreferences(false); 
    kryo.setregistrationrequired(false); 
    kryo.setinstantiatorstrategy(new stdinstantiatorstrategy()); 
    input input; 
    try { 
      input = new input(new fileinputstream("d:/file1.bin")); 
      simple simple =null; 
      while((simple=kryo.readobject(input, simple.class)) != null){ 
        //system.out.println(simple.getage() + " " + simple.getname() + " " + simple.getmap().tostring()); 
      } 
      input.close(); 
    } catch (filenotfoundexception e) { 
      e.printstacktrace(); 
    } catch(kryoexception e){ 
    } 
  } 
} 

 4、测试结果对比

java原生序列化时间:8281 ms

java原生反序列化时间:5899 ms


kryo 序列化时间:630 ms

kryo 反序列化时间:15 ms

经过对比,可以发现kryo是java原生序列化性能十几倍

总结

以上就是本文关于java原生序列化和kryo序列化性能实例对比分析的全部内容,希望对大家有所帮助,感兴趣的朋友可以参考:kryo框架使用方法代码示例  实例解析json反序列化之objectmapper(自定义实现反序列化方法)  浅谈java序列化和hessian序列化的差异等,有什么问题可以随时留言,小编必定及时回复大家,感谢朋友们对本站的支持。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网