1. Mito
2. Overview
In recent years, various new efficient serialization methods have emerged endlessly, constantly updating the upper limit of serialization performance. The most typical ones include:
Java-specific: Kryo, FST, etc.
Cross-language: Protostuff, ProtoBuf, Thrift, Avro MsgPack, etc.
The performance of most of these serialization methods is significantly better than hessian2 (even including the premature dubbo serialization). In view of this, we introduced two efficient Java serialization implementations, Kryo and FST, for dubbo to gradually replace hessian2. Among them, Kryo is a very mature serialization implementation, which has been widely used in Twitter, Groupon, Yahoo, and many well-known open source projects (such as Hive, Storm). The FST is a newer serialization implementation, and there are still not 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
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 native serialization 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 serialization 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. Comparison of test results
java原生序列化时间:8281 ms
java原生反序列化时间:5899 ms
with
Kryo 序列化时间:630 ms
Kryo 反序列化时间:15 ms
After comparison, it can be found that kryo is more than ten times the performance of java native serialization
https://www.jb51.net/article/125245.htm