diff --git a/src/benchmarks/micro/MicroBenchmarks.csproj b/src/benchmarks/micro/MicroBenchmarks.csproj index 2985a171ba9..04b3bf66eb8 100644 --- a/src/benchmarks/micro/MicroBenchmarks.csproj +++ b/src/benchmarks/micro/MicroBenchmarks.csproj @@ -154,6 +154,7 @@ + diff --git a/src/benchmarks/micro/libraries/System.Text.Json/Node/ParseThenWrite.cs b/src/benchmarks/micro/libraries/System.Text.Json/Node/ParseThenWrite.cs new file mode 100644 index 00000000000..928efbeee14 --- /dev/null +++ b/src/benchmarks/micro/libraries/System.Text.Json/Node/ParseThenWrite.cs @@ -0,0 +1,89 @@ +using BenchmarkDotNet.Attributes; +using MicroBenchmarks; +using System.Buffers; +using System.Collections.Generic; +using System.Text.Json.Document.Tests; +using System.Text.Json.Nodes; +using System.Text.Json.Tests; + +namespace System.Text.Json.Node.Tests +{ + [BenchmarkCategory(Categories.Libraries, Categories.JSON)] + public class Perf_ParseThenWrite + { + public enum TestCaseType + { + HelloWorld, + DeepTree, + BroadTree, + LotsOfNumbers, + LotsOfStrings, + Json400B, + Json4KB, + Json400KB + } + + private byte[] _dataUtf8; + private Utf8JsonWriter _writer; + + [ParamsAllValues] + public TestCaseType TestCase; + + [Params(true, false)] + public bool IsDataIndented; + + [GlobalSetup] + public void Setup() + { + string jsonString = JsonStrings.ResourceManager.GetString(TestCase.ToString()); + + if (!IsDataIndented) + { + _dataUtf8 = DocumentHelpers.RemoveFormatting(jsonString); + } + else + { + _dataUtf8 = Encoding.UTF8.GetBytes(jsonString); + } + + var abw = new ArrayBufferWriter(); + _writer = new Utf8JsonWriter(abw, new JsonWriterOptions { Indented = IsDataIndented }); + } + + [GlobalCleanup] + public void CleanUp() + { + _writer.Dispose(); + } + + [Benchmark] + public void ParseThenWrite() + { + _writer.Reset(); + + JsonNode jsonNode = JsonNode.Parse(_dataUtf8); + WalkNode(jsonNode); + jsonNode.WriteTo(_writer); + + + static void WalkNode(JsonNode node) + { + // Forces conversion of lazy JsonElement representation of document into + // a materialized JsonNode tree so that we measure writing performance + // of the latter representation. + + switch (node) + { + case JsonObject obj: + foreach (KeyValuePair kvp in obj) + WalkNode(kvp.Value); + break; + case JsonArray arr: + foreach (JsonNode elem in arr) + WalkNode(elem); + break; + } + } + } + } +}