Пример #1
0
        public static void MeasurePerf(string json, string filename, int numberOfIterations = 1)
        {
            byte[] utf8ByteArray = Encoding.UTF8.GetBytes(json);

            // Text
            TimeSpan           textReaderTime     = JsonPerfMeasurement.MeasureReadPerformance(JsonReader.Create(utf8ByteArray), numberOfIterations);
            TimeSpan           textWriterTime     = JsonPerfMeasurement.MeasureWritePerformance(JsonWriter.Create(JsonSerializationFormat.Text), json, numberOfIterations);
            TimeSpan           textNavigatorTime  = JsonPerfMeasurement.MeasureNavigationPerformance(JsonNavigator.Create(utf8ByteArray), numberOfIterations);
            JsonExecutionTimes textExecutionTimes = new JsonExecutionTimes(textReaderTime, textWriterTime, textNavigatorTime, utf8ByteArray.Length, "Text");

            // Newtonsoft
            TimeSpan           newtonsoftReaderTime     = JsonPerfMeasurement.MeasureReadPerformance(new JsonNewtonsoftNewtonsoftTextReader(json), numberOfIterations);
            TimeSpan           newtonsoftWriterTime     = JsonPerfMeasurement.MeasureWritePerformance(new JsonNewtonsoftNewtonsoftTextWriter(), json, numberOfIterations);
            TimeSpan           newtonsoftNavigatorTime  = JsonPerfMeasurement.MeasureNavigationPerformance(new JsonNewtonsoftNavigator(json), numberOfIterations);
            JsonExecutionTimes newtonsoftExecutionTimes = new JsonExecutionTimes(newtonsoftReaderTime, newtonsoftWriterTime, newtonsoftNavigatorTime, json.Length, "Newtonsoft");

            // Binary
            byte[]             binaryPayload        = JsonTestUtils.ConvertTextToBinary(json);
            TimeSpan           binaryReaderTime     = JsonPerfMeasurement.MeasureReadPerformance(JsonReader.Create(binaryPayload), numberOfIterations);
            TimeSpan           binarytWriterTime    = JsonPerfMeasurement.MeasureWritePerformance(JsonWriter.Create(JsonSerializationFormat.Binary), json, numberOfIterations);
            TimeSpan           binaryNavigatorTime  = JsonPerfMeasurement.MeasureNavigationPerformance(JsonNavigator.Create(binaryPayload), numberOfIterations);
            JsonExecutionTimes binaryExecutionTimes = new JsonExecutionTimes(binaryReaderTime, binarytWriterTime, binaryNavigatorTime, binaryPayload.Length, "Binary");

            JsonPerfMeasurement.PrintStatisticsTable(filename, textExecutionTimes, newtonsoftExecutionTimes, binaryExecutionTimes);
        }
        // Checks to see if we can go from a JsonReader to a NewtonsoftWriter and get back the original document and visa versa
        private void RoundTripTestCuratedJson(string filename)
        {
            string path = string.Format("TestJsons/{0}", filename);
            string json = File.ReadAllText(path);

#if true
            json = JsonTestUtils.RandomSampleJson(json);
#endif
            this.MultiSerializationRoundTrip(json);
        }
        private void VerifyNavigatorWithCurratedDoc(string filename, bool performExtraChecks = true)
        {
            string path = string.Format("TestJsons/{0}", filename);
            string json = File.ReadAllText(path);

#if true
            json = JsonTestUtils.RandomSampleJson(json);
#endif

            this.VerifyNavigator(json, performExtraChecks);
        }
