Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/benchmarks/micro/MicroBenchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
<Compile Remove="libraries\Common\AlignedMemory.cs" />
<Compile Remove="libraries\System.IO.FileSystem\Perf.RandomAccess.cs" />
<Compile Remove="libraries\System.IO.FileSystem\Perf.RandomAccess.NoBuffering.cs" />
<Compile Remove="libraries\System.Text.Json\Node\*.cs" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFrameworkIdentifier)' == '.NETFramework' Or ('$(TargetFrameworkIdentifier)' == '.NETCoreApp' And '$(_TargetFrameworkVersionWithoutV)' &lt; '7.0')">
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
Comment on lines +40 to +47
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!IsDataIndented)
{
_dataUtf8 = DocumentHelpers.RemoveFormatting(jsonString);
}
else
{
_dataUtf8 = Encoding.UTF8.GetBytes(jsonString);
}
_dataUtf8 = IsDataIndented ?
Encoding.UTF8.GetBytes(jsonString) :
DocumentHelpers.RemoveFormatting(jsonString);

?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initialization code is copypasta'd from the equivalent JsonDocument benchmark (also called ParseThenWrite.cs).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initialization code is copypasta'd from the equivalent

FWIW Setup and cleanup code can be always refactored and improved (duplicated code moved to helper type), as changing it typically does not affect the benchmark itself


var abw = new ArrayBufferWriter<byte>();
_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<string, JsonNode> kvp in obj)
WalkNode(kvp.Value);
break;
case JsonArray arr:
foreach (JsonNode elem in arr)
WalkNode(elem);
break;
}
}
}
}
}