Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create parse micro-bench for JsonNode and fix other JsonNode bench namespace #4391

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
161 changes: 161 additions & 0 deletions src/benchmarks/micro/libraries/System.Text.Json/Node/NodeParse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using BenchmarkDotNet.Attributes;
using MicroBenchmarks;
using System.Text.Json.Document.Tests;
using System.Text.Json.Tests;

namespace System.Text.Json.Nodes.Tests;

[BenchmarkCategory(Categories.Libraries, Categories.JSON)]
public class Perf_NodeParse
{
public enum TestCaseType
{
HelloWorld,
BasicJson,
Json400B,
Json400KB
}

[ParamsAllValues]
public TestCaseType TestCase;

[Params(true, false)]
public bool IsDataIndented;

[Params(false, true)]
public bool TestRandomAccess;

private byte[] _dataUtf8;

[GlobalSetup]
public void Setup()
{
string jsonString = JsonStrings.ResourceManager.GetString(TestCase.ToString());

// Remove all formatting/indentation
if (!IsDataIndented)
{
_dataUtf8 = DocumentHelpers.RemoveFormatting(jsonString);
}
else
{
_dataUtf8 = Encoding.UTF8.GetBytes(jsonString);
}
}

[Benchmark]
[MemoryRandomization]
public void Parse()
{
JsonNode node = JsonNode.Parse(_dataUtf8);
if (TestRandomAccess)
{
if (TestCase == TestCaseType.HelloWorld)
{
ReadHelloWorld(node);
}
else if (TestCase == TestCaseType.Json400B)
{
ReadJson400B(node);
}
else if (TestCase == TestCaseType.BasicJson)
{
ReadJsonBasic(node);
}
else if (TestCase == TestCaseType.Json400KB)
{
ReadJson400KB(node);
}
}
}

private static string ReadHelloWorld(JsonNode node)
{
string message = node["message"].GetValue<string>();
return message;
}

private static void ReadJson400B(JsonNode node)
{
JsonArray nodeArr = node.AsArray();
for (int i = 0; i < nodeArr.Count; i++)
{
nodeArr[i]["_id"].GetValue<string>();
nodeArr[i]["index"].GetValue<int>();
nodeArr[i]["isActive"].GetValue<bool>();
nodeArr[i]["balance"].GetValue<string> ();
nodeArr[i]["picture"].GetValue<string>();
nodeArr[i]["age"].GetValue<int>();
nodeArr[i]["email"].GetValue<string>();
nodeArr[i]["phone"].GetValue<string>();
nodeArr[i]["address"].GetValue<string>();
nodeArr[i]["registered"].GetValue<string>();
nodeArr[i]["latitude"].GetValue<double>();
nodeArr[i]["longitude"].GetValue<double>();
}
}

private static void ReadJsonBasic(JsonNode node)
{
node["age"].GetValue<int>();
node["first"].GetValue<string>();
node["last"].GetValue<string>();

JsonArray phoneNumbers = node["phoneNumbers"].AsArray();
for (int i = 0; i < phoneNumbers.Count; i++)
{
phoneNumbers[i].GetValue<string>();
}

JsonNode address = node["address"];
address["street"].GetValue<string>();
address["city"].GetValue<string>();
address["zip"].GetValue<int>();
}

private static void ReadJson400KB(JsonNode node)
{
JsonArray nodeArr = node.AsArray();
for (int i = 0; i < nodeArr.Count; i++)
{
nodeArr[i]["_id"].GetValue<string>();
nodeArr[i]["index"].GetValue<int>();
nodeArr[i]["guid"].GetValue<string>();
nodeArr[i]["isActive"].GetValue<bool>();
nodeArr[i]["balance"].GetValue<string>();
nodeArr[i]["picture"].GetValue<string>();
nodeArr[i]["age"].GetValue<int>();
nodeArr[i]["eyeColor"].GetValue<string>();
nodeArr[i]["name"].GetValue<string>();
nodeArr[i]["gender"].GetValue<string>();
nodeArr[i]["company"].GetValue<string>();
nodeArr[i]["email"].GetValue<string>();
nodeArr[i]["phone"].GetValue<string>();
nodeArr[i]["address"].GetValue<string>();
nodeArr[i]["about"].GetValue<string>();
nodeArr[i]["registered"].GetValue<string>();
nodeArr[i]["latitude"].GetValue<double>();
nodeArr[i]["longitude"].GetValue<double>();

JsonArray tagsObject = nodeArr[i]["tags"].AsArray();
for (int j = 0; j < tagsObject.Count; j++)
{
tagsObject[j].GetValue<string>();
}

JsonArray friendsObject = node[i]["friends"].AsArray();
for (int j = 0; j < friendsObject.Count; j++)
{
friendsObject[j]["id"].GetValue<int>();
friendsObject[j]["name"].GetValue<string>();
}

node[i]["greeting"].GetValue<string>();
node[i]["favoriteFruit"].GetValue<string>();
}
}
}
130 changes: 66 additions & 64 deletions src/benchmarks/micro/libraries/System.Text.Json/Node/ParseThenWrite.cs
Original file line number Diff line number Diff line change
@@ -1,89 +1,91 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

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
namespace System.Text.Json.Nodes.Tests;

