Skip to content
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
66 changes: 66 additions & 0 deletions YamlDotNet.Benchmark/AliasBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// This file is part of YamlDotNet - A .NET library for YAML.
// Copyright (c) Antoine Aubry and contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using System.Collections.Generic;
using BenchmarkDotNet.Attributes;
using YamlDotNet.Serialization;

namespace YamlDotNet.Benchmark;

// Exercises the anchor/alias path, which the rest of the suite does not touch. The graph holds
// many references to a small set of shared objects, so serialization (with aliases enabled, the
// default) emits anchors + aliases, and deserialization resolves them via the alias value
// deserializer. This isolates the anchor-assignment, anchor-name and alias-resolution hot paths.
public class AliasBenchmarks
{
private const int ReferenceCount = 5000;

private readonly ISerializer serializer = new SerializerBuilder().Build();
private readonly IDeserializer deserializer = new DeserializerBuilder().Build();

private List<Address> graph = null!;
private string yaml = "";

[GlobalSetup]
public void Setup()
{
var shared = new[]
{
new Address { Street = "1 Shared Way", City = "Common", State = "CA", Zip = "90001", Country = "US" },
new Address { Street = "2 Shared Way", City = "Common", State = "CA", Zip = "90002", Country = "US" },
new Address { Street = "3 Shared Way", City = "Common", State = "CA", Zip = "90003", Country = "US" },
};

graph = new List<Address>(ReferenceCount);
for (var i = 0; i < ReferenceCount; i++)
{
graph.Add(shared[i % shared.Length]);
}

yaml = serializer.Serialize(graph);
}

[Benchmark]
public string SerializeWithAliases() => serializer.Serialize(graph);

[Benchmark]
public List<Address> DeserializeWithAliases() => deserializer.Deserialize<List<Address>>(yaml);
}
4 changes: 0 additions & 4 deletions YamlDotNet.Benchmark/BigFileBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,12 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using System.IO.Compression;
using System.Text;
using BenchmarkDotNet.Attributes;
using FastSerialization;
using YamlDotNet.RepresentationModel;
using YamlDotNet.Serialization;

namespace YamlDotNet.Benchmark;

[MemoryDiagnoser]
public class BigFileBenchmark
{
private string yamlString = "";
Expand Down
60 changes: 60 additions & 0 deletions YamlDotNet.Benchmark/ObjectGraphBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// This file is part of YamlDotNet - A .NET library for YAML.
// Copyright (c) Antoine Aubry and contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using BenchmarkDotNet.Attributes;
using YamlDotNet.Serialization;

namespace YamlDotNet.Benchmark;

// Exercises the full serialize / deserialize object-mapping pipeline over a realistic,
// heterogeneous typed graph (see SampleModel). This is the read-path-weighted core of the
// campaign: DeserializeTyped and RoundtripTyped drive the reflection/property-lookup hot paths,
// SerializeTyped drives object-graph traversal + the emitter, and DeserializeUntyped drives the
// scalar/collection/dictionary deserializers with runtime type resolution.
public class ObjectGraphBenchmarks
{
private const int EmployeeCount = 2000;

private readonly ISerializer serializer = new SerializerBuilder().Build();
private readonly IDeserializer deserializer = new DeserializerBuilder().Build();

private Company company = null!;
private string yaml = "";

[GlobalSetup]
public void Setup()
{
company = SampleModel.CreateCompany(EmployeeCount);
yaml = serializer.Serialize(company);
}

[Benchmark]
public string SerializeTyped() => serializer.Serialize(company);

[Benchmark]
public Company DeserializeTyped() => deserializer.Deserialize<Company>(yaml);

[Benchmark]
public object? DeserializeUntyped() => deserializer.Deserialize<object>(yaml);

[Benchmark]
public Company RoundtripTyped() => deserializer.Deserialize<Company>(serializer.Serialize(company));
}
86 changes: 86 additions & 0 deletions YamlDotNet.Benchmark/ProfilingDriver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// This file is part of YamlDotNet - A .NET library for YAML.
// Copyright (c) Antoine Aubry and contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using System;
using System.Diagnostics;
using YamlDotNet.Serialization;

namespace YamlDotNet.Benchmark;

// Steady-state CPU-profiling driver, kept out of BenchmarkDotNet so a sampling profiler (ultra)
// sees only YamlDotNet work rather than the BDN harness. It reuses a cached serializer /
// deserializer and a single pre-serialized document (mirroring real reuse), then loops the unit of
// work for a fixed wall-clock window so the profile settles into steady state. The default "both"
// mode is naturally deserialize-weighted because the read path dominates the per-iteration cost.
//
// Capture (elevated shell; profile the built exe directly, not `dotnet run`):
// ultra profile -o baseline --delay 3 -- YamlDotNet.Benchmark.exe profile [both|deser|ser] [seconds]
internal static class ProfilingDriver
{
public static void Run(string[] args)
{
var mode = args.Length > 1 ? args[1].ToLowerInvariant() : "both";
var seconds = (args.Length > 2 && int.TryParse(args[2], out var s)) ? s : 25;

var serializer = new SerializerBuilder().Build();
var deserializer = new DeserializerBuilder().Build();

var company = SampleModel.CreateCompany(2000);
var yaml = serializer.Serialize(company);

// Validate the workload up front so a broken run fails fast (ultra runs the target silently).
var check = deserializer.Deserialize<Company>(yaml);
if (check.Departments.Count == 0 || check.Departments[0].Employees.Count == 0)
{
throw new InvalidOperationException("Profiling workload produced an empty graph.");
}

var stopwatch = Stopwatch.StartNew();
long iterations = 0;
long checksum = 0;

while (stopwatch.Elapsed.TotalSeconds < seconds)
{
if (mode != "ser")
{
var round = deserializer.Deserialize<Company>(yaml);
checksum += round.Departments.Count;
}

if (mode != "deser")
{
var text = serializer.Serialize(company);
checksum += text.Length;
}

iterations++;
}

// Guard against dead-code elimination of the loop body.
if (checksum < 0)
{
throw new InvalidOperationException("unreachable");
}

Console.Error.WriteLine(
$"Profiling driver finished: mode={mode}, iterations={iterations}, elapsed={stopwatch.Elapsed.TotalSeconds:F1}s");
}
}
19 changes: 18 additions & 1 deletion YamlDotNet.Benchmark/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,24 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Running;
using YamlDotNet.Benchmark;

BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
// Profiling driver mode (for a CPU sampling profiler such as ultra) — a steady-state loop that
// bypasses the BenchmarkDotNet harness. See ProfilingDriver for usage.
if (args.Length > 0 && args[0].Equals("profile", StringComparison.OrdinalIgnoreCase))
{
ProfilingDriver.Run(args);
return;
}

// Shared config for every benchmark: add MemoryDiagnoser for allocation tracking. DefaultConfig
// already emits a GitHub-flavoured markdown export, so before/after tables can be pasted straight
// into a PR. Run on a quiet machine so the numbers stay clean:
// dotnet run -c Release --project YamlDotNet.Benchmark -f net10.0 -- --filter '*'
var config = DefaultConfig.Instance
.AddDiagnoser(MemoryDiagnoser.Default);

BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args, config);
Binary file removed YamlDotNet.Benchmark/Resources/saltern.yml.gz
Binary file not shown.
Loading
Loading