Exemplo n.º 1
0
 public void Serialise(UnsafePacker packer, T value) => _inner.Serialise(packer, value);
Exemplo n.º 2
0
 public UnsafePackerTests()
 {
     _stream   = new MemoryStream();
     _packer   = new UnsafePacker(_stream);
     _unpacker = MsgPack.Unpacker.Create(_stream);
 }
Exemplo n.º 3
0
//        [Fact]
        public void PackerPerf()
        {
            var s = new MemoryStream();

            var thisPacker       = new Packer(s);
            var thisUnsafePacker = new UnsafePacker(s);
            var thatPacker       = MsgPack.Packer.Create(s);

//            var str = new string('a', 256);
//            var bytes = new byte[256];

            const int loopCount = 1024 * 1024;

            Action thisBytePack = () =>
            {
                s.Position = 0;
                thisPacker.Pack(false);
                thisPacker.Pack(true);
                thisPacker.Pack((byte)1);
                thisPacker.Pack((sbyte)-1);
                thisPacker.Pack(1.1f);
                thisPacker.Pack(1.1d);
                thisPacker.Pack((short)1234);
                thisPacker.Pack((ushort)1234);
                thisPacker.Pack((int)1234);
                thisPacker.Pack((uint)1234);
                thisPacker.Pack((long)1234);
                thisPacker.Pack((ulong)1234);
                thisPacker.Pack("Hello World");
//                thisPacker.Pack(str);
//                thisPacker.Pack(bytes);
            };

            Action thisUnsafePack = () =>
            {
                s.Position = 0;
                thisUnsafePacker.Pack(false);
                thisUnsafePacker.Pack(true);
                thisUnsafePacker.Pack((byte)1);
                thisUnsafePacker.Pack((sbyte)-1);
                thisUnsafePacker.Pack(1.1f);
                thisUnsafePacker.Pack(1.1d);
                thisUnsafePacker.Pack((short)1234);
                thisUnsafePacker.Pack((ushort)1234);
                thisUnsafePacker.Pack((int)1234);
                thisUnsafePacker.Pack((uint)1234);
                thisUnsafePacker.Pack((long)1234);
                thisUnsafePacker.Pack((ulong)1234);
                thisUnsafePacker.Pack("Hello World");
//                thisUnsafePacker.Pack(str);
//                thisUnsafePacker.Pack(bytes);
                thisUnsafePacker.Flush();
            };

            Action thatPack = () =>
            {
                s.Position = 0;
                thatPacker.Pack(false);
                thatPacker.Pack(true);
                thatPacker.Pack((byte)1);
                thatPacker.Pack((sbyte)-1);
                thatPacker.Pack(1.1f);
                thatPacker.Pack(1.1d);
                thatPacker.Pack((short)1234);
                thatPacker.Pack((ushort)1234);
                thatPacker.Pack((int)1234);
                thatPacker.Pack((uint)1234);
                thatPacker.Pack((long)1234);
                thatPacker.Pack((ulong)1234);
                thatPacker.Pack("Hello World");
//                thatPacker.Pack(str);
//                thatPacker.Pack(bytes);
            };

            for (var i = 0; i < 10; i++)
            {
                thisBytePack();
                thisUnsafePack();
                thatPack();
            }

            var sw = Stopwatch.StartNew();

            for (var i = 0; i < loopCount; i++)
            {
                thisBytePack();
            }

            var thisBytePackTime = sw.Elapsed.TotalMilliseconds;

            sw.Restart();

            for (var i = 0; i < loopCount; i++)
            {
                thisUnsafePack();
            }

            var thisUnsafeTime = sw.Elapsed.TotalMilliseconds;

            sw.Restart();

            for (var i = 0; i < loopCount; i++)
            {
                thatPack();
            }

            var thatPackTime = sw.Elapsed.TotalMilliseconds;

            Assert.True(false, $"thisBytePackTime={thisBytePackTime}, thisUnsafeTime={thisUnsafeTime}, thatPackTime={thatPackTime}");
        }