public static void Serialize(Stream stream, object obj) { LZ4MessagePackSerializer.Serialize(stream, obj, defaultResolver); }
private static void Benchmark <T>(T target) { const int Iteration = 1000000; // 10000 var jsonSerializer = new JsonSerializer(); var msgpack = MsgPack.Serialization.SerializationContext.Default; msgpack.GetSerializer <T>().PackSingleObject(target); DefaultSerializer.Serialize(target); LZ4Serializer.Serialize(target); ZeroFormatter.ZeroFormatterSerializer.Serialize(target); ProtoBuf.Serializer.Serialize(new MemoryStream(), target); jsonSerializer.Serialize(new JsonTextWriter(new StringWriter()), target); Console.WriteLine(typeof(T).Name + " serialization test"); Console.WriteLine(); Console.WriteLine("Serialize::"); byte[] data = null; byte[] data0 = null; byte[] data1 = null; byte[] data2 = null; byte[] data3 = null; byte[] dataJson = null; byte[] dataGzipJson = null; using (new Measure("MsgPack-Cli")) { for (int i = 0; i < Iteration; i++) { data = msgpack.GetSerializer <T>().PackSingleObject(target); } } using (new Measure("MessagePack-CSharp")) { for (int i = 0; i < Iteration; i++) { data0 = DefaultSerializer.Serialize(target); } } using (new Measure("MessagePack(LZ4)")) { for (int i = 0; i < Iteration; i++) { data3 = LZ4Serializer.Serialize(target); } } using (new Measure("ZeroFormatter")) { for (int i = 0; i < Iteration; i++) { data1 = ZeroFormatter.ZeroFormatterSerializer.Serialize(target); } } using (new Measure("JsonNet")) { for (int i = 0; i < Iteration; i++) { using (var ms = new MemoryStream()) using (var sw = new StreamWriter(ms, Encoding.UTF8, 1024, true)) using (var jw = new JsonTextWriter(sw)) { jsonSerializer.Serialize(jw, target); } } } using (new Measure("JsonNet+Gzip")) { for (int i = 0; i < Iteration; i++) { using (var ms = new MemoryStream()) using (var gzip = new GZipStream(ms, CompressionLevel.Fastest)) using (var sw = new StreamWriter(gzip, Encoding.UTF8, 1024, true)) using (var jw = new JsonTextWriter(sw)) { jsonSerializer.Serialize(jw, target); } } } using (new Measure("protobuf-net")) { for (int i = 0; i < Iteration; i++) { using (var ms = new MemoryStream()) { ProtoBuf.Serializer.Serialize(ms, target); } } } using (var ms = new MemoryStream()) { ProtoBuf.Serializer.Serialize(ms, target); data2 = ms.ToArray(); } using (var ms = new MemoryStream()) { using (var sw = new StreamWriter(ms, Encoding.UTF8, 1024, true)) using (var jw = new JsonTextWriter(sw)) { jsonSerializer.Serialize(jw, target); } dataJson = ms.ToArray(); } using (var ms = new MemoryStream()) { using (var gzip = new GZipStream(ms, CompressionLevel.Fastest)) using (var sw = new StreamWriter(gzip, Encoding.UTF8, 1024, true)) using (var jw = new JsonTextWriter(sw)) { jsonSerializer.Serialize(jw, target); } dataGzipJson = ms.ToArray(); } msgpack.GetSerializer <T>().UnpackSingleObject(data); DefaultSerializer.Deserialize <T>(data0); //ZeroFormatterSerializer.Deserialize<T>(data1); ProtoBuf.Serializer.Deserialize <T>(new MemoryStream(data2)); LZ4Serializer.Deserialize <T>(data3); jsonSerializer.Deserialize <T>(new JsonTextReader(new StreamReader(new MemoryStream(dataJson)))); Console.WriteLine(); Console.WriteLine("Deserialize::"); using (new Measure("MsgPack-Cli")) { for (int i = 0; i < Iteration; i++) { msgpack.GetSerializer <T>().UnpackSingleObject(data); } } using (new Measure("MessagePack-CSharp")) { for (int i = 0; i < Iteration; i++) { DefaultSerializer.Deserialize <T>(data0); } } using (new Measure("MessagePack(LZ4)")) { for (int i = 0; i < Iteration; i++) { LZ4Serializer.Deserialize <T>(data3); } } using (new Measure("ZeroFormatter")) { for (int i = 0; i < Iteration; i++) { ZeroFormatterSerializer.Deserialize <T>(data1); } } using (new Measure("JsonNet")) { for (int i = 0; i < Iteration; i++) { using (var ms = new MemoryStream(dataJson)) using (var sr = new StreamReader(ms, Encoding.UTF8)) using (var jr = new JsonTextReader(sr)) { jsonSerializer.Deserialize <T>(jr); } } } using (new Measure("JsonNet+Gzip")) { for (int i = 0; i < Iteration; i++) { using (var ms = new MemoryStream(dataGzipJson)) using (var gzip = new GZipStream(ms, CompressionMode.Decompress)) using (var sr = new StreamReader(gzip, Encoding.UTF8)) using (var jr = new JsonTextReader(sr)) { jsonSerializer.Deserialize <T>(jr); } } } using (new Measure("protobuf-net")) { for (int i = 0; i < Iteration; i++) { using (var ms = new MemoryStream(data2)) { ProtoBuf.Serializer.Deserialize <T>(ms); } } } Console.WriteLine(); Console.WriteLine("FileSize::"); var label = ""; label = "MsgPack-Cli"; Console.WriteLine($"{label,20} {data.Length} Byte"); label = "MessagePack-CSharp"; Console.WriteLine($"{label,20} {data0.Length} Byte"); label = "MessagePack(LZ4)"; Console.WriteLine($"{label,20} {data3.Length} Byte"); label = "ZeroFormatter"; Console.WriteLine($"{label,20} {data1.Length} Byte"); label = "protobuf-net"; Console.WriteLine($"{label,20} {data2.Length} Byte"); label = "JsonNet"; Console.WriteLine($"{label,20} {dataJson.Length} Byte"); label = "JsonNet+GZip"; Console.WriteLine($"{label,20} {dataGzipJson.Length} Byte"); Console.WriteLine(); Console.WriteLine(); }
public static byte[] Serialize(object obj) { return(LZ4MessagePackSerializer.Serialize(obj, defaultResolver)); }
public static void Serialize <T>(IArrayBufferWriter <byte> output, T obj) { LZ4MessagePackSerializer.Serialize(output, obj, defaultResolver); }