<commit_before>﻿using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using DAL;
using json.Json;
using json.Objects;
using json.PerformanceTests.Data;
using json.Tests;
using NUnit.Framework;
using ServiceStack.Text;
using XmlSerializer = System.Xml.Serialization.XmlSerializer;

namespace json.PerformanceTests
{
    [TestFixture]
    public class PerformanceTest
    {
        private const int Iterations = 100;

        [Test]
        public void VerifyNorthwindDeserializeReserialize()
        {
            string originalJson = GetNorthwindJson();
            ObjectWriter<Database> writer = new ObjectWriter<Database>();
            JsonParser.Parse(originalJson, writer);

            Database db = writer.Result;

            db.ShouldNotBeNull();

            StringWriter stringWriter = new StringWriter();
            PrettyPrintingJsonStringWriter jsonWriter = new PrettyPrintingJsonStringWriter(stringWriter);
            ObjectReader.Read(db, jsonWriter, new ObjectParsingOptions { SerializeTypeInformation = TypeInformationLevel.None });

            string result = stringWriter.ToString();

            result.ShouldBe(originalJson);
        }

        [Test]
        public void SerializeToNull()
        {
            DatabaseCompat db = GetNorthwindObject();
            ObjectReader.Read(db, new JsonStringWriter(NullTextWriter.Instance));

            Time(Iterations, () => ObjectReader.Read(db, new JsonStringWriter(TextWriter.Null)));
        }

        [Test]
        public void XmlSerialize()
        {
            DatabaseCompat db = GetNorthwindObject();

            XmlSerializer serializer = new XmlSerializer(typeof(DatabaseCompat));
            serializer.Serialize(Stream.Null, db);

            Time(Iterations, () => serializer.Serialize(Stream.Null, db));
        }

        [Test]
        public void ServiceStackSerialize()
        {
            DatabaseCompat db = GetNorthwindObject();
            JsonSerializer<DatabaseCompat> serializer = new JsonSerializer<DatabaseCompat>();
            serializer.SerializeToWriter(db, TextWriter.Null);

            Time(Iterations, () => serializer.SerializeToWriter(db, TextWriter.Null));
        }

        [Test]
        public void Deserialize()
        {
            GetNorthwindObject();
            string json = GetNorthwindJson();

            Time(10, () => JsonParser.Parse(json, new ObjectWriter<DatabaseCompat>()));
        }

        private static void Time(int iterations, Action action)
        {
            Stopwatch watch = Stopwatch.StartNew();
            for (int i = 0; i < iterations; i++)
                action();
            watch.Stop();
            Console.WriteLine(watch.ElapsedMilliseconds);
        }

        private DatabaseCompat GetNorthwindObject()
        {
            string json = GetNorthwindJson();
            ObjectWriter<DatabaseCompat> objectWriter = new ObjectWriter<DatabaseCompat>();
            JsonParser.Parse(json, objectWriter);
            return objectWriter.Result;
        }

        private string GetNorthwindJson()
        {
            Stream jsonStream = GetType().Assembly.GetManifestResourceStream("json.PerformanceTests.Data.northwind.json");
            byte[] buffer = new byte[jsonStream.Length];
            jsonStream.Read(buffer, 0, (int)jsonStream.Length);
            return Encoding.UTF8.GetString(buffer);
        }
    }

    internal class NullTextWriter : TextWriter
    {
        public static readonly NullTextWriter Instance = new NullTextWriter();

        private NullTextWriter() { }

        public override void Write(char value) { }

        public override void Write(double value) { }

        public override void Write(int value) { }

        public override void Write(string value) { }

        public override void WriteLine() { }

        public override Encoding Encoding { get { return Encoding.Default; } }
    }
}<commit_msg>Adding deserialization perf tests for ServiceStack.Text & XmlSerializer.<commit_after>﻿using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using DAL;
using json.Json;
using json.Objects;
using json.PerformanceTests.Data;
using json.Tests;
using NUnit.Framework;
using ServiceStack.Text;
using XmlSerializer = System.Xml.Serialization.XmlSerializer;

namespace json.PerformanceTests
{
    [TestFixture]
    public class PerformanceTest
    {
        private const int Iterations = 100;

        [Test]
        public void VerifyNorthwindDeserializeReserialize()
        {
            string originalJson = GetNorthwindJson();
            ObjectWriter<Database> writer = new ObjectWriter<Database>();
            JsonParser.Parse(originalJson, writer);

            Database db = writer.Result;

            db.ShouldNotBeNull();

            StringWriter stringWriter = new StringWriter();
            PrettyPrintingJsonStringWriter jsonWriter = new PrettyPrintingJsonStringWriter(stringWriter);
            ObjectReader.Read(db, jsonWriter, new ObjectParsingOptions { SerializeTypeInformation = TypeInformationLevel.None });

            string result = stringWriter.ToString();

            result.ShouldBe(originalJson);
        }

