簡介
最近幾年,各種新的高效序列化方式層出不窮,不斷刷新序列化性能的上限,最典型的包括:
專門針對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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
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序列化性能實例對比分析的全部內容,希望對大家有所幫助,有什么問題可以隨時留言,小編必定及時回復大家,感謝朋友們對本站的支持。
原文鏈接:http://www.cnblogs.com/520playboy/p/6341490.html