fix(benchmarks): make SetupTeardownTests JSON tests Native AOT compatible#6434
Conversation
…ible 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.
Not up to standards ⛔🔴 Issues
|
| Category | Results |
|---|---|
| CodeStyle | 1 minor |
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
Code reviewNo issues found. Checked for bugs and CLAUDE.md compliance. Summary: This PR replaces an anonymous-type JSON payload in |
Problem
The README benchmark table shows
—for TUnit (AOT) on the Setup/teardown lifecycle row. The AOT benchmark run forSetupTeardownTestscrashes:JsonOperationTest/AsyncJsonOperationTestserialize an anonymous type withSystem.Text.Json.JsonSerializer.Serialize(data).PublishAot=truesetsJsonSerializerIsReflectionEnabledByDefault=false, so reflection-based STJ throwsInvalidOperationExceptionat runtime.ExecuteBufferedAsyncthrows inRuntimeBenchmarks.TUnit_AOT→ BenchmarkDotNet recordsNA→ README renders—.This is a benchmark-suite bug, not a TUnit hooks bug —
[Before(Test)]/[After(Test)]work fine under AOT.Fix
Replace the anonymous type with a concrete
JsonPayloadrecord serialized via a source-generatedJsonSerializerContext. Works under Native AOT and compiles for all four framework variants (shared file).Verification
No MSVC linker on this machine, so full AOT link wasn''t possible locally. Instead, reproduced the exact AOT failure mode by building the TUnit variant with
-p:JsonSerializerIsReflectionEnabledByDefault=false(the same feature switchPublishAotflips) and running--treenode-filter "/*/*/SetupTeardownTests/*":"System.Text.Json.JsonSerializer.IsReflectionEnabledByDefault": falsein runtimeconfig.jsonAlso compile-checked
XUNIT3,NUNIT,MSTESTvariants — all build clean.After merge, the next weekly Speed Comparison run should fill in the missing AOT cell.