        [Test]
        public void SerializeToNull()
        {
            DatabaseCompat db = GetNorthwindObject();
            ObjectReader.Read(db, new JsonStringWriter(NullTextWriter.Instance));

            Time(Iterations, () => ObjectReader.Read(db, new JsonStringWriter(TextWriter.Null)));
        }

        [Test]
        public void XmlSerialize()
        {
            DatabaseCompat db = GetNorthwindObject();

            XmlSerializer serializer = new XmlSerializer(typeof(DatabaseCompat));
            serializer.Serialize(Stream.Null, db);

            Time(Iterations, () => serializer.Serialize(Stream.Null, db));
        }

        [Test]
        public void ServiceStackSerialize()
        {
            DatabaseCompat db = GetNorthwindObject();
            JsonSerializer<DatabaseCompat> serializer = new JsonSerializer<DatabaseCompat>();
            serializer.SerializeToWriter(db, TextWriter.Null);

            Time(Iterations, () => serializer.SerializeToWriter(db, TextWriter.Null));
        }

        [Test]
        public void Deserialize()
        {
            GetNorthwindObject();
            string json = GetNorthwindJson();

            Time(100, () => JsonParser.Parse(json, new ObjectWriter<DatabaseCompat>()));
        }

        [Test]
        public void ServiceStackDeserialize()
        {
            string json = GetNorthwindJson();
            json.FromJson<DatabaseCompat>();

            Time(100, () => json.FromJson<DatabaseCompat>());
        }

        [Test]
        public void XmlDeserialize()
        {
            DatabaseCompat db = GetNorthwindObject();
            XmlSerializer serializer = new XmlSerializer(typeof(DatabaseCompat));
            StringWriter stringWriter = new StringWriter();
            serializer.Serialize(stringWriter, db);

            string xml = stringWriter.ToString();

            Time(100, () => new XmlSerializer(typeof(DatabaseCompat)).Deserialize(new StringReader(xml)));
        }

        private static void Time(int iterations, Action action)
        {
            Stopwatch watch = Stopwatch.StartNew();
            for (int i = 0; i < iterations; i++)
                action();
            watch.Stop();
            Console.WriteLine(watch.ElapsedMilliseconds);
        }

        private DatabaseCompat GetNorthwindObject()
        {
            string json = GetNorthwindJson();
            ObjectWriter<DatabaseCompat> objectWriter = new ObjectWriter<DatabaseCompat>();
            JsonParser.Parse(json, objectWriter);
            return objectWriter.Result;
        }

        private string GetNorthwindJson()
        {
            Stream jsonStream = GetType().Assembly.GetManifestResourceStream("json.PerformanceTests.Data.northwind.json");
            byte[] buffer = new byte[jsonStream.Length];
            jsonStream.Read(buffer, 0, (int)jsonStream.Length);
            return Encoding.UTF8.GetString(buffer);
        }
    }

    internal class NullTextWriter : TextWriter
    {
        public static readonly NullTextWriter Instance = new NullTextWriter();

        private NullTextWriter() { }

        public override void Write(char value) { }

        public override void Write(double value) { }

        public override void Write(int value) { }

        public override void Write(string value) { }

        public override void WriteLine() { }

        public override Encoding Encoding { get { return Encoding.Default; } }
    }

    public class NullWriter : Writer
    {
        public bool CanWrite(object value)
        {
            return true;
        }

        public void Write(object value)
        {
        }

        public void BeginStructure(Type readerType)
        {
        }

        public void BeginStructure(string typeIdentifier, Type readerType)
        {
        }

        public void EndStructure()
        {
        }

        public void AddProperty(string name)
        {
        }

        public void BeginSequence()
        {
        }

        public void EndSequence()
        {
        }

        public void WriteReference(int referenceIndex)
        {
        }

        public void Write(bool value)
        {
        }

        public void Write(char value)
        {
        }

        public void Write(decimal value)
        {
        }

        public void Write(double value)
        {
        }

        public void Write(float value)
        {
        }

        public void Write(int value)
        {
        }

        public void Write(long value)
        {
        }

        public void Write(string value)
        {
        }

        public void WriteString(string value)
        {
        }

        public void Write(uint value)
        {
        }

        public void Write(ulong value)
        {
        }

        public void WriteNull()
        {
        }
    }
}
{'fraction_non_alphanumeric': 0.08819430619151901, 'fraction_numerical': 0.0022894684451522994, 'mean_word_length': 2.1153488372093023, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2213377', 'n_tokens_mistral': 2522, 'n_tokens_neox': 2400, 'n_words': 572}