69 lines
3.2 KiB
C#
69 lines
3.2 KiB
C#
using BenchmarkDotNet.Running;
|
|
using AyCode.Core.Benchmarks;
|
|
|
|
namespace BenchmarkSuite1
|
|
{
|
|
internal class Program
|
|
{
|
|
static void Main(string[] args)
|
|
{
|
|
// Quick size comparison test
|
|
if (args.Length > 0 && args[0] == "--sizes")
|
|
{
|
|
RunSizeComparison();
|
|
return;
|
|
}
|
|
|
|
// Use assembly-wide discovery for all benchmarks
|
|
BenchmarkSwitcher.FromAssembly(typeof(SerializationBenchmarks).Assembly).Run(args);
|
|
}
|
|
|
|
static void RunSizeComparison()
|
|
{
|
|
Console.WriteLine("=== JSON Size Comparison ===\n");
|
|
|
|
var benchmark = new AyCode.Core.Benchmarks.SerializationBenchmarks();
|
|
|
|
// Manually invoke setup
|
|
var setupMethod = typeof(AyCode.Core.Benchmarks.SerializationBenchmarks)
|
|
.GetMethod("Setup");
|
|
setupMethod?.Invoke(benchmark, null);
|
|
|
|
// Get JSON sizes via reflection (private fields)
|
|
var newtonsoftJson = typeof(AyCode.Core.Benchmarks.SerializationBenchmarks)
|
|
.GetField("_newtonsoftJson", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
|
|
?.GetValue(benchmark) as string;
|
|
|
|
var ayCodeJson = typeof(AyCode.Core.Benchmarks.SerializationBenchmarks)
|
|
.GetField("_ayCodeJson", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
|
|
?.GetValue(benchmark) as string;
|
|
|
|
if (newtonsoftJson != null && ayCodeJson != null)
|
|
{
|
|
var newtonsoftBytes = System.Text.Encoding.UTF8.GetByteCount(newtonsoftJson);
|
|
var ayCodeBytes = System.Text.Encoding.UTF8.GetByteCount(ayCodeJson);
|
|
|
|
Console.WriteLine($"Newtonsoft JSON (no refs):");
|
|
Console.WriteLine($" - Characters: {newtonsoftJson.Length:N0}");
|
|
Console.WriteLine($" - Bytes: {newtonsoftBytes:N0} ({newtonsoftBytes / 1024.0 / 1024.0:F2} MB)");
|
|
Console.WriteLine();
|
|
|
|
Console.WriteLine($"AyCode JSON (with refs):");
|
|
Console.WriteLine($" - Characters: {ayCodeJson.Length:N0}");
|
|
Console.WriteLine($" - Bytes: {ayCodeBytes:N0} ({ayCodeBytes / 1024.0 / 1024.0:F2} MB)");
|
|
Console.WriteLine();
|
|
|
|
var reduction = (1.0 - (double)ayCodeBytes / newtonsoftBytes) * 100;
|
|
Console.WriteLine($"Size Reduction: {reduction:F1}%");
|
|
Console.WriteLine($"AyCode is {(reduction > 0 ? "smaller" : "larger")} by {Math.Abs(newtonsoftBytes - ayCodeBytes):N0} bytes");
|
|
|
|
// Count $ref occurrences
|
|
var refCount = System.Text.RegularExpressions.Regex.Matches(ayCodeJson, @"\$ref").Count;
|
|
var idCount = System.Text.RegularExpressions.Regex.Matches(ayCodeJson, @"\$id").Count;
|
|
Console.WriteLine($"\nAyCode $id count: {idCount}");
|
|
Console.WriteLine($"AyCode $ref count: {refCount}");
|
|
}
|
|
}
|
|
}
|
|
}
|