180 lines
7.2 KiB
C#
180 lines
7.2 KiB
C#
using BenchmarkDotNet.Running;
|
|
using AyCode.Core.Benchmarks;
|
|
using AyCode.Core.Extensions;
|
|
using AyCode.Core.Tests.TestModels;
|
|
using System.Text;
|
|
using MessagePack;
|
|
using MessagePack.Resolvers;
|
|
|
|
namespace BenchmarkSuite1
|
|
{
|
|
internal class Program
|
|
{
|
|
static void Main(string[] args)
|
|
{
|
|
if (args.Length > 0 && args[0] == "--test")
|
|
{
|
|
RunQuickTest();
|
|
return;
|
|
}
|
|
|
|
if (args.Length > 0 && args[0] == "--testmsgpack")
|
|
{
|
|
RunMessagePackTest();
|
|
return;
|
|
}
|
|
|
|
if (args.Length > 0 && args[0] == "--minimal")
|
|
{
|
|
BenchmarkRunner.Run<MinimalBenchmark>();
|
|
return;
|
|
}
|
|
|
|
if (args.Length > 0 && args[0] == "--simple")
|
|
{
|
|
BenchmarkRunner.Run<SimpleBinaryBenchmark>();
|
|
return;
|
|
}
|
|
|
|
if (args.Length > 0 && args[0] == "--complex")
|
|
{
|
|
BenchmarkRunner.Run<ComplexBinaryBenchmark>();
|
|
return;
|
|
}
|
|
|
|
if (args.Length > 0 && args[0] == "--msgpack")
|
|
{
|
|
BenchmarkRunner.Run<MessagePackComparisonBenchmark>();
|
|
return;
|
|
}
|
|
|
|
if (args.Length > 0 && args[0] == "--sizes")
|
|
{
|
|
RunSizeComparison();
|
|
return;
|
|
}
|
|
|
|
Console.WriteLine("Usage:");
|
|
Console.WriteLine(" --test Quick AcBinary test");
|
|
Console.WriteLine(" --testmsgpack Quick MessagePack test");
|
|
Console.WriteLine(" --minimal Minimal benchmark");
|
|
Console.WriteLine(" --simple Simple flat object benchmark");
|
|
Console.WriteLine(" --complex Complex hierarchy (AcBinary vs JSON)");
|
|
Console.WriteLine(" --msgpack MessagePack comparison");
|
|
Console.WriteLine(" --sizes Size comparison only");
|
|
|
|
if (args.Length == 0)
|
|
{
|
|
BenchmarkSwitcher.FromAssembly(typeof(MinimalBenchmark).Assembly).Run(args);
|
|
}
|
|
else
|
|
{
|
|
BenchmarkSwitcher.FromAssembly(typeof(MinimalBenchmark).Assembly).Run(args);
|
|
}
|
|
}
|
|
|
|
static void RunQuickTest()
|
|
{
|
|
Console.WriteLine("=== Quick AcBinary Test ===\n");
|
|
|
|
try
|
|
{
|
|
Console.WriteLine("Creating test data...");
|
|
var order = TestDataFactory.CreateBenchmarkOrder(
|
|
itemCount: 3,
|
|
palletsPerItem: 2,
|
|
measurementsPerPallet: 2,
|
|
pointsPerMeasurement: 5);
|
|
Console.WriteLine($"Created order with {order.Items.Count} items");
|
|
|
|
Console.WriteLine("\nTesting JSON serialization...");
|
|
var jsonOptions = AcJsonSerializerOptions.WithoutReferenceHandling();
|
|
var json = AcJsonSerializer.Serialize(order, jsonOptions);
|
|
Console.WriteLine($"JSON size: {json.Length:N0} chars, {Encoding.UTF8.GetByteCount(json):N0} bytes");
|
|
|
|
Console.WriteLine("\nTesting Binary serialization...");
|
|
var binaryOptions = AcBinarySerializerOptions.Default;
|
|
var binary = AcBinarySerializer.Serialize(order, binaryOptions);
|
|
Console.WriteLine($"Binary size: {binary.Length:N0} bytes");
|
|
|
|
Console.WriteLine("\nTesting Binary deserialization...");
|
|
var result = AcBinaryDeserializer.Deserialize<TestOrder>(binary);
|
|
Console.WriteLine($"Deserialized order: Id={result?.Id}, Items={result?.Items.Count}");
|
|
|
|
Console.WriteLine("\n=== All tests passed! ===");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"\n!!! ERROR: {ex.GetType().Name}: {ex.Message}");
|
|
Console.WriteLine(ex.StackTrace);
|
|
}
|
|
}
|
|
|
|
static void RunMessagePackTest()
|
|
{
|
|
Console.WriteLine("=== Quick MessagePack Test ===\n");
|
|
|
|
try
|
|
{
|
|
Console.WriteLine("Creating test data...");
|
|
var order = TestDataFactory.CreateBenchmarkOrder(
|
|
itemCount: 2,
|
|
palletsPerItem: 2,
|
|
measurementsPerPallet: 2,
|
|
pointsPerMeasurement: 3);
|
|
Console.WriteLine($"Created order with {order.Items.Count} items");
|
|
|
|
Console.WriteLine("\nTesting MessagePack serialization...");
|
|
var msgPackOptions = ContractlessStandardResolver.Options.WithCompression(MessagePackCompression.None);
|
|
var msgPack = MessagePackSerializer.Serialize(order, msgPackOptions);
|
|
Console.WriteLine($"MessagePack size: {msgPack.Length:N0} bytes");
|
|
|
|
Console.WriteLine("\nTesting MessagePack deserialization...");
|
|
var result = MessagePackSerializer.Deserialize<TestOrder>(msgPack, msgPackOptions);
|
|
Console.WriteLine($"Deserialized order: Id={result?.Id}, Items={result?.Items.Count}");
|
|
|
|
Console.WriteLine("\n=== MessagePack test passed! ===");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"\n!!! ERROR: {ex.GetType().Name}: {ex.Message}");
|
|
Console.WriteLine(ex.StackTrace);
|
|
}
|
|
}
|
|
|
|
static void RunSizeComparison()
|
|
{
|
|
Console.WriteLine("=== Size Comparison ===\n");
|
|
|
|
var order = TestDataFactory.CreateBenchmarkOrder(
|
|
itemCount: 3,
|
|
palletsPerItem: 2,
|
|
measurementsPerPallet: 2,
|
|
pointsPerMeasurement: 5);
|
|
|
|
var binaryWithRef = AcBinarySerializer.Serialize(order, AcBinarySerializerOptions.Default);
|
|
var binaryNoRef = AcBinarySerializer.Serialize(order, AcBinarySerializerOptions.WithoutReferenceHandling);
|
|
var json = AcJsonSerializer.Serialize(order, AcJsonSerializerOptions.WithoutReferenceHandling());
|
|
var jsonBytes = Encoding.UTF8.GetByteCount(json);
|
|
|
|
Console.WriteLine($"| Format | Size (bytes) | vs JSON |");
|
|
Console.WriteLine($"|-----------------|--------------|---------|");
|
|
Console.WriteLine($"| AcBinary | {binaryWithRef.Length,12:N0} | {100.0 * binaryWithRef.Length / jsonBytes,6:F1}% |");
|
|
Console.WriteLine($"| AcBinary(NoRef) | {binaryNoRef.Length,12:N0} | {100.0 * binaryNoRef.Length / jsonBytes,6:F1}% |");
|
|
Console.WriteLine($"| JSON | {jsonBytes,12:N0} | 100.0% |");
|
|
|
|
// Try MessagePack
|
|
try
|
|
{
|
|
var msgPackOptions = ContractlessStandardResolver.Options.WithCompression(MessagePackCompression.None);
|
|
var msgPack = MessagePackSerializer.Serialize(order, msgPackOptions);
|
|
Console.WriteLine($"| MessagePack | {msgPack.Length,12:N0} | {100.0 * msgPack.Length / jsonBytes,6:F1}% |");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"| MessagePack | FAILED: {ex.Message}");
|
|
}
|
|
}
|
|
}
|
|
}
|