コード例 #1
0
        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;

                byte result;
                Assert.True(_unpacker.ReadByte(out result));
                Assert.Equal(i, result);
            }
        }
コード例 #2
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}");
        }