高性能序列化框架FST

fst是完全兼容JDK序列化协议的系列化框架,序列化速度大概是JDK的4-10倍,大小是JDK大小的1/3左右。
首先引入pom

Xml代码   收藏代码
  1. <dependency>  
  2.   <groupId>de.ruedigermoeller</groupId>  
  3.   <artifactId>fst</artifactId>  
  4.   <version>2.04</version>  
  5. </dependency>  
 测试代码
Java代码   收藏代码
  1. package zookeeper.seria;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. public class FSTSeriazle {  
  6.   
  7.     public static void main(String[] args) {  
  8.         User bean = new User();  
  9.         bean.setUsername("xxxxx");  
  10.         bean.setPassword("123456");  
  11.         bean.setAge(1000000);  
  12.         System.out.println("序列化 , 反序列化 对比测试:");  
  13.         long size = 0;  
  14.         long time1 = System.currentTimeMillis();  
  15.         for (int i = 0; i < 10000; i++) {  
  16.             byte[] jdkserialize = JRedisSerializationUtils.jdkserialize(bean);  
  17.             size += jdkserialize.length;  
  18.             JRedisSerializationUtils.jdkdeserialize(jdkserialize);  
  19.         }  
  20.         System.out.println("原生序列化方案[序列化10000次]耗时:"  
  21.                 + (System.currentTimeMillis() - time1) + "ms size:=" + size);  
  22.   
  23.         size = 0;  
  24.         long time2 = System.currentTimeMillis();  
  25.         for (int i = 0; i < 10000; i++) {  
  26.             byte[] serialize = JRedisSerializationUtils.serialize(bean);  
  27.             size += serialize.length;  
  28.             User u = (User) JRedisSerializationUtils.unserialize(serialize);  
  29.         }  
  30.         System.out.println("fst序列化方案[序列化10000次]耗时:"  
  31.                 + (System.currentTimeMillis() - time2) + "ms size:=" + size);  
  32.         size = 0;  
  33.         long time3 = System.currentTimeMillis();  
  34.         for (int i = 0; i < 10000; i++) {  
  35.             byte[] serialize = JRedisSerializationUtils.kryoSerizlize(bean);  
  36.             size += serialize.length;  
  37.             User u = (User) JRedisSerializationUtils.kryoUnSerizlize(serialize);  
  38.         }  
  39.         System.out.println("kryo序列化方案[序列化10000次]耗时:"  
  40.                 + (System.currentTimeMillis() - time3) + "ms size:=" + size);  
  41.   
  42.     }  
  43.   
  44. }  
  45.   
  46. class User implements Serializable{  
  47.   
  48.     private String username;  
  49.     private int age;  
  50.     private String password;  
  51.   
  52.     public String getUsername() {  
  53.         return username;  
  54.     }  
  55.   
  56.     public void setUsername(String username) {  
  57.         this.username = username;  
  58.     }  
  59.   
  60.     public int getAge() {  
  61.         return age;  
  62.     }  
  63.   
  64.     public void setAge(int age) {  
  65.         this.age = age;  
  66.     }  
  67.   
  68.     public String getPassword() {  
  69.         return password;  
  70.     }  
  71.   
  72.     public void setPassword(String password) {  
  73.         this.password = password;  
  74.     }  
  75.   
  76. }  
 结果
Java代码   收藏代码
  1. 序列化 , 反序列化 对比测试:  
  2. 原生序列化方案[序列化10000次]耗时:458ms size:=1160000  
  3. fst序列化方案[序列化10000次]耗时:184ms size:=550000  
  4. kryo序列化方案[序列化10000次]耗时:462ms size:=390000  
 工具类
Java代码   收藏代码
  1. package zookeeper.seria;  
  2.   
  3. import java.io.ByteArrayInputStream;  
  4. import java.io.ByteArrayOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.ObjectInputStream;  
  7. import java.io.ObjectOutputStream;  
  8.   
  9. import org.nustaq.serialization.FSTConfiguration;  
  10.   
  11. import com.esotericsoftware.kryo.Kryo;  
  12. import com.esotericsoftware.kryo.io.Input;  
  13. import com.esotericsoftware.kryo.io.Output;  
  14.   
  15. public class JRedisSerializationUtils {  
  16.   
  17.     public JRedisSerializationUtils() {  
  18.     }  
  19.   
  20.     static FSTConfiguration configuration = FSTConfiguration  
  21.     // .createDefaultConfiguration();  
  22.             .createStructConfiguration();  
  23.   
  24.     public static byte[] serialize(Object obj) {  
  25.         return configuration.asByteArray(obj);  
  26.     }  
  27.   
  28.     public static Object unserialize(byte[] sec) {  
  29.         return configuration.asObject(sec);  
  30.     }  
  31.   
  32.     public static byte[] kryoSerizlize(Object obj) {  
  33.         Kryo kryo = new Kryo();  
  34.         byte[] buffer = new byte[2048];  
  35.         try(  
  36.                 Output output = new Output(buffer);  
  37.                 ) {  
  38.               
  39.             kryo.writeClassAndObject(output, obj);  
  40.             return output.toBytes();  
  41.         } catch (Exception e) {  
  42.         }  
  43.         return buffer;  
  44.     }  
  45.   
  46.     static Kryo kryo = new Kryo();  
  47.     public static Object kryoUnSerizlize(byte[] src) {  
  48.         try(  
  49.         Input input = new Input(src);  
  50.                 ){  
  51.             return kryo.readClassAndObject(input);  
  52.         }catch (Exception e) {  
  53.         }  
  54.         return kryo;  
  55.     }  
  56.   
  57.     // jdk原生序列换方案  
  58.     public static byte[] jdkserialize(Object obj) {  
  59.         try (ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  60.                 ObjectOutputStream oos = new ObjectOutputStream(baos);) {  
  61.             oos.writeObject(obj);  
  62.             return baos.toByteArray();  
  63.         } catch (IOException e) {  
  64.             throw new RuntimeException(e);  
  65.         }  
  66.     }  
  67.   
  68.     public static Object jdkdeserialize(byte[] bits) {  
  69.         try (ByteArrayInputStream bais = new ByteArrayInputStream(bits);  
  70.                 ObjectInputStream ois = new ObjectInputStream(bais);  
  71.   
  72.         ) {  
  73.             return ois.readObject();  
  74.         } catch (Exception e) {  
  75.             throw new RuntimeException(e);  
  76.         }  
  77.     }  
  78. }  

http://liuyieyer.iteye.com/blog/2136240

猜你喜欢

转载自m635674608.iteye.com/blog/2382106
今日推荐