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

Add JsonNode construction benchmarks #4463

Merged
merged 2 commits into from
Sep 18, 2024
Merged
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
105 changes: 105 additions & 0 deletions src/benchmarks/micro/libraries/System.Text.Json/Node/Perf_Create.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using BenchmarkDotNet.Attributes;
using MicroBenchmarks;
using System.Text.Json.Nodes;

namespace System.Text.Json.Node.Tests
{
[BenchmarkCategory(Categories.Libraries, Categories.JSON)]
public class Perf_Create
{
private readonly JsonNode[] _results = new JsonNode[50];

[Benchmark]
public Span<JsonNode> Create_JsonBool()
{
Span<JsonNode> results = _results.AsSpan();
for (int i = 0; i < results.Length; i++)
{
results[i] = (JsonNode)true;
}

return results;
}

[Benchmark]
public Span<JsonNode> Create_JsonNumber()
{
Span<JsonNode> results = _results.AsSpan();
for (int i = 0; i < results.Length; i++)
{
results[i] = (JsonNode)42;
}

return results;
}

[Benchmark]
public Span<JsonNode> Create_JsonString()
{
Span<JsonNode> results = _results.AsSpan();
for (int i = 0; i < results.Length; i++)
{
results[i] = (JsonNode)"some string";
}

return results;
}

[Benchmark]
public Span<JsonNode> Create_JsonArray()
{
Span<JsonNode> results = _results.AsSpan(0, 20);
for (int i = 0; i < results.Length; i++)
{
results[i] = new JsonArray { null, null, null, null };
}

return results;
}

[Benchmark]
public JsonNode Create_JsonObject_Small()
{
return new JsonObject
{
["prop0"] = null,
["prop1"] = null,
["prop2"] = null,
["prop3"] = null,
["prop4"] = null,
["prop5"] = null,
["prop6"] = null,
["prop7"] = null,
["prop8"] = null,
};
}

[Benchmark]
public JsonNode Create_JsonObject_Large()
{
return new JsonObject
{
["prop0"] = null,
["prop1"] = null,
["prop2"] = null,
["prop3"] = null,
["prop4"] = null,
["prop5"] = null,
["prop6"] = null,
["prop7"] = null,
["prop8"] = null,
["prop9"] = null,
["prop10"] = null,
["prop11"] = null,
["prop12"] = null,
["prop13"] = null,
["prop14"] = null,
["prop15"] = null,
["prop16"] = null,
["prop17"] = null,
["prop18"] = null,
["prop19"] = null,
};
}
}
}
Loading