public void HandlesRecurringType() { var stream = new MemoryStream(); var packer = new Packer(stream); packer.PackMapHeader(2); packer.Pack("Num"); packer.Pack(1); packer.Pack("Inner"); { packer.PackMapHeader(2); packer.Pack("Num"); packer.Pack(2); packer.Pack("Inner"); packer.PackNull(); } packer.Flush(); stream.Position = 0; var after = new Deserialiser <Recurring>().Deserialise(stream); Assert.Equal(1, after.Num); Assert.NotNull(after.Inner); Assert.Equal(2, after.Inner.Num); Assert.Null(after.Inner.Inner); }
protected override void PackToCore(Packer packer, NvimMessage message) { message.TypeId = message.GetType() .GetCustomAttribute <NvimMessageTypeAttribute>().Id; packer.PackObject(message); packer.Flush(); }
public void HandlesNullRootObject() { var stream = new MemoryStream(); var packer = new Packer(stream); packer.PackNull(); packer.Flush(); stream.Position = 0; Assert.Null(new Deserialiser <Recurring>().Deserialise(stream)); }
private static void Main() { var stream = new MemoryStream(); var packer = new Packer(stream); packer.PackMapHeader(8); packer.Pack("Int32"); packer.Pack(1234); packer.Pack("Null"); packer.PackNull(); packer.Pack("String"); packer.Pack("Hello, World!"); packer.Pack("Float32"); packer.Pack(12.34f); packer.Pack("Float64"); packer.Pack(12.34d); packer.Pack("True"); packer.Pack(true); packer.Pack("False"); packer.Pack(true); packer.Pack("Array"); packer.PackArrayHeader(3); packer.Pack(1234); packer.PackNull(); packer.Pack("Foo"); packer.Flush(); var bytes = stream.GetBuffer(); Console.WriteLine($"Packed {bytes.Length} bytes"); // Show the visualizer (via code) var developmentHost = new VisualizerDevelopmentHost( bytes, typeof(MsgPackByteArrayVisualizer), typeof(MsgPackByteArrayObjectSource) ); developmentHost.ShowVisualizer(); }
public void HandlesNullNestedObject() { var stream = new MemoryStream(); var packer = new Packer(stream); packer.PackMapHeader(2); packer.Pack("Num"); packer.Pack(1); packer.Pack("Inner"); packer.PackNull(); packer.Flush(); stream.Position = 0; Assert.Null(new Deserialiser <Recurring>().Deserialise(stream).Inner); }
public void PacksByte() { for (var i = (int)byte.MinValue; i <= byte.MaxValue; i++) { _stream.Position = 0; _packer.Pack((byte)i); _packer.Flush(); _stream.Position = 0; Assert.True(_unpacker.ReadByte(out byte result)); Assert.Equal(i, result); } }
public void PackerPerf() { var s = new MemoryStream(); var dasherPacker = new Packer(s); var msgPackCliPacker = MsgPack.Packer.Create(s); // var str = new string('a', 256); // var bytes = new byte[256]; const int loopCount = 1024 * 1024; void DasherPack() { s.Position = 0; dasherPacker.Pack(false); dasherPacker.Pack(true); dasherPacker.Pack((byte)1); dasherPacker.Pack((sbyte)-1); dasherPacker.Pack(1.1f); dasherPacker.Pack(1.1d); dasherPacker.Pack((short)1234); dasherPacker.Pack((ushort)1234); dasherPacker.Pack((int)1234); dasherPacker.Pack((uint)1234); dasherPacker.Pack((long)1234); dasherPacker.Pack((ulong)1234); dasherPacker.Pack("Hello World"); // dasherPacker.Pack(str); // dasherPacker.Pack(bytes); dasherPacker.Flush(); } void MsgPackCliPack() { s.Position = 0; msgPackCliPacker.Pack(false); msgPackCliPacker.Pack(true); msgPackCliPacker.Pack((byte)1); msgPackCliPacker.Pack((sbyte)-1); msgPackCliPacker.Pack(1.1f); msgPackCliPacker.Pack(1.1d); msgPackCliPacker.Pack((short)1234); msgPackCliPacker.Pack((ushort)1234); msgPackCliPacker.Pack((int)1234); msgPackCliPacker.Pack((uint)1234); msgPackCliPacker.Pack((long)1234); msgPackCliPacker.Pack((ulong)1234); msgPackCliPacker.Pack("Hello World"); // msgPackCliPacker.Pack(str); // msgPackCliPacker.Pack(bytes); } for (var i = 0; i < 10; i++) { DasherPack(); MsgPackCliPack(); } var sw = Stopwatch.StartNew(); for (var i = 0; i < loopCount; i++) { DasherPack(); } var unsafePackTime = sw.Elapsed.TotalMilliseconds; sw.Restart(); for (var i = 0; i < loopCount; i++) { MsgPackCliPack(); } var msgPackCliPackTime = sw.Elapsed.TotalMilliseconds; TestOutput.WriteLine($"{nameof(unsafePackTime)}={unsafePackTime}, {nameof(msgPackCliPackTime)}={msgPackCliPackTime}"); }
public void SerialisationPerf() { #if DEBUG Assert.True(false, "Performance comparison must be performed on a release build."); #endif var dasherSer = new Serialiser <UserScore>(); var dasherPacker = new Packer(Stream.Null); var cliSer = MessagePackSerializer.Get <UserScore>(new SerializationContext { CompatibilityOptions = { PackerCompatibilityOptions = PackerCompatibilityOptions.None }, SerializationMethod = SerializationMethod.Map }); var cliPacker = MsgPack.Packer.Create(Stream.Null, PackerCompatibilityOptions.None); var obj = new UserScore("Bob", 1234); const int warmUpLoopCount = 1000; const int timedLoopCount = 10 * 1000 * 1000; var sw = Stopwatch.StartNew(); //// for (var i = 0; i < warmUpLoopCount; i++) { cliSer.PackTo(cliPacker, obj); } TestUtils.CleanUpForPerfTest(); sw.Restart(); for (var i = 0; i < timedLoopCount; i++) { cliSer.PackTo(cliPacker, obj); } var cliMs = sw.Elapsed.TotalMilliseconds; //// for (var i = 0; i < warmUpLoopCount; i++) { dasherSer.Serialise(dasherPacker, obj); dasherPacker.Flush(); } TestUtils.CleanUpForPerfTest(); sw.Restart(); for (var i = 0; i < timedLoopCount; i++) { dasherSer.Serialise(dasherPacker, obj); dasherPacker.Flush(); } var dasherMs = sw.Elapsed.TotalMilliseconds; //// TestOutput.WriteLine($"{nameof(dasherMs)}={dasherMs} {nameof(cliMs)}={cliMs}"); // serialisation performance is on par with MsgPack.Cli. should always be within 10%. Assert.True(dasherMs < cliMs * 1.1, $"{nameof(dasherMs)}={dasherMs} {nameof(cliMs)}={cliMs}"); }