java对象 序列化性能 比较 fst FastJSON kryo (无持久化)

java对象 序列化性能 比较 fst FastJSON kryo (无持久化)

  • 实验环境
    • 包版本
  • 实验结果与结论
    • 实验结果
    • 结论
    • 原因分析
  • 附件
    • 代码2
    • 代码1

实验环境

包版本

		
		    de.ruedigermoeller
		    fst
		    2.57
		

		
		    com.esotericsoftware
		    kryo
		    4.0.2
		
		
		
			com.alibaba
			fastjson
			1.2.49
		

实验结果与结论

实验结果

连续执行n次 序列化和反序列化 对比实验

#########200000条 #############
测试JSON
3137ms
测试FST
2335ms
测试kryo
7161ms
#########100000 条#############
测试JSON
515ms
测试FST
399ms
测试kryo
2809ms
######### 50000 条 #############
测试JSON
88ms
测试FST
104ms
测试kryo
1215ms
######### 10000 条###########
测试JSON
19ms
测试FST
18ms
测试kryo
235ms
######## 3000   条  ########
测试JSON
3ms
测试FST
5ms
测试kryo
71ms

结论

5万条以内 fastjson最快 ,适合中小网站处理
5万条以上 fst 最快, 适合大数据处理
kryo 很慢

原因分析

kryo 最慢的原因是 虚拟化 中用到了实例化
实例化对性能影响是致命的

附件

代码2

	    System.out.println("#########200000#############");
		testtimecache_0(200000);
		System.out.println("#########100000#############");
		testtimecache_0(100000);
		System.out.println("######### 50000 #############");
		testtimecache_0(50000);
		System.out.println("######### 10000 ###########");
		testtimecache_0(10000);
		System.out.println("######## 3000     ########");
		testtimecache_0(3000);
	
	///
	public void testtimecache_0(int max) throws Exception {
		System.out.println("测试JSON");
		long l = MyFun.getMs();
		byte[] bts;
		for (int i = 0; i < max; i++) {
			bts = JSON.toJSONBytes("v" + i);
			JSON.parseObject(bts, Object.class);
		}
		System.out.println((MyFun.getMs() - l)+"ms");
		///
		l = MyFun.getMs();
		bts = MyFst.serialize("测试FST");
		System.out.println(MyFst.unserialize(bts));
		for (int i = 0; i < max; i++) {
			bts = MyFst.serialize("v" + i);
			MyFst.unserialize(bts);
		}
		System.out.println((MyFun.getMs() - l)+"ms");

		bts = MyFst.kryoSerizlize("测试kryo");
		System.out.println(MyFst.kryoUnSerizlize(bts));
		l = MyFun.getMs();
		for (int i = 0; i < max; i++) {
			bts = MyFst.kryoSerizlize("v" + i);
			MyFst.kryoUnSerizlize(bts);
		}
		System.out.println((MyFun.getMs() - l)+"ms");
	}

代码1

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.nustaq.serialization.FSTConfiguration;
import org.nustaq.serialization.FSTObjectInput;
import org.nustaq.serialization.FSTObjectOutput;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;

public class MyFst {
	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  T myreadMethod(InputStream stream) throws Exception {
		FSTObjectInput in = configuration.getObjectInput(stream);
		Object result = in.readObject();
		// DON'T: in.close(); here prevents reuse and will result in an exception
		stream.close();
		return (T) result;
	}

	public  void mywriteMethod(OutputStream stream, T clazz) throws IOException {
		FSTObjectOutput out = configuration.getObjectOutput(stream);
		out.writeObject(clazz);
		// DON'T out.close() when using factory method;
		out.flush();
		stream.close();
	}

	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;
	}

}

你可能感兴趣的:(java,编程,序列化,java,fst,kryo,fastjson)