This article mainly introduces the comparative analysis of Java native serialization and Kryo serialization performance examples, involving Java and kryo serialization and deserialization related examples. The editor thinks it is very good, and I share it with you here. I hope to give you a refer to.
Introduction
In recent years, various new efficient serialization methods have emerged in an endless stream, constantly refreshing the upper limit of serialization performance. The most typical ones include:
Specifically for Java language: 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 immature 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 that has been widely used in Twitter, Groupon, Yahoo, and many well-known open source projects (such as Hive and Storm). FST is a newer serialization implementation that currently lacks enough mature use cases, but it is still very promising. Let’s compare the performance of java native serialization and Kryo serialization
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 native serialization time: 8281 ms
java native deserialization time: 5899 ms
and
Kryo serialization time: 630 ms
Kryo deserialization time: 15 ms
After comparison, it can be found that kryo is more than ten times better than Java's native serialization performance
Summary
The above is the detailed content of Examples of performance comparison analysis between Java native serialization and Kryo serialization. For more information, please follow other related articles on the PHP Chinese website!