Exemplo n.º 1
0
        private static string NewtonsoftFormat(string json)
        {
            NewtonsoftToCosmosDBReader newtonsoftToCosmosDBReader = NewtonsoftToCosmosDBReader.CreateFromString(json);
            NewtonsoftToCosmosDBWriter newtonsoftToCosmosDBWriter = NewtonsoftToCosmosDBWriter.CreateTextWriter();

            newtonsoftToCosmosDBWriter.WriteAll(newtonsoftToCosmosDBReader);
            return(Encoding.UTF8.GetString(newtonsoftToCosmosDBWriter.GetResult()));
        }
        private static void PerformRoundTrip(
            SerializationFormat sourceFormat,
            SerializationFormat destinationFormat,
            string json,
            bool writeAsRootNode)
        {
            IJsonReader reader = sourceFormat switch
            {
                SerializationFormat.Text => JsonReader.Create(Encoding.UTF8.GetBytes(json)),
                SerializationFormat.Binary => JsonReader.Create(JsonTestUtils.ConvertTextToBinary(json)),
                SerializationFormat.NewtonsoftText => NewtonsoftToCosmosDBReader.CreateFromString(json),
                _ => throw new ArgumentException($"Unexpected {nameof(sourceFormat)} of type: {sourceFormat}"),
            };

            IJsonNavigator navigator = sourceFormat switch
            {
                SerializationFormat.Text => JsonNavigator.Create(Encoding.UTF8.GetBytes(json)),
                SerializationFormat.Binary => JsonNavigator.Create(JsonTestUtils.ConvertTextToBinary(json)),
                SerializationFormat.NewtonsoftText => new JsonNewtonsoftNavigator(json),
                _ => throw new ArgumentException($"Unexpected {nameof(sourceFormat)} of type: {sourceFormat}"),
            };

            foreach (object source in new object[] { reader, navigator })
            {
                IJsonWriter writer = destinationFormat switch
                {
                    SerializationFormat.Text => JsonWriter.Create(JsonSerializationFormat.Text),
                    SerializationFormat.Binary => JsonWriter.Create(JsonSerializationFormat.Binary),
                    SerializationFormat.NewtonsoftText => NewtonsoftToCosmosDBWriter.CreateTextWriter(),
                    _ => throw new ArgumentException($"Unexpected {nameof(destinationFormat)} of type: {destinationFormat}"),
                };

                switch (source)
                {
                case IJsonReader sourceReader:
                    sourceReader.WriteAll(writer);
                    break;

                case IJsonNavigator sourceNavigator:
                    if (writeAsRootNode)
                    {
                        sourceNavigator.WriteNode(sourceNavigator.GetRootNode(), writer);
                    }
                    else
                    {
                        IJsonNavigatorNode rootNode     = sourceNavigator.GetRootNode();
                        JsonNodeType       jsonNodeType = sourceNavigator.GetNodeType(rootNode);
                        switch (jsonNodeType)
                        {
                        case JsonNodeType.Array:
                            writer.WriteArrayStart();

                            foreach (IJsonNavigatorNode arrayItem in sourceNavigator.GetArrayItems(rootNode))
                            {
                                sourceNavigator.WriteNode(arrayItem, writer);
                            }

                            writer.WriteArrayEnd();
                            break;

                        case JsonNodeType.Object:
                            writer.WriteObjectStart();

                            foreach (ObjectProperty objectProperty in sourceNavigator.GetObjectProperties(rootNode))
                            {
                                sourceNavigator.WriteNode(objectProperty.NameNode, writer);
                                sourceNavigator.WriteNode(objectProperty.ValueNode, writer);
                            }

                            writer.WriteObjectEnd();
                            break;

                        default:
                            sourceNavigator.WriteNode(sourceNavigator.GetRootNode(), writer);
                            break;
                        }
                    }
                    break;

                default:
                    Assert.Fail("Failed to downcast source type.");
                    break;
                }

                string result = writer.SerializationFormat switch
                {
                    JsonSerializationFormat.Text => Utf8String.UnsafeFromUtf8BytesNoValidation(writer.GetResult()).ToString(),
                    JsonSerializationFormat.Binary => JsonTestUtils.ConvertBinaryToText(writer.GetResult()),
                    _ => throw new ArgumentException(),
                };
                string normalizedResult = JsonRoundTripsTests.NewtonsoftFormat(result);
                string normalizedJson   = JsonRoundTripsTests.NewtonsoftFormat(json);

                Assert.AreEqual(normalizedJson, normalizedResult);
            }
        }
Exemplo n.º 3
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(
                NewtonsoftToCosmosDBReader.CreateFromString(json),
                numberOfIterations);
            TimeSpan newtonsoftWriterTime = JsonPerfMeasurement.MeasureWritePerformance(
                NewtonsoftToCosmosDBWriter.CreateTextWriter(),
                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);
        }
        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 = NewtonsoftToCosmosDBReader.CreateFromString(json);
                        break;

                    case SerializationFormat.BinaryWithDictionaryEncoding:
                        JsonStringDictionary jsonStringDictionary = new JsonStringDictionary(capacity: 128);
                        reader = JsonReader.Create(JsonTestUtils.ConvertTextToBinary(json, jsonStringDictionary), jsonStringDictionary);
                        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;

                    case SerializationFormat.BinaryWithDictionaryEncoding:
                        JsonStringDictionary jsonStringDictionary = new JsonStringDictionary(capacity: 128);
                        navigator = JsonNavigator.Create(JsonTestUtils.ConvertTextToBinary(json, jsonStringDictionary), jsonStringDictionary);
                        break;

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

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

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

                        case SerializationFormat.NewtonsoftText:
                            writer = NewtonsoftToCosmosDBWriter.CreateTextWriter();
                            jsonStringDictionary = null;
                            break;

                        case SerializationFormat.BinaryWithDictionaryEncoding:
                            jsonStringDictionary = new JsonStringDictionary(capacity: 128);
                            writer = JsonWriter.Create(JsonSerializationFormat.Binary, jsonStringDictionary);
                            break;

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

                        Stopwatch stopwatch = Stopwatch.StartNew();
                        switch (source)
                        {
                        case IJsonReader sourceReader:
                            writer.WriteAll(sourceReader);
                            break;

                        case IJsonNavigator sourceNavigator:
                            writer.WriteJsonNode(sourceNavigator, sourceNavigator.GetRootNode());
                            break;

                        default:
                            Assert.Fail("Failed to downcast source type.");
                            break;
                        }
                        stopwatch.Stop();

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

                        case JsonSerializationFormat.Binary:
                            result = JsonTestUtils.ConvertBinaryToText(writer.GetResult().ToArray(), jsonStringDictionary);
                            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");
                    }
                }
            }
        }
Exemplo n.º 5
0
        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 = NewtonsoftToCosmosDBReader.CreateFromString(json);
                        break;

                    case NewtonsoftWrapperFormat.CosmosDBText:
                        reader = JsonReader.Create(Encoding.UTF8.GetBytes(json));
                        break;

                    case NewtonsoftWrapperFormat.CosmosDBBinary:
                        reader = JsonReader.Create(JsonPerfMeasurement.ConvertTextToBinary(json));
                        break;

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

                    IJsonWriter writer;
                    switch (destinationFormat)
                    {
                    case NewtonsoftWrapperFormat.NewtonsoftText:
                        writer = NewtonsoftToCosmosDBWriter.CreateTextWriter();
                        break;

                    case NewtonsoftWrapperFormat.CosmosDBText:
                        writer = JsonWriter.Create(JsonSerializationFormat.Text);
                        break;

                    case NewtonsoftWrapperFormat.CosmosDBBinary:
                        writer = JsonWriter.Create(JsonSerializationFormat.Binary);
                        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().ToArray());
                        break;

                    case JsonSerializationFormat.Binary:
                        result = JsonTestUtils.ConvertBinaryToText(writer.GetResult().ToArray());
                        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 static void PerformRoundTrip(
            SerializationFormat sourceFormat,
            SerializationFormat destinationFormat,
            string json)
        {
            JsonStringDictionary sourceDictionary = null;

            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 = NewtonsoftToCosmosDBReader.CreateFromString(json);
                break;

            case SerializationFormat.BinaryWithDictionaryEncoding:
                sourceDictionary = new JsonStringDictionary(capacity: 128);
                reader           = JsonReader.Create(JsonTestUtils.ConvertTextToBinary(json, sourceDictionary), sourceDictionary);
                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;

            case SerializationFormat.BinaryWithDictionaryEncoding:
                sourceDictionary = new JsonStringDictionary(capacity: 128);
                navigator        = JsonNavigator.Create(JsonTestUtils.ConvertTextToBinary(json, sourceDictionary), sourceDictionary);
                break;

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

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

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

                case SerializationFormat.NewtonsoftText:
                    writer = NewtonsoftToCosmosDBWriter.CreateTextWriter();
                    jsonStringDictionary = null;
                    break;

                case SerializationFormat.BinaryWithDictionaryEncoding:
                    jsonStringDictionary = new JsonStringDictionary(capacity: 128);
                    if (sourceFormat == SerializationFormat.BinaryWithDictionaryEncoding)
                    {
                        int index = 0;
                        while (sourceDictionary.TryGetStringAtIndex(index++, out UtfAllString value))
                        {
                            Assert.IsTrue(jsonStringDictionary.TryAddString(value.Utf16String, out _));
                        }
                    }

                    writer = JsonWriter.Create(JsonSerializationFormat.Binary, jsonStringDictionary);
                    break;

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

                switch (source)
                {
                case IJsonReader sourceReader:
                    sourceReader.WriteAll(writer);
                    break;

                case IJsonNavigator sourceNavigator:
                    sourceNavigator.WriteNode(sourceNavigator.GetRootNode(), writer);
                    break;

                default:
                    Assert.Fail("Failed to downcast source type.");
                    break;
                }

                string result;
                switch (writer.SerializationFormat)
                {
                case JsonSerializationFormat.Text:
                    result = Utf8String.UnsafeFromUtf8BytesNoValidation(writer.GetResult()).ToString();
                    break;

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

                default:
                    throw new ArgumentException();
                }

                string normalizedResult = JsonRoundTripsTests.NewtonsoftFormat(result);
                string normalizedJson   = JsonRoundTripsTests.NewtonsoftFormat(json);

                Assert.AreEqual(normalizedJson, normalizedResult);
            }
        }