[BenchmarkCategory(Categories.Libraries, Categories.JSON)]
public class Perf_ParseThenWrite
{
[BenchmarkCategory(Categories.Libraries, Categories.JSON)]
public class Perf_ParseThenWrite
public enum TestCaseType
{
public enum TestCaseType
{
HelloWorld,
DeepTree,
BroadTree,
LotsOfNumbers,
LotsOfStrings,
Json400B,
Json4KB,
Json400KB
}

private byte[] _dataUtf8;
private Utf8JsonWriter _writer;
HelloWorld,
DeepTree,
BroadTree,
LotsOfNumbers,
LotsOfStrings,
Json400B,
Json4KB,
Json400KB
}

[ParamsAllValues]
public TestCaseType TestCase;
private byte[] _dataUtf8;
private Utf8JsonWriter _writer;

[Params(true, false)]
public bool IsDataIndented;
[ParamsAllValues]
public TestCaseType TestCase;

[GlobalSetup]
public void Setup()
{
string jsonString = JsonStrings.ResourceManager.GetString(TestCase.ToString());
[Params(true, false)]
public bool IsDataIndented;

if (!IsDataIndented)
{
_dataUtf8 = DocumentHelpers.RemoveFormatting(jsonString);
}
else
{
_dataUtf8 = Encoding.UTF8.GetBytes(jsonString);
}
[GlobalSetup]
public void Setup()
{
string jsonString = JsonStrings.ResourceManager.GetString(TestCase.ToString());

var abw = new ArrayBufferWriter<byte>();
_writer = new Utf8JsonWriter(abw, new JsonWriterOptions { Indented = IsDataIndented });
if (!IsDataIndented)
{
_dataUtf8 = DocumentHelpers.RemoveFormatting(jsonString);
}

[GlobalCleanup]
public void CleanUp()
else
{
_writer.Dispose();
_dataUtf8 = Encoding.UTF8.GetBytes(jsonString);
}

[Benchmark]
[MemoryRandomization]
public void ParseThenWrite()
{
_writer.Reset();
var abw = new ArrayBufferWriter<byte>();
_writer = new Utf8JsonWriter(abw, new JsonWriterOptions { Indented = IsDataIndented });
}

[GlobalCleanup]
public void CleanUp()
{
_writer.Dispose();
}

JsonNode jsonNode = JsonNode.Parse(_dataUtf8);
WalkNode(jsonNode);
jsonNode.WriteTo(_writer);
[Benchmark]
[MemoryRandomization]
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;
}
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;
}
}
}
Expand Down
Loading