Exemple #1
0
        public void TestLargeSerialization()
        {
            int[] selections = { 1 };
            var   serializer = new BinarySerialization.BinarySerializer();

            serializer.Endianness = BinarySerialization.Endianness.Big;

            var req = new SellBetRequest()
            {
                RacecardDate       = DateTime.UtcNow.ToString("ddMMyyyy"),
                BetCode            = Enums.BetCode.WIN,
                BetId              = 1,
                BetOption          = Enums.BetOption.STRAIGHT,
                NumberOfSelections = (ushort)selections.Length,
                Selections         = selections.Select(s => new Selection()
                {
                    MeetingNumber = 1,
                    RaceNumber    = 1,
                    HorseNumber   = (ushort)s,
                    IsBanker      = 0
                }).ToList(),
                TotalStake = 500000,
                UnitStake  = 500000
            };

            var stream = new MemoryStream();

            serializer.Serialize(stream, req);
            var buffer = stream.ToArray();

            Assert.AreEqual(buffer.Length, 57);
        }
Exemple #2
0
        private static void DoBSParallel <T>(T obj, int iterations)
        {
            var stopwatch = new Stopwatch();

            var ser = new BinarySerialization.BinarySerializer();

            stopwatch.Start();
            var data = Enumerable.Range(0, iterations).AsParallel().Select(i =>
            {
                using (var ms = new MemoryStream())
                {
                    ser.Serialize(ms, obj);
                    return(ms.ToArray());
                }
            }).ToArray();

            stopwatch.Stop();
            Console.WriteLine("BS || SER: {0}", stopwatch.Elapsed);
            stopwatch.Reset();

            stopwatch.Start();
            data.AsParallel().ForAll(d => ser.Deserialize <T>(d));
            stopwatch.Stop();
            Console.WriteLine("BS || DESER: {0}", stopwatch.Elapsed);
            stopwatch.Reset();
        }
        public void TestContext()
        {
            var contextClass = new ContextClass();
            var serializer = new BinarySerialization.BinarySerializer();

            var context = new Context {SerializeCondtion = false};

            var stream = new MemoryStream();
            serializer.Serialize(stream, contextClass, context);

            context = new Context { SerializeCondtion = true };

            stream = new MemoryStream();
            serializer.Serialize(stream, contextClass, context);

            Assert.AreEqual(sizeof(int), stream.Length);
        }
        private T Roundtrip <T>(T o, out byte[] data)
        {
            var stream = new MemoryStream();

            Serializer.Serialize(stream, o);

            stream.Position = 0;
            data            = stream.ToArray();
            return(Serializer.Deserialize <T>(stream));
        }
        public void TestSerializerEndianness()
        {
            var serializer = new BinarySerialization.BinarySerializer {Endianness = BinarySerialization.Endianness.Big};
            var expected = new EndiannessClass {Short = 1};

            var stream = new MemoryStream();
            serializer.Serialize(stream, expected);

            var data = stream.ToArray();

            Assert.AreEqual(0x1, data[1]);
        }
Exemple #6
0
        public void TestContext()
        {
            var contextClass = new ContextClass();
            var serializer   = new BinarySerialization.BinarySerializer();

            var context = new Context {
                SerializeCondtion = false
            };

            var stream = new MemoryStream();

            serializer.Serialize(stream, contextClass, context);

            context = new Context {
                SerializeCondtion = true
            };

            stream = new MemoryStream();
            serializer.Serialize(stream, contextClass, context);

            Assert.AreEqual(sizeof(int), stream.Length);
        }
        private static async Task DoBSAsync <T>(T obj, int iterations)
        {
            var stopwatch = new Stopwatch();

            var ser = new BinarySerialization.BinarySerializer();

            using (var ms = new MemoryStream())
            {
                stopwatch.Start();
                for (int i = 0; i < iterations; i++)
                {
                    ser.Serialize(ms, obj);
                }
                stopwatch.Stop();
                Console.WriteLine("BSA SER: {0}", stopwatch.Elapsed);
                stopwatch.Reset();
            }

            var dataStream = new MemoryStream();

            ser.Serialize(dataStream, obj);
            byte[] data = dataStream.ToArray();

            using (var ms = new MemoryStream(data))
            {
                stopwatch.Start();
                for (int i = 0; i < iterations; i++)
                {
                    await ser.DeserializeAsync <T>(ms)
                    .ConfigureAwait(false);

                    ms.Position = 0;
                }
                stopwatch.Stop();
                Console.WriteLine("BSA DESER: {0}", stopwatch.Elapsed);
                stopwatch.Reset();
            }
        }
        private static void DoBS <T>(T obj, int iterations)
        {
            var ser = new BinarySerialization.BinarySerializer();

            var stopwatch = new Stopwatch();

            using (var ms = new MemoryStream())
            {
                stopwatch.Start();
                for (int i = 0; i < iterations; i++)
                {
                    ser.Serialize(ms, obj);
                }
                stopwatch.Stop();
                System.Console.WriteLine("BS SER: {0}", stopwatch.Elapsed);
                stopwatch.Reset();
            }

            var dataStream = new MemoryStream();

            ser.Serialize(dataStream, obj);
            byte[] data = dataStream.ToArray();

            using (var ms = new MemoryStream(data))
            {
                stopwatch.Start();
                for (int i = 0; i < iterations; i++)
                {
                    ser.Deserialize <T>(ms);
                    ms.Position = 0;
                }
                stopwatch.Stop();
                System.Console.WriteLine("BS DESER: {0}", stopwatch.Elapsed);
                stopwatch.Reset();
            }
        }
Exemple #9
0
        public void TestSerializerEndianness()
        {
            var serializer = new BinarySerialization.BinarySerializer {
                Endianness = BinarySerialization.Endianness.Big
            };
            var expected = new EndiannessClass {
                Short = 1
            };

            var stream = new MemoryStream();

            serializer.Serialize(stream, expected);

            var data = stream.ToArray();

            Assert.AreEqual(0x1, data[1]);
        }
Exemple #10
0
        public void Test()
        {
            var source = new ChunkContainer
            {
                ChunkType = "TEST",
                Chunk     = new TestChunk
                {
                    Customs = new[]
                    {
                        new CustomSerializable
                        {
                            Value = 1
                        },
                        new CustomSerializable
                        {
                            Value = 2
                        }
                    }
                }
            };

            Roundtrip(source);

            var serializer   = new BinarySerialization.BinarySerializer();
            var outputStream = new MemoryStream();

            serializer.Serialize(outputStream, source);

            outputStream.Seek(0, SeekOrigin.Begin);
            var inputStream = new LooksLikeANetworkStream(outputStream); //non-seekable stream
            //var inputStream = outputStream; //successful

            var roundtrip = serializer.Deserialize <ChunkContainer>(inputStream);
            //Assert.That(roundtrip.Chunk, Is.InstanceOf<TestChunk>());

            //var sourceChunk = (TestChunk)source.Chunk;
            //var testChunk = (TestChunk)roundtrip.Chunk;
            //Assert.That(testChunk.Customs.Length, Is.EqualTo(sourceChunk.Customs.Length));
            //Assert.That(testChunk.Customs.ElementAt(0).Value, Is.EqualTo(sourceChunk.Customs.ElementAt(0).Value));
            //Assert.That(testChunk.Customs.ElementAt(1).Value, Is.EqualTo(sourceChunk.Customs.ElementAt(1).Value));
        }