Пример #4
0
        private void VerifyNavigator(string input, Exception expectedException, bool performExtraChecks = true)
        {
            CultureInfo defaultCultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture;

            CultureInfo[] cultureInfoList = new CultureInfo[]
            {
                defaultCultureInfo,
                System.Globalization.CultureInfo.GetCultureInfo("fr-FR")
            };

            try
            {
                foreach (CultureInfo cultureInfo in cultureInfoList)
                {
                    System.Threading.Thread.CurrentThread.CurrentCulture = cultureInfo;

                    IJsonReader     jsonReader       = JsonReader.Create(Encoding.UTF8.GetBytes(input));
                    JsonTokenInfo[] tokensFromReader = JsonNavigatorTests.GetTokensWithReader(jsonReader);

                    // Test text
                    IJsonNavigator     textNavigator           = JsonNavigator.Create(Encoding.UTF8.GetBytes(input));
                    IJsonNavigatorNode textRootNode            = textNavigator.GetRootNode();
                    JsonTokenInfo[]    tokensFromTextNavigator = JsonNavigatorTests.GetTokensFromNode(textRootNode, textNavigator, performExtraChecks);

                    Assert.IsTrue(tokensFromTextNavigator.SequenceEqual(tokensFromReader));

                    // Test binary
                    byte[]             binaryInput               = JsonTestUtils.ConvertTextToBinary(input);
                    IJsonNavigator     binaryNavigator           = JsonNavigator.Create(binaryInput);
                    IJsonNavigatorNode binaryRootNode            = binaryNavigator.GetRootNode();
                    JsonTokenInfo[]    tokensFromBinaryNavigator = JsonNavigatorTests.GetTokensFromNode(binaryRootNode, binaryNavigator, performExtraChecks);

                    Assert.IsTrue(tokensFromBinaryNavigator.SequenceEqual(tokensFromReader));

                    // Test binary + user string encoding
                    JsonStringDictionary jsonStringDictionary = new JsonStringDictionary(capacity: 4096);
                    byte[] binaryWithUserStringEncodingInput  = JsonTestUtils.ConvertTextToBinary(input, jsonStringDictionary);
                    if (jsonStringDictionary.TryGetStringAtIndex(index: 0, value: out string temp))
                    {
                        Assert.IsFalse(binaryWithUserStringEncodingInput.SequenceEqual(binaryInput), "Binary should be different with user string encoding");
                    }
                    IJsonNavigator     binaryNavigatorWithUserStringEncoding           = JsonNavigator.Create(binaryInput, jsonStringDictionary);
                    IJsonNavigatorNode binaryRootNodeWithUserStringEncoding            = binaryNavigatorWithUserStringEncoding.GetRootNode();
                    JsonTokenInfo[]    tokensFromBinaryNavigatorWithUserStringEncoding = JsonNavigatorTests.GetTokensFromNode(binaryRootNode, binaryNavigator, performExtraChecks);

                    Assert.IsTrue(tokensFromBinaryNavigatorWithUserStringEncoding.SequenceEqual(tokensFromReader));
                }
            }
            finally
            {
                System.Threading.Thread.CurrentThread.CurrentCulture = defaultCultureInfo;
            }
        }
        private void VerifyNavigator(string input, Exception expectedException, bool performExtraChecks = true)
        {
            IJsonReader jsonReader = JsonReader.Create(Encoding.UTF8.GetBytes(input));

            JsonTokenInfo[] tokensFromReader = JsonNavigatorTests.GetTokensWithReader(jsonReader);

            // Test text
            IJsonNavigator     textNavigator = JsonNavigator.Create(Encoding.UTF8.GetBytes(input));
            IJsonNavigatorNode textRootNode  = textNavigator.GetRootNode();

            JsonTokenInfo[] tokensFromTextNavigator = JsonNavigatorTests.GetTokensFromNode(textRootNode, textNavigator, performExtraChecks);

            Assert.IsTrue(tokensFromTextNavigator.SequenceEqual(tokensFromReader));

            // Test binary
            byte[]             binaryInput     = JsonTestUtils.ConvertTextToBinary(input);
            IJsonNavigator     binaryNavigator = JsonNavigator.Create(binaryInput);
            IJsonNavigatorNode binaryRootNode  = binaryNavigator.GetRootNode();

            JsonTokenInfo[] tokensFromBinaryNavigator = JsonNavigatorTests.GetTokensFromNode(binaryRootNode, binaryNavigator, performExtraChecks);

            Assert.IsTrue(tokensFromBinaryNavigator.SequenceEqual(tokensFromReader));

            // Test binary + user string encoding
            JsonStringDictionary jsonStringDictionary = new JsonStringDictionary(capacity: 4096);

            byte[] binaryWithUserStringEncodingInput = JsonTestUtils.ConvertTextToBinary(input, jsonStringDictionary);
            if (jsonStringDictionary.TryGetStringAtIndex(index: 0, value: out string temp))
            {
                Assert.IsFalse(binaryWithUserStringEncodingInput.SequenceEqual(binaryInput), "Binary should be different with user string encoding");
            }
            IJsonNavigator     binaryNavigatorWithUserStringEncoding = JsonNavigator.Create(binaryInput, jsonStringDictionary);
            IJsonNavigatorNode binaryRootNodeWithUserStringEncoding  = binaryNavigatorWithUserStringEncoding.GetRootNode();

            JsonTokenInfo[] tokensFromBinaryNavigatorWithUserStringEncoding = JsonNavigatorTests.GetTokensFromNode(binaryRootNode, binaryNavigator, performExtraChecks);

            Assert.IsTrue(tokensFromBinaryNavigatorWithUserStringEncoding.SequenceEqual(tokensFromReader));
        }
Пример #6
0
        private static void VerifyWriter <T>(JsonSerializationFormat jsonSerializationFormat, T expectedDeserializedValue)
        {
            using (CosmosDBToNewtonsoftWriter writer = new CosmosDBToNewtonsoftWriter(jsonSerializationFormat))
            {
                JsonSerializer serializer = new JsonSerializer();
                serializer.Serialize(writer, expectedDeserializedValue);

                byte[] result = writer.GetResult();
                string actualSerializedValue;
                if (jsonSerializationFormat == JsonSerializationFormat.Binary)
                {
                    actualSerializedValue = JsonTestUtils.ConvertBinaryToText(result);
                }
                else
                {
                    actualSerializedValue = Encoding.UTF8.GetString(result);
                }

                actualSerializedValue = NewtonsoftInteropTests.NewtonsoftFormat(actualSerializedValue);
                string expectedSerializedValue = NewtonsoftInteropTests.NewtonsoftFormat(JsonConvert.SerializeObject(expectedDeserializedValue));
                Assert.AreEqual(expectedSerializedValue, actualSerializedValue);
            }
        }
        private void VerifyNavigator(string input, Exception expectedException, bool performExtraChecks = true)
        {
            IJsonReader jsonReader = JsonReader.Create(Encoding.UTF8.GetBytes(input));

            JsonTokenInfo[] tokensFromReader = JsonNavigatorTests.GetTokensWithReader(jsonReader);

            // Test text
            IJsonNavigator     textNavigator = JsonNavigator.Create(Encoding.UTF8.GetBytes(input));
            IJsonNavigatorNode textRootNode  = textNavigator.GetRootNode();

            JsonTokenInfo[] tokensFromTextNavigator = JsonNavigatorTests.GetTokensFromNode(textRootNode, textNavigator, performExtraChecks);

            Assert.IsTrue(tokensFromTextNavigator.SequenceEqual(tokensFromReader));

            // Test binary
            byte[]             binaryInput     = JsonTestUtils.ConvertTextToBinary(input);
            IJsonNavigator     binaryNavigator = JsonNavigator.Create(binaryInput);
            IJsonNavigatorNode binaryRootNode  = binaryNavigator.GetRootNode();

            JsonTokenInfo[] tokensFromBinaryNavigator = JsonNavigatorTests.GetTokensFromNode(binaryRootNode, binaryNavigator, performExtraChecks);

            Assert.IsTrue(tokensFromBinaryNavigator.SequenceEqual(tokensFromReader));
        }
        private void NewtonsoftWrapperRoundTrip(string json)
        {
            // Normalize the json to get rid of any formatting issues
            json = this.NewtonsoftFormat(json);

            foreach (NewtonsoftWrapperFormat sourceFormat in Enum.GetValues(typeof(NewtonsoftWrapperFormat)))
            {
                foreach (NewtonsoftWrapperFormat destinationFormat in Enum.GetValues(typeof(NewtonsoftWrapperFormat)))
                {
                    IJsonReader reader;
                    switch (sourceFormat)
                    {
                    case NewtonsoftWrapperFormat.NewtonsoftText:
                        reader = new JsonNewtonsoftNewtonsoftTextReader(json);
                        break;

                    case NewtonsoftWrapperFormat.CosmosDBText:
                        reader = new JsonNewtonsoftCosmosDBTextReader(json);
                        break;

                    case NewtonsoftWrapperFormat.CosmosDBBinary:
                        reader = new JsonNewtonsoftCosmosDBBinaryReader(json);
                        break;

                    default:
                        throw new ArgumentException($"Unexpected {nameof(sourceFormat)} of type: {sourceFormat}");
                    }

                    IJsonWriter writer;
                    switch (destinationFormat)
                    {
                    case NewtonsoftWrapperFormat.NewtonsoftText:
                        writer = new JsonNewtonsoftNewtonsoftTextWriter();
                        break;

                    case NewtonsoftWrapperFormat.CosmosDBText:
                        writer = new JsonNewtonsoftCosmosDBTextWriter();
                        break;

                    case NewtonsoftWrapperFormat.CosmosDBBinary:
                        writer = new JsonNewtonsoftCosmosDBBinaryWriter();
                        break;

                    default:
                        throw new ArgumentException($"Unexpected {nameof(sourceFormat)} of type: {sourceFormat}");
                    }

                    Stopwatch stopwatch = Stopwatch.StartNew();
                    writer.WriteAll(reader);
                    stopwatch.Stop();

                    string result;
                    switch (writer.SerializationFormat)
                    {
                    case JsonSerializationFormat.Text:
                        result = Encoding.UTF8.GetString(writer.GetResult());
                        break;

                    case JsonSerializationFormat.Binary:
                        result = JsonTestUtils.ConvertBinaryToText(writer.GetResult());
                        break;

                    default:
                        throw new ArgumentException();
                    }

                    result = this.NewtonsoftFormat(result);
                    Assert.AreEqual(json, result);

                    Console.WriteLine($"{sourceFormat} Reader to {destinationFormat} Writer took {stopwatch.ElapsedMilliseconds}ms");
                }
            }
        }
        private void MultiSerializationRoundTrip(string json)
        {
            // Normalize the json to get rid of any formatting issues
            json = this.NewtonsoftFormat(json);

            foreach (SerializationFormat sourceFormat in Enum.GetValues(typeof(SerializationFormat)))
            {
                foreach (SerializationFormat destinationFormat in Enum.GetValues(typeof(SerializationFormat)))
                {
                    IJsonReader reader;
                    switch (sourceFormat)
                    {
                    case SerializationFormat.Text:
                        reader = JsonReader.Create(Encoding.UTF8.GetBytes(json));
                        break;

                    case SerializationFormat.Binary:
                        reader = JsonReader.Create(JsonTestUtils.ConvertTextToBinary(json));
                        break;

                    case SerializationFormat.NewtonsoftText:
                        reader = new JsonNewtonsoftNewtonsoftTextReader(json);
                        break;

                    default:
                        throw new ArgumentException($"Unexpected {nameof(sourceFormat)} of type: {sourceFormat}");
                    }

                    IJsonNavigator navigator;
                    switch (sourceFormat)
                    {
                    case SerializationFormat.Text:
                        navigator = JsonNavigator.Create(Encoding.UTF8.GetBytes(json));
                        break;

                    case SerializationFormat.Binary:
                        navigator = JsonNavigator.Create(JsonTestUtils.ConvertTextToBinary(json));
                        break;

                    case SerializationFormat.NewtonsoftText:
                        navigator = new JsonNewtonsoftNavigator(json);
                        break;

                    default:
                        throw new ArgumentException($"Unexpected {nameof(sourceFormat)} of type: {sourceFormat}");
                    }

                    object[] sources = new object[] { reader, navigator };
                    foreach (object source in sources)
                    {
                        IJsonWriter writer;
                        switch (destinationFormat)
                        {
                        case SerializationFormat.Text:
                            writer = JsonWriter.Create(JsonSerializationFormat.Text);
                            break;

                        case SerializationFormat.Binary:
                            writer = JsonWriter.Create(JsonSerializationFormat.Binary);
                            break;

                        case SerializationFormat.NewtonsoftText:
                            writer = new JsonNewtonsoftNewtonsoftTextWriter();
                            break;

                        default:
                            throw new ArgumentException($"Unexpected {nameof(destinationFormat)} of type: {destinationFormat}");
                        }

                        Stopwatch stopwatch = Stopwatch.StartNew();
                        if (source is IJsonReader)
                        {
                            IJsonReader sourceReader = source as IJsonReader;
                            writer.WriteAll(sourceReader);
                        }
                        else if (source is IJsonNavigator)
                        {
                            IJsonNavigator sourceNavigator = source as IJsonNavigator;
                            writer.WriteJsonNode(sourceNavigator, sourceNavigator.GetRootNode());
                        }
                        stopwatch.Stop();

                        string result;
                        switch (writer.SerializationFormat)
                        {
                        case JsonSerializationFormat.Text:
                            result = Encoding.UTF8.GetString(writer.GetResult());
                            break;

                        case JsonSerializationFormat.Binary:
                            result = JsonTestUtils.ConvertBinaryToText(writer.GetResult());
                            break;

                        default:
                            throw new ArgumentException();
                        }

                        result = this.NewtonsoftFormat(result);

                        Assert.AreEqual(json, result);
                        string sourceType = (source is IJsonReader) ? "Reader" : "Navigator";
                        Console.WriteLine($"{sourceFormat} {sourceType} to {destinationFormat} Writer took {stopwatch.ElapsedMilliseconds}ms");
                    }
                }
            }
        }