Brief introduction
In recent years, a variety of new and efficient serialization methods have continued to refresh the upper limit of serialization performance, most typically including:
- Specifically for the Java language: KRYO,FST, etc.
- Cross-lingual: Protostuff,protobuf,thrift,avro,msgpack, etc.
Most of the performance of these serialization methods is significantly better than hessian2 (even including immature Dubbo serialization). For this reason, we have introduced two efficient Java serialization implementations for Dubbo, Kryo and FST, to gradually replace Hessian2. Among them, Kryo is a very mature serialization implementation that has been widely used in Twitter, Groupon, Yahoo, and several well-known open source projects such as Hive, Storm. While FST is a relatively new serialization implementation, there is still a lack of enough mature use cases, but it is still very promising, below we compare, Java native serialization Kryo serialization performance comparison
1. Entity class Simple.java
Packagebhz.entity;Importjava.io.Serializable;ImportJava.util.Map; Public classSimpleImplementsserializable{Private Static Final LongSerialversionuid = -4914434736682797743l; PrivateString name; Private intAge ; PrivateMap<string,integer>map; PublicSimple () {} PublicSimple (String name,intAge,map<string,integer>map) { This. Name =name; This. Age =Age ; This. Map =map; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; } PublicMap<string, integer>Getmap () {returnmap; } Public voidSetmap (map<string, integer>map) { This. Map =map; } }
2. Native serialization Originalserializable.java of Java
Packagebhz.test;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.ObjectInputStream;ImportJava.io.ObjectOutputStream;ImportJava.util.HashMap;ImportJava.util.Map;Importbhz.entity.Simple; Public classoriginalserializable { Public Static voidMain (string[] args)throwsIOException, ClassNotFoundException {LongStart =System.currenttimemillis (); Setserializableobject (); System.out.println ("Java Native serialization time:" + (System.currenttimemillis ()-start) + "MS"); Start=System.currenttimemillis (); Getserializableobject (); System.out.println ("Java Native deserialization time:" + (System.currenttimemillis ()-start) + "MS"); } Public Static voidSetserializableobject ()throwsioexception{fileoutputstream fo=NewFileOutputStream ("D:/file2.bin"); ObjectOutputStream So=NewObjectOutputStream (FO); for(inti = 0; I < 100000; i++) {Map<String,Integer> map =NewHashmap<string, integer> (2); Map.put ("ZHANG0", i); Map.put ("Zhang1", i); So.writeobject (NewSimple ("Zhang" +i, (i+1), map); } so.flush (); So.close (); } Public Static voidGetserializableobject () {FileInputStream fi; Try{fi=NewFileInputStream ("D:/file2.bin"); ObjectInputStream si=NewObjectInputStream (FI); Simple and 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 serialization of Kyroserializable.java
Packagebhz.test;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.util.HashMap;ImportJava.util.Map;ImportOrg.objenesis.strategy.StdInstantiatorStrategy;Importbhz.entity.Simple;ImportCom.esotericsoftware.kryo.Kryo;Importcom.esotericsoftware.kryo.KryoException;ImportCom.esotericsoftware.kryo.io.Input;ImportCom.esotericsoftware.kryo.io.Output; Public classkyroserializable { Public Static voidMain (string[] args)throwsIOException {LongStart =System.currenttimemillis (); Setserializableobject (); System.out.println ("Kryo Serialization Time:" + (System.currenttimemillis ()-start) + "MS" ); Start=System.currenttimemillis (); Getserializableobject (); System.out.println ("Kryo de-serialization time:" + (System.currenttimemillis ()-start) + "MS"); } Public Static voidSetserializableobject ()throwsfilenotfoundexception{Kryo Kryo=NewKryo (); Kryo.setreferences (false); Kryo.setregistrationrequired (false); Kryo.setinstantiatorstrategy (Newstdinstantiatorstrategy ()); Kryo.register (simple.class); Output Output=NewOutput (NewFileOutputStream ("D:/file1.bin")); for(inti = 0; I < 100000; i++) {Map<String,Integer> map =NewHashmap<string, integer> (2); Map.put ("ZHANG0", i); Map.put ("Zhang1", i); Kryo.writeobject (Output,NewSimple ("Zhang" +i, (i+1), map); } output.flush (); Output.close (); } Public Static voidGetserializableobject () {Kryo Kryo=NewKryo (); Kryo.setreferences (false); Kryo.setregistrationrequired (false); Kryo.setinstantiatorstrategy (Newstdinstantiatorstrategy ()); input input; Try{input=NewInput (NewFileInputStream ("D:/file1.bin")); Simple and 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. Comparison of test results
Java native serialization time: 8281 MS
Java Native deserialization time: 5899 ms
And
Kryo Serialization Time: 630 ms
Kryo deserialization Time: MS
By comparison, it can be found that Kryo is more than 10 times times the native serialization performance of Java
Comparison of Java native serialization and Kryo serialization performance