public void ToBase64String_WhenUsingByteArray_ExpectDeserializeCorrect()
        {
            // Arrange
            var protocolBufferSerializer = new ProtocolBufferSerializer(false);

            var testEntity = TestHelper.GetDefaultTestEntity;

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

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

            var protobufBytes = base64String.DecodeBase64ToBytes();
            var deserialized  = protocolBufferSerializer.DeserializeFromByteArray <TestEntity>(protobufBytes);

            stopwatch.Stop();

            // Assert
            this.WriteTimeElapsed(stopwatch);

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

            var testEntity = TestHelper.GetDefaultTestEntity;

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

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

            var deserializeFromByteArray = protocolBufferSerializer.DeserializeFromByteArray(serializeToByteArray, typeof(TestEntity));

            stopwatch.Stop();

            // Assert
            this.WriteTimeElapsed(stopwatch);

            deserializeFromByteArray.AssertEqualsDefault();
        }
Esempio n. 3
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");
        }