public void SerializeToString_WhenDateTime_ExpectSerializeAndDeserialize()
        {
            // Arrange
            var protocolBufferSerializer = new ProtocolBufferSerializer(true);

            var testEntity = new TestEntity2
            {
                Value1 = new DateTime(2018, 1, 1),
                Value2 = 2.53m,
            };

            var serialized = testEntity.SerializeToByteArray().ToBase64String();
            var des        = serialized.DecodeBase64ToString().Deserialize <TestEntity2>();

            return;

            // Act
            var stopwatch                = Stopwatch.StartNew();
            var serializeToString        = protocolBufferSerializer.SerializeToString <TestEntity2>(testEntity);
            var deserializeFromByteArray = protocolBufferSerializer.DeserializeFromString(serializeToString, typeof(TestEntity2)) as TestEntity2;

            stopwatch.Stop();

            // Assert
            this.WriteTimeElapsed(stopwatch);

            Assert.That(deserializeFromByteArray.Value1, Is.EqualTo(new DateTime(2018, 1, 1)));
            Assert.That(deserializeFromByteArray.Value2, Is.EqualTo(2.53m));
        }
Exemplo n.º 2
0
        public void ToBase64String_WhenUsingString_ExpectDeserializeCorrect()
        {
            // Arrange
            var protocolBufferSerializer = new ProtocolBufferSerializer(true);

            var testEntity = TestHelper.GetDefaultTestEntity;

            // Act
            var stopwatch    = Stopwatch.StartNew();
            var serialized   = protocolBufferSerializer.SerializeToString(testEntity);
            var base64String = serialized.ToBase64String();

            Console.WriteLine("Protobuf String:");
            Console.WriteLine(serialized);
            Console.WriteLine("\r\nBase 64 String:");
            Console.WriteLine(base64String);

            var protobufString = base64String.DecodeBase64ToString();
            var deserialized   = protocolBufferSerializer.DeserializeFromString <TestEntity>(protobufString);

            stopwatch.Stop();

            // Assert
            this.WriteTimeElapsed(stopwatch);

            deserialized.AssertEqualsDefault();
        }
        public void SerializeToString_WhenUsingStringWithType_ExpectDeserializeCorrect()
        {
            // Arrange
            var protocolBufferSerializer = new ProtocolBufferSerializer(true);

            var testEntity = TestHelper.GetDefaultTestEntity;

            // Act
            var stopwatch         = Stopwatch.StartNew();
            var serializeToString = protocolBufferSerializer.SerializeToString(testEntity, typeof(TestEntity));

            Console.WriteLine(JsonConvert.SerializeObject(serializeToString));

            var deserializeFromByteArray = protocolBufferSerializer.DeserializeFromString(serializeToString, typeof(TestEntity));

            stopwatch.Stop();

            // Assert
            this.WriteTimeElapsed(stopwatch);

            deserializeFromByteArray.AssertEqualsDefault();
        }
Exemplo n.º 4
0
        public void ProtocolBuffersSerializerWithType_PerformanceTest()
        {
            const int Iterations = 100000;
            var       testEntity = TestHelper.GetDefaultTestEntity;
            var       type       = typeof(TestEntity);

            var serializeToStream    = new List <Stream>();
            var serializeToByteArray = default(byte[]);
            var serializeToString    = default(string);

            Console.WriteLine("Protocol Buffers Tests\r\n******");

            var protocolBufferSerializer = new ProtocolBufferSerializer(true);

            /* Serialize Tests */
            Console.WriteLine("SerializeToStream");
            var sw = Stopwatch.StartNew();

            for (var i = 0; i < Iterations; i++)
            {
                serializeToStream.Add(protocolBufferSerializer.SerializeToStream(testEntity, type));
            }

            sw.Stop();
            Console.WriteLine($"Test time: {sw.ElapsedMilliseconds}ms\r\n-----");

            Console.WriteLine("SerializeToByteArray");
            sw.Restart();
            for (var i = 0; i < Iterations; i++)
            {
                serializeToByteArray = protocolBufferSerializer.SerializeToByteArray(testEntity, type);
            }

            sw.Stop();
            Console.WriteLine($"Test time: {sw.ElapsedMilliseconds}ms\r\n-----");

            Console.WriteLine("SerializeToString");
            sw.Restart();
            for (var i = 0; i < Iterations; i++)
            {
                serializeToString = protocolBufferSerializer.SerializeToString(testEntity, type);
            }

            sw.Stop();
            Console.WriteLine($"Test time: {sw.ElapsedMilliseconds}ms\r\n-----");

            /* De-serialize Tests */
            Console.WriteLine("DeserializeFromStream");
            Console.WriteLine($"Length: {serializeToStream[0].Length}");
            sw.Restart();
            foreach (var stream in serializeToStream)
            {
                protocolBufferSerializer.DeserializeFromStream(stream, type);
            }

            sw.Stop();
            Console.WriteLine($"Test time: {sw.ElapsedMilliseconds}ms\r\n-----");

            Console.WriteLine("DeserializeFromByteArray");
            Console.WriteLine($"Length: {serializeToByteArray.Length}");
            sw.Restart();
            for (var i = 0; i < Iterations; i++)
            {
                protocolBufferSerializer.DeserializeFromByteArray(serializeToByteArray, type);
            }

            sw.Stop();
            Console.WriteLine($"Test time: {sw.ElapsedMilliseconds}ms\r\n-----");

            Console.WriteLine("DeserializeFromString");
            Console.WriteLine($"Length: {serializeToString.Length}");
            sw.Restart();
            for (var i = 0; i < Iterations; i++)
            {
                protocolBufferSerializer.DeserializeFromString(serializeToString, type);
            }

            sw.Stop();
            Console.WriteLine($"Test time: {sw.ElapsedMilliseconds}ms\r\n-----");

            Console.WriteLine($"\r\n***---TestEnded---***\r\n");
        }