141 lines
6.1 KiB
C#
141 lines
6.1 KiB
C#
using AyCode.Core.Compression;
|
|
using AyCode.Core.Serializers.Attributes;
|
|
using AyCode.Core.Serializers.Binaries;
|
|
using AyCode.Core.Tests.Serialization; // DrainFromAsync extension (test-only, used by benchmark)
|
|
using AyCode.Core.Tests.TestModels;
|
|
using MemoryPack;
|
|
#if !AYCODE_NATIVEAOT
|
|
using MessagePack;
|
|
using MessagePack.Resolvers;
|
|
#endif
|
|
using Microsoft.Extensions.Options;
|
|
using System.Buffers;
|
|
using System.Diagnostics;
|
|
using System.IO.Pipelines;
|
|
using System.IO.Pipes;
|
|
using System.Reflection;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
|
|
using AyCode.Core.Serializers.Console.Benchmarks;
|
|
|
|
namespace AyCode.Core.Serializers.Console;
|
|
|
|
/// <summary>
|
|
/// Comprehensive benchmark application for all serializers.
|
|
/// Compares: AcBinary (all options), MemoryPack, MessagePack, Newtonsoft.Json, System.Text.Json
|
|
///
|
|
/// Usage:
|
|
/// dotnet run # Run all benchmarks
|
|
/// dotnet run -- quick # Quick mode (fewer iterations)
|
|
/// dotnet run -- serialize # Serialize only
|
|
/// dotnet run -- deserialize # Deserialize only
|
|
/// </summary>
|
|
public static class Program
|
|
{
|
|
// Configuration (constants, mutable state, attribute-flag aggregation) → Configuration.cs
|
|
// BuildAcBinary + GetMemPack helpers → Benchmarks/BenchmarkOptions.cs
|
|
|
|
public static void Main(string[] args)
|
|
{
|
|
// Set console encoding to UTF-8 for proper Unicode character display
|
|
System.Console.OutputEncoding = Encoding.UTF8;
|
|
|
|
// Setup validation — abort BEFORE any benchmark logic if MemoryPack baseline is invalid.
|
|
// Done early so user is told immediately, not after warmup.
|
|
BenchmarkLoop.ValidateMemoryPackSetup();
|
|
|
|
// CLI mode (args provided): run once, parse args, exit. Backward-compatible behaviour.
|
|
if (args.Length > 0)
|
|
{
|
|
if (!TryParseCliArgs(args, out var layer, out var opMode, out var serializerMode))
|
|
return; // invalid args
|
|
|
|
BenchmarkLoop.RunBenchmark(layer, opMode, serializerMode);
|
|
return;
|
|
}
|
|
|
|
// Interactive mode (no args): loop the menu so the user doesn't have to restart between runs.
|
|
// Q exits the menu (and the application).
|
|
while (true)
|
|
{
|
|
var selection = Menu.ShowInteractiveMenu();
|
|
if (selection == null) return; // user pressed Q
|
|
|
|
BenchmarkLoop.RunBenchmark(selection.Value.layer, BenchmarkOpMode.All, selection.Value.serializerMode);
|
|
|
|
System.Console.WriteLine();
|
|
System.Console.WriteLine("─────────────────────────────────────────────────────────────────────");
|
|
System.Console.WriteLine("Returning to menu — press any key to continue, or Q to quit...");
|
|
var key = System.Console.ReadKey(intercept: true);
|
|
if (key.Key == ConsoleKey.Q) return;
|
|
System.Console.WriteLine();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Parses CLI arguments into (layer, opMode, serializerMode). Uses <see cref="Enum.TryParse{T}(string, bool, out T)"/>
|
|
/// case-insensitive against the three enums in this order: <see cref="SerializerSelectionMode"/> (matches
|
|
/// "FastestByte"/"AsyncPipe"), <see cref="BenchmarkOpMode"/> (matches "Serialize"/"Deserialize"), then
|
|
/// <see cref="BenchmarkLayer"/> (matches "Core"/"Comprehensive"/"Edge"/"Small"/...). Special-cased:
|
|
/// <c>"quick"</c> mutates <see cref="Configuration"/> warmup/iter counts but selects no layer/op/mode.
|
|
/// Unknown args silently default to (All, All, Standard) — matches the prior behavior where unrecognized
|
|
/// args fell through <see cref="BenchmarkLoop.FilterByLayer"/>'s default branch (full unfiltered suite).
|
|
/// Returns <c>false</c> only if the caller-side path would need to abort; currently always returns <c>true</c>.
|
|
/// </summary>
|
|
private static bool TryParseCliArgs(string[] args, out BenchmarkLayer layer, out BenchmarkOpMode opMode, out SerializerSelectionMode serializerMode)
|
|
{
|
|
layer = BenchmarkLayer.All;
|
|
opMode = BenchmarkOpMode.All;
|
|
serializerMode = SerializerSelectionMode.Standard;
|
|
|
|
var arg = args[0];
|
|
|
|
// Quick mode: short warmup, few iterations, small sample count. Not an enum value — it's a
|
|
// Configuration meta-flag, so handle it before the enum-parse cascade.
|
|
if (string.Equals(arg, "quick", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
Configuration.WarmupIterations = 5;
|
|
Configuration.TestIterations = 100;
|
|
Configuration.BenchmarkSamples = 3;
|
|
return true;
|
|
}
|
|
|
|
// Serializer-selection first (AsyncPipe/FastestByte/Standard) — narrower set than layers,
|
|
// and "AsyncPipe" forced layer=All in the old code anyway.
|
|
if (Enum.TryParse<SerializerSelectionMode>(arg, ignoreCase: true, out var sm))
|
|
{
|
|
serializerMode = sm;
|
|
return true;
|
|
}
|
|
|
|
// Op-mode (Serialize/Deserialize/All).
|
|
if (Enum.TryParse<BenchmarkOpMode>(arg, ignoreCase: true, out var om))
|
|
{
|
|
opMode = om;
|
|
return true;
|
|
}
|
|
|
|
// Layer (Core/Comprehensive/Edge/Small/Medium/Large/Repeated/Deep/All).
|
|
if (Enum.TryParse<BenchmarkLayer>(arg, ignoreCase: true, out var ly))
|
|
{
|
|
layer = ly;
|
|
return true;
|
|
}
|
|
|
|
// Unknown arg — defaults remain (All, All, Standard). Matches prior behaviour where the
|
|
// unrecognized string fell through FilterByLayer's `_ => all.ToList()` default branch.
|
|
System.Console.Error.WriteLine($"Warning: unrecognized argument '{arg}'. Running full suite (Layer=All, OpMode=All, SerializerMode=Standard).");
|
|
return true;
|
|
}
|
|
|
|
// RunBenchmark + RunBenchmarksForTestData + CreateSerializers → BenchmarkLoop.cs
|
|
|
|
// Serializer implementations (ISerializerBenchmark + 12 concrete benchmark classes) → Benchmarks/
|
|
|
|
// Results / output formatters → Output.cs
|
|
// BenchmarkResult DTO → BenchmarkResult.cs
|
|
|
|
}
|