Fst_ serialization of high performance serialization framework

Source: Internet
Author: User
Tags serialization zookeeper
FST is a serialized framework for a fully compliant JDK serialization protocol, which is about 4-10 times the size of the JDK and about 1/3 of the JDK size.
First introduce POM
<dependency>
  <groupId>de.ruedigermoeller</groupId>
  <artifactid>fst</ artifactid>
  <version>2.04</version>
</dependency>
Test code
Package Zookeeper.seria;

Import java.io.Serializable;
		public class Fstseriazle {public static void main (string[] args) {User Bean = new user ();
		Bean.setusername ("xxxxx");
		Bean.setpassword ("123456");
		Bean.setage (1000000);
		System.out.println ("serialization, deserialization comparison test:");
		Long size = 0;
		Long time1 = System.currenttimemillis ();
			for (int i = 0; I < 10000 i++) {byte[] jdkserialize = jredisserializationutils.jdkserialize (bean);
			Size + = Jdkserialize.length;
		Jredisserializationutils.jdkdeserialize (jdkserialize);

		System.out.println ("native serialization scheme [serialization 10,000 times] time consuming:" + (System.currenttimemillis ()-time1) + "Ms Size:=" + size);
		size = 0;
		Long time2 = System.currenttimemillis ();
			for (int i = 0; I < 10000 i++) {byte[] serialize = jredisserializationutils.serialize (bean);
			Size + = Serialize.length;
		User U = (user) jredisserializationutils.unserialize (serialize); System.out.println ("FST serialization scenario [serialization 10,000 times] time consuming:" + (System.currenttimemillis ()-time2)+ "Ms Size:=" + size);
		size = 0;
		Long Time3 = System.currenttimemillis ();
			for (int i = 0; I < 10000 i++) {byte[] serialize = jredisserializationutils.kryoserizlize (bean);
			Size + = Serialize.length;
		User U = (user) jredisserializationutils.kryounserizlize (serialize);

	System.out.println ("Kryo serialization scenario [serialization 10,000 times] is time-consuming:" + (System.currenttimemillis ()-Time3) + "Ms Size:=" + size);
	The class User implements serializable{private String username;
	private int age;

	private String password;
	Public String GetUserName () {return username;
	} public void Setusername (String username) {this.username = username;
	public int getage () {return age;
	public void Setage (int age) {this.age = age;
	Public String GetPassword () {return password;
	} public void SetPassword (String password) {this.password = password; }

}
Results
Serialization, deserialization comparison test:
native serialization scheme [serialization 10,000 times] time consuming: 458ms size:=1160000
FST serialization scenario [serialization 10,000 times] time consuming: 184ms size:=550000
Kryo serialization scenario [serialization 10,000 times] time consuming: 462ms size:=390000
Tool class
Package Zookeeper.seria;
Import Java.io.ByteArrayInputStream;
Import Java.io.ByteArrayOutputStream;
Import java.io.IOException;
Import Java.io.ObjectInputStream;

Import Java.io.ObjectOutputStream;

Import org.nustaq.serialization.FSTConfiguration;
Import Com.esotericsoftware.kryo.Kryo;
Import Com.esotericsoftware.kryo.io.Input;

Import Com.esotericsoftware.kryo.io.Output;  public class Jredisserializationutils {public Jredisserializationutils () {} static fstconfiguration configuration =
			Fstconfiguration//. Createdefaultconfiguration ();

	. Createstructconfiguration ();
	public static byte[] Serialize (Object obj) {return configuration.asbytearray (obj);
	public static Object Unserialize (byte[] sec) {return Configuration.asobject (sec);
		public static byte[] Kryoserizlize (Object obj) {kryo Kryo = new Kryo ();
		byte[] buffer = new byte[2048];
				Try (Output output = new output (buffer);
			) {kryo.writeclassandobject (output, obj); Return Output.tobytES ();
	catch (Exception e) {} return buffer;
	static Kryo Kryo = new Kryo ();
				public static Object kryounserizlize (byte[] src) {try (Input input = new input (SRC);
		) {return kryo.readclassandobject (input);
	}catch (Exception e) {} return kryo; }//JDK native sequence Exchange scheme public static byte[] Jdkserialize (Object obj) {try (bytearrayoutputstream BAOs = new Bytearrayoutput
				Stream (); ObjectOutputStream oos = new ObjectOutputStream (BAOs);)
			{oos.writeobject (obj);
		return Baos.tobytearray ();
		catch (IOException e) {throw new RuntimeException (e); } public static Object jdkdeserialize (byte[] bits) {try (Bytearrayinputstream Bais = new Bytearrayinputstream (bits
				);

		ObjectInputStream ois = new ObjectInputStream (Bais);
		) {return ois.readobject ();
		catch (Exception e) {throw new RuntimeException (e); }
	}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.