#if !AYCODE_NATIVEAOT using AyCode.Core.Tests.TestModels; using MessagePack; using MessagePack.Resolvers; using System.Runtime.CompilerServices; namespace AyCode.Core.Serializers.Console.Benchmarks; /// /// MessagePack benchmark, Byte[] I/O mode. Excluded from NativeAOT build because v3's StandardResolver /// falls back to DynamicGenericResolver for closed-generic types (List<TestOrderItem> et al.), /// which uses Activator.CreateInstance on formatter types the AOT trimmer drops → /// MissingMethodException at runtime. Available for regular JIT runs (dotnet run) only. /// internal sealed class MessagePackBenchmark : ISerializerBenchmark { private readonly TestOrder _order; private readonly MessagePackSerializerOptions _options; private readonly byte[] _serialized; public string Engine => Configuration.EngineMessagePack; public string IoMode => Configuration.IoByteArray; public string DispatchMode => Configuration.ModeSGen; // MessagePack uses [MessagePackObject] source-generated formatters (StandardResolver) public string OptionsPreset { get; } public int SerializedSize => _serialized.Length; public long SetupSerializeAllocBytes => 0; public long SetupDeserializeAllocBytes => 0; public string OptionsDescription { get; } public MessagePackBenchmark(TestOrder order, string optionsPreset) { _order = order; OptionsPreset = optionsPreset; //_options = ContractlessStandardResolver.Options.WithCompression(MessagePackCompression.None); //_options = ContractlessStandardResolver.Options.WithCompression(MessagePackCompression.Lz4Block); _options = MessagePackSerializerOptions.Standard.WithCompression(MessagePackCompression.None); var isContractless = _options.Resolver is ContractlessStandardResolver; OptionsDescription = $"Mode={( isContractless ? "Contractless" : "ContractBased")}, Compression={_options.Compression}"; _serialized = MessagePackSerializer.Serialize(order, _options); } [MethodImpl(MethodImplOptions.NoInlining)] public void Serialize() => MessagePackSerializer.Serialize(_order, _options); [MethodImpl(MethodImplOptions.NoInlining)] public void Deserialize() => MessagePackSerializer.Deserialize(_serialized, _options); public bool VerifyRoundTrip() { var bytes = MessagePackSerializer.Serialize(_order, _options); var roundTripped = MessagePackSerializer.Deserialize(bytes, _options); return BenchmarkLoop.DeepEqualsViaJson(_order, roundTripped); } } #endif