From d157acae683b7a6e6e3188c969afe9d8ce6f3003 Mon Sep 17 00:00:00 2001 From: Tom Longhurst <30480171+thomhurst@users.noreply.github.com> Date: Thu, 16 Jul 2026 15:02:42 +0100 Subject: [PATCH] fix(benchmarks): make SetupTeardownTests JSON tests Native AOT compatible JsonOperationTest and AsyncJsonOperationTest serialized an anonymous type via reflection-based System.Text.Json, which is disabled under PublishAot (JsonSerializerIsReflectionEnabledByDefault=false). Both tests threw at runtime, failing the run, so BenchmarkDotNet reported NA for TUnit AOT in the Setup/teardown lifecycle scenario and the README showed a dash. Replace the anonymous type with a concrete record and a source-generated JsonSerializerContext, which works under Native AOT and all four frameworks. Verified by running the suite with the reflection feature switch disabled: 22/22 pass. --- .../UnifiedTests/SetupTeardownTests.cs | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/tools/speed-comparison/UnifiedTests/SetupTeardownTests.cs b/tools/speed-comparison/UnifiedTests/SetupTeardownTests.cs index 613d9a842a5..e28487101fd 100644 --- a/tools/speed-comparison/UnifiedTests/SetupTeardownTests.cs +++ b/tools/speed-comparison/UnifiedTests/SetupTeardownTests.cs @@ -353,13 +353,8 @@ public async Task AsyncDictionaryOperationTest() public void JsonOperationTest() { // Simulate JSON serialization - var data = new - { - Id = 123, - Name = "Test Data", - Values = Enumerable.Range(0, 50).ToArray() - }; - var json = System.Text.Json.JsonSerializer.Serialize(data); + var data = new JsonPayload(123, "Test Data", Enumerable.Range(0, 50).ToArray()); + var json = System.Text.Json.JsonSerializer.Serialize(data, BenchmarkJsonContext.Default.JsonPayload); _logBuilder.AppendLine($"JSON length: {json.Length}"); } @@ -368,13 +363,15 @@ public async Task AsyncJsonOperationTest() { // Simulate async JSON operations await Task.Delay(10); - var data = new - { - Id = 123, - Name = "Test Data", - Values = Enumerable.Range(0, 50).ToArray() - }; - var json = System.Text.Json.JsonSerializer.Serialize(data); + var data = new JsonPayload(123, "Test Data", Enumerable.Range(0, 50).ToArray()); + var json = System.Text.Json.JsonSerializer.Serialize(data, BenchmarkJsonContext.Default.JsonPayload); _logBuilder.AppendLine($"Async JSON length: {json.Length}"); } } + +// Concrete DTO + source-generated serializer context so JSON tests work under Native AOT, +// where reflection-based System.Text.Json serialization is disabled. +public sealed record JsonPayload(int Id, string Name, int[] Values); + +[System.Text.Json.Serialization.JsonSerializable(typeof(JsonPayload))] +internal partial class BenchmarkJsonContext : System.Text.Json.Serialization.JsonSerializerContext;