public void Benchmark()
        {
            const int Iterations = 50000;

            var xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(ContainerWithAbstract), null, null, null, null);
            var customSerializer = CustomSerializer.GetSerializer(typeof(ContainerWithInterface), null, TestXmlSerializerOptions.Empty);

            var xmlSerializerStopwatch = Stopwatch.StartNew();

            for (int i = 0; i < Iterations; i++)
            {
                using (var stringReader = new StringReader(_xmlWithAbstract))
                {
                    using (var reader = new XmlTextReader(stringReader))
                    {
                        xmlSerializer.Deserialize(reader);
                    }
                }
            }

            xmlSerializerStopwatch.Stop();

            var options = new TestSerializeOptions();

            var customSerializerStopwatch = Stopwatch.StartNew();

            for (int i = 0; i < Iterations; i++)
            {
                using (var stringReader = new StringReader(_xmlWithInterface))
                {
                    using (var xmlReader = new XmlTextReader(stringReader))
                    {
                        using (var reader = new XSerializerXmlReader(xmlReader, options.GetEncryptionMechanism(), options.EncryptKey, options.SerializationState))
                        {
                            customSerializer.DeserializeObject(reader, options);
                        }
                    }
                }
            }

            customSerializerStopwatch.Stop();

            Console.WriteLine("XmlSerializer Elapsed Time: {0}", xmlSerializerStopwatch.Elapsed);
            Console.WriteLine("CustomSerializer Elapsed Time: {0}", customSerializerStopwatch.Elapsed);
        }
        public void Benchmark()
        {
            const int Iterations = 50000;

            var xmlSerializer    = new System.Xml.Serialization.XmlSerializer(typeof(ContainerWithAbstract), null, null, null, null);
            var customSerializer = CustomSerializer.GetSerializer(typeof(ContainerWithInterface), null, TestXmlSerializerOptions.Empty);

            var xmlSerializerStopwatch = Stopwatch.StartNew();

            for (int i = 0; i < Iterations; i++)
            {
                using (var stringReader = new StringReader(_xmlWithAbstract))
                {
                    using (var reader = new XmlTextReader(stringReader))
                    {
                        xmlSerializer.Deserialize(reader);
                    }
                }
            }

            xmlSerializerStopwatch.Stop();

            var options = new TestSerializeOptions();

            var customSerializerStopwatch = Stopwatch.StartNew();

            for (int i = 0; i < Iterations; i++)
            {
                using (var stringReader = new StringReader(_xmlWithInterface))
                {
                    using (var xmlReader = new XmlTextReader(stringReader))
                    {
                        using (var reader = new XSerializerXmlReader(xmlReader, options.GetEncryptionMechanism(), options.EncryptKey, options.SerializationState))
                        {
                            customSerializer.DeserializeObject(reader, options);
                        }
                    }
                }
            }

            customSerializerStopwatch.Stop();

            Console.WriteLine("XmlSerializer Elapsed Time: {0}", xmlSerializerStopwatch.Elapsed);
            Console.WriteLine("CustomSerializer Elapsed Time: {0}", customSerializerStopwatch.Elapsed);
        }
Beispiel #3
0
        public void CanEncryptAndDecryptIndividualElementValues()
        {
            var sb = new StringBuilder();
            IEncryptionMechanism encryptionMechanism = new Base64EncryptionMechanism();
            var options = new TestSerializeOptions {
                EncryptionMechanism = encryptionMechanism, SerializationState = new SerializationState()
            };

            using (var stringWriter = new StringWriter(sb))
            {
                using (var writer = new XSerializerXmlTextWriter(stringWriter, options))
                {
                    writer.WriteStartElement("foo");

                    writer.WriteStartElement("bar");

                    writer.WriteStartAttribute("baz");
                    writer.WriteValue("123");
                    writer.WriteEndAttribute();

                    writer.WriteStartElement("qux");
                    writer.IsEncryptionEnabled = true;
                    writer.WriteValue("abc");
                    writer.IsEncryptionEnabled = false;
                    writer.WriteEndElement();

                    writer.WriteEndElement(); // </bar>

                    writer.WriteStartElement("rab");

                    writer.WriteStartAttribute("zab");
                    writer.WriteValue("789");
                    writer.WriteEndAttribute();

                    writer.WriteStartElement("xuq");
                    writer.IsEncryptionEnabled = true;
                    writer.WriteValue("xyz");
                    writer.IsEncryptionEnabled = false;
                    writer.WriteEndElement();

                    writer.WriteEndElement(); // </rab>

                    writer.WriteEndElement(); // </foo>
                }
            }

            Func <string, string> e = x => encryptionMechanism.Encrypt(x, null, options.SerializationState);

            // reference xml:
            // <foo><bar baz="123"><qux>abc</qux></bar><rab zab="789"><xuq>xyz</xuq></rab></foo>

            var xml = sb.ToString();

            var expectedXml =
                "<foo>" +
                @"<bar baz=""123"">"
                + "<qux>" + e("abc") + "</qux>"
                + "</bar>" +
                @"<rab zab=""789"">"
                + "<xuq>" + e("xyz") + "</xuq>"
                + "</rab>"
                + "</foo>";

            Assert.That(xml, Is.EqualTo(expectedXml));

            using (var stringReader = new StringReader(xml))
            {
                using (var xmlReader = new XmlTextReader(stringReader))
                {
                    using (var reader = new XSerializerXmlReader(xmlReader, encryptionMechanism, options.EncryptKey, options.SerializationState))
                    {
                        reader.Read(); // None -> <foo>

                        reader.Read(); // <foo> -> <bar>

                        Assert.That(reader.GetAttribute("baz"), Is.EqualTo("123"));

                        reader.Read(); // <bar> -> <qux>

                        reader.IsDecryptionEnabled = true;
                        Assert.That(reader.ReadString(), Is.EqualTo("abc")); // <qux> -> "abc" -> </qux>
                        reader.IsDecryptionEnabled = false;

                        reader.Read(); // </qux> -> </bar>
                        reader.Read(); // </bar> -> <rab>

                        Assert.That(reader.GetAttribute("zab"), Is.EqualTo("789"));

                        reader.Read(); // <rab> -> <xuq>

                        reader.IsDecryptionEnabled = true;
                        Assert.That(reader.ReadString(), Is.EqualTo("xyz")); // <xuq> -> "xyz" -> </xuq>
                        reader.IsDecryptionEnabled = false;

                        reader.Read(); // </xuq> -> </rab>

                        reader.Read(); // </rab> -> </foo>

                        reader.Read(); // </foo> -> None

                        Assert.That(reader.NodeType, Is.EqualTo(XmlNodeType.None));
                    }
                }
            }
        }
        public void Benchmark()
        {
            new System.Xml.Serialization.XmlSerializer(typeof(JitPreparation), null, null, null, null);
            CustomSerializer.GetSerializer(typeof(JitPreparation), null, TestXmlSerializerOptions.Empty);

            var containerWithAbstract =
                new ColdStartContainerWithAbstract
            {
                Id  = "a",
                One =
                    new ColdStartOneWithAbstract
                {
                    Id  = "b",
                    Two = new ColdStartTwoWithAbstract {
                        Id = "c", Value = "abc"
                    }
                }
            };
            var containerWithInterface =
                new ColdStartContainerWithInterface
            {
                Id  = "a",
                One =
                    new ColdStartOneWithInterface
                {
                    Id  = "b",
                    Two = new ColdStartTwoWithInterface {
                        Id = "c", Value = "abc"
                    }
                }
            };

            var xmlSerializerStopwatch = Stopwatch.StartNew();

            var xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(ColdStartContainerWithAbstract), null, null, null, null);
            var xmlSerializerStringBuilder = new StringBuilder();

            using (var stringWriter = new StringWriter(xmlSerializerStringBuilder))
            {
                using (var writer = new XmlTextWriter(stringWriter))
                {
                    xmlSerializer.Serialize(writer, containerWithAbstract, null);
                }
            }

            using (var stringReader = new StringReader(xmlSerializerStringBuilder.ToString()))
            {
                using (var reader = new XmlTextReader(stringReader))
                {
                    xmlSerializer.Deserialize(reader);
                }
            }

            xmlSerializerStopwatch.Stop();

            ISerializeOptions options = new TestSerializeOptions();

            var customSerializerStopwatch = Stopwatch.StartNew();

            var customSerializer = CustomSerializer.GetSerializer(typeof(ColdStartContainerWithInterface), null, TestXmlSerializerOptions.Empty);
            var customSerializerStringBuilder = new StringBuilder();

            using (var stringWriter = new StringWriter(customSerializerStringBuilder))
            {
                using (var writer = new XSerializerXmlTextWriter(stringWriter, options))
                {
                    customSerializer.SerializeObject(writer, containerWithInterface, new TestSerializeOptions(true));
                }
            }

            using (var stringReader = new StringReader(customSerializerStringBuilder.ToString()))
            {
                using (var xmlReader = new XmlTextReader(stringReader))
                {
                    using (var reader = new XSerializerXmlReader(xmlReader, options.GetEncryptionMechanism(), options.EncryptKey, options.SerializationState))
                    {
                        customSerializer.DeserializeObject(reader, options);
                    }
                }
            }

            customSerializerStopwatch.Stop();

            Console.WriteLine("XmlSerializer Elapsed Time: {0}", xmlSerializerStopwatch.Elapsed);
            Console.WriteLine("CustomSerializer Elapsed Time: {0}", customSerializerStopwatch.Elapsed);
        }
        public void CanEncryptAndDecryptIndividualElementValues()
        {
            var sb = new StringBuilder();
            IEncryptionMechanism encryptionMechanism = new Base64EncryptionMechanism();
            var options = new TestSerializeOptions { EncryptionMechanism = encryptionMechanism, SerializationState = new SerializationState() };

            using (var stringWriter = new StringWriter(sb))
            {
                using (var writer = new XSerializerXmlTextWriter(stringWriter, options))
                {
                    writer.WriteStartElement("foo");

                    writer.WriteStartElement("bar");

                    writer.WriteStartAttribute("baz");
                    writer.WriteValue("123");
                    writer.WriteEndAttribute();

                    writer.WriteStartElement("qux");
                    writer.IsEncryptionEnabled = true;
                    writer.WriteValue("abc");
                    writer.IsEncryptionEnabled = false;
                    writer.WriteEndElement();

                    writer.WriteEndElement(); // </bar>

                    writer.WriteStartElement("rab");

                    writer.WriteStartAttribute("zab");
                    writer.WriteValue("789");
                    writer.WriteEndAttribute();

                    writer.WriteStartElement("xuq");
                    writer.IsEncryptionEnabled = true;
                    writer.WriteValue("xyz");
                    writer.IsEncryptionEnabled = false;
                    writer.WriteEndElement();

                    writer.WriteEndElement(); // </rab>

                    writer.WriteEndElement(); // </foo>
                }
            }

            Func<string, string> e = x => encryptionMechanism.Encrypt(x, null, options.SerializationState);

            // reference xml:
            // <foo><bar baz="123"><qux>abc</qux></bar><rab zab="789"><xuq>xyz</xuq></rab></foo>

            var xml = sb.ToString();

            var expectedXml =
                "<foo>" +
                @"<bar baz=""123"">"
                + "<qux>" + e("abc") + "</qux>"
                + "</bar>" +
                @"<rab zab=""789"">"
                + "<xuq>" + e("xyz") + "</xuq>"
                + "</rab>"
                + "</foo>";

            Assert.That(xml, Is.EqualTo(expectedXml));

            using (var stringReader = new StringReader(xml))
            {
                using (var xmlReader = new XmlTextReader(stringReader))
                {
                    using (var reader = new XSerializerXmlReader(xmlReader, encryptionMechanism, options.EncryptKey, options.SerializationState))
                    {
                        reader.Read(); // None -> <foo>

                        reader.Read(); // <foo> -> <bar>

                        Assert.That(reader.GetAttribute("baz"), Is.EqualTo("123"));

                        reader.Read(); // <bar> -> <qux>

                        reader.IsDecryptionEnabled = true;
                        Assert.That(reader.ReadString(), Is.EqualTo("abc")); // <qux> -> "abc" -> </qux>
                        reader.IsDecryptionEnabled = false;

                        reader.Read(); // </qux> -> </bar>
                        reader.Read(); // </bar> -> <rab>

                        Assert.That(reader.GetAttribute("zab"), Is.EqualTo("789"));

                        reader.Read(); // <rab> -> <xuq>

                        reader.IsDecryptionEnabled = true;
                        Assert.That(reader.ReadString(), Is.EqualTo("xyz")); // <xuq> -> "xyz" -> </xuq>
                        reader.IsDecryptionEnabled = false;

                        reader.Read(); // </xuq> -> </rab>

                        reader.Read(); // </rab> -> </foo>

                        reader.Read(); // </foo> -> None

                        Assert.That(reader.NodeType, Is.EqualTo(XmlNodeType.None));
                    }
                }
            }
        }
        public void Benchmark()
        {
            new System.Xml.Serialization.XmlSerializer(typeof(JitPreparation), null, null, null, null);
            CustomSerializer.GetSerializer(typeof(JitPreparation), null, TestXmlSerializerOptions.Empty);

            var containerWithAbstract =
                new ColdStartContainerWithAbstract
                {
                    Id = "a",
                    One =
                        new ColdStartOneWithAbstract
                        {
                            Id = "b",
                            Two = new ColdStartTwoWithAbstract { Id = "c", Value = "abc" }
                        }
                };
            var containerWithInterface =
                new ColdStartContainerWithInterface
                {
                    Id = "a",
                    One =
                        new ColdStartOneWithInterface
                        {
                            Id = "b",
                            Two = new ColdStartTwoWithInterface { Id = "c", Value = "abc" }
                        }
                };

            var xmlSerializerStopwatch = Stopwatch.StartNew();

            var xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(ColdStartContainerWithAbstract), null, null, null, null);
            var xmlSerializerStringBuilder = new StringBuilder();

            using (var stringWriter = new StringWriter(xmlSerializerStringBuilder))
            {
                using (var writer = new XmlTextWriter(stringWriter))
                {
                    xmlSerializer.Serialize(writer, containerWithAbstract, null);
                }
            }

            using (var stringReader = new StringReader(xmlSerializerStringBuilder.ToString()))
            {
                using (var reader = new XmlTextReader(stringReader))
                {
                    xmlSerializer.Deserialize(reader);
                }
            }

            xmlSerializerStopwatch.Stop();

            ISerializeOptions options = new TestSerializeOptions();

            var customSerializerStopwatch = Stopwatch.StartNew();

            var customSerializer = CustomSerializer.GetSerializer(typeof(ColdStartContainerWithInterface), null, TestXmlSerializerOptions.Empty);
            var customSerializerStringBuilder = new StringBuilder();

            using (var stringWriter = new StringWriter(customSerializerStringBuilder))
            {
                using (var writer = new XSerializerXmlTextWriter(stringWriter, options))
                {
                    customSerializer.SerializeObject(writer, containerWithInterface, new TestSerializeOptions(true));
                }
            }

            using (var stringReader = new StringReader(customSerializerStringBuilder.ToString()))
            {
                using (var xmlReader = new XmlTextReader(stringReader))
                {
                    using (var reader = new XSerializerXmlReader(xmlReader, options.GetEncryptionMechanism(), options.EncryptKey, options.SerializationState))
                    {
                        customSerializer.DeserializeObject(reader, options);
                    }
                }
            }

            customSerializerStopwatch.Stop();

            Console.WriteLine("XmlSerializer Elapsed Time: {0}", xmlSerializerStopwatch.Elapsed);
            Console.WriteLine("CustomSerializer Elapsed Time: {0}", customSerializerStopwatch.Elapsed);
        }