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); }
private static void VerifyBinaryReader <T>(T expectedDeserializedValue) { string stringValue = NewtonsoftInteropTests.NewtonsoftFormat(JsonConvert.SerializeObject(expectedDeserializedValue)); byte[] result = JsonPerfMeasurement.ConvertTextToBinary(stringValue); NewtonsoftInteropTests.VerifyReader <T>(result, expectedDeserializedValue); }
private void PerformanceBenchmarkCuratedJson(string filename) { string path = string.Format("TestJsons/{0}", filename); string json = File.ReadAllText(path); const int numberOfIterations = 1; JsonPerfMeasurement.MeasurePerf(json, filename, numberOfIterations); }
private void TestWriterToReader(JsonTokenInfo[] tokens) { IJsonWriter jsonWriter = JsonWriter.Create(JsonSerializationFormat.Text); JsonPerfMeasurement.MeasureWritePerformance(tokens, jsonWriter); string writerResults = Encoding.UTF8.GetString(jsonWriter.GetResult()); IJsonReader jsonReader = JsonReader.Create(Encoding.UTF8.GetBytes(writerResults)); JsonTokenInfo[] tokenArrayFromReader = JsonPerfMeasurement.Tokenize(jsonReader, writerResults); tokenArrayFromReader.SequenceEqual(tokens); }
private void ExecuteMicroBenchmark(string token, string microName) { List <string> tokenArray = new List <string>(); for (int i = 0; i < 1000000; i++) { tokenArray.Add(token); } string tokenArrayJson = "[" + string.Join(",", tokenArray) + "]"; JsonPerfMeasurement.MeasurePerf(tokenArrayJson, microName + "Micro Benchmark"); }
/// <summary> /// Initializes a new instance of the JsonNewtonsoftCosmosDBBinaryReader class. /// </summary> /// <param name="jsonText">The json text.</param> public JsonNewtonsoftCosmosDBBinaryReader(string jsonText) : base(new JsonCosmosDBReader(new MemoryStream(JsonPerfMeasurement.ConvertTextToBinary(jsonText)))) { }
public static JsonToken[] Tokenize(string json) { IJsonReader jsonReader = JsonReader.Create(Encoding.UTF8.GetBytes(json)); return(JsonPerfMeasurement.Tokenize(jsonReader, json)); }
public static TimeSpan MeasureWritePerformance(IJsonWriter jsonWriter, string json, int numberOfIterations = 1) { JsonToken[] tokens = JsonPerfMeasurement.Tokenize(json); return(JsonPerfMeasurement.MeasureWritePerformance(tokens, jsonWriter, numberOfIterations)); }
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()); 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"); } } }