diff --git a/samples/ZeroAlloc.Mediator.AotSmoke/Authorization/AuthorizedScenario.cs b/samples/ZeroAlloc.Mediator.AotSmoke/Authorization/AuthorizedScenario.cs index 0a3ee22..7a259c7 100644 --- a/samples/ZeroAlloc.Mediator.AotSmoke/Authorization/AuthorizedScenario.cs +++ b/samples/ZeroAlloc.Mediator.AotSmoke/Authorization/AuthorizedScenario.cs @@ -7,7 +7,7 @@ using ZeroAlloc.Authorization; using ZeroAlloc.Authorization.Generated; using ZeroAlloc.Mediator; -using ZeroAlloc.Mediator.AotSmoke.Internal; +using ZeroAlloc.TestHelpers; using ZeroAlloc.Mediator.Authorization; using ZeroAlloc.Results; diff --git a/samples/ZeroAlloc.Mediator.AotSmoke/Internal/AllocationGate.cs b/samples/ZeroAlloc.Mediator.AotSmoke/Internal/AllocationGate.cs deleted file mode 100644 index 0af7840..0000000 --- a/samples/ZeroAlloc.Mediator.AotSmoke/Internal/AllocationGate.cs +++ /dev/null @@ -1,73 +0,0 @@ -namespace ZeroAlloc.Mediator.AotSmoke.Internal; - -internal static class AllocationGate -{ - public static void AssertBudget(int budgetBytes, int iterations, Action action, string label) - { - ArgumentNullException.ThrowIfNull(action); - if (iterations < 1) throw new ArgumentOutOfRangeException(nameof(iterations)); - - // Warmup — JIT-compile, populate type-handle caches, allocate one-time fixtures. - action(); - action(); - - // Flush warmup garbage so it can't leak into the measurement. - GC.Collect(); - GC.WaitForPendingFinalizers(); - GC.Collect(); - - var before = GC.GetAllocatedBytesForCurrentThread(); - for (int i = 0; i < iterations; i++) action(); - var allocated = GC.GetAllocatedBytesForCurrentThread() - before; - - var perCall = allocated / iterations; - var totalBudget = (long)budgetBytes * iterations; - if (allocated > totalBudget) - { - throw new InvalidOperationException( - $"AllocationGate: {label} allocated {allocated} B total over {iterations} iterations " + - $"(~{perCall} B/call avg), budget is {budgetBytes} B/call ({totalBudget} B total). " + - "Use BenchmarkDotNet [MemoryDiagnoser] locally to find the culprit."); - } - } - - public static void AssertBudgetValueTask(int budgetBytes, int iterations, Func> action, string label) - { - ArgumentNullException.ThrowIfNull(action); - if (iterations < 1) throw new ArgumentOutOfRangeException(nameof(iterations)); - - static T Drain(ValueTask t) - { - if (!t.IsCompletedSuccessfully) - { - throw new InvalidOperationException( - "AllocationGate: sync-completion-required — the supplied ValueTask did not " + - "complete synchronously. Awaiter machinery would pollute the measurement; " + - "the API under test must return an already-completed ValueTask."); - } - return t.Result; - } - - // Warmup. - Drain(action()); - Drain(action()); - - GC.Collect(); - GC.WaitForPendingFinalizers(); - GC.Collect(); - - var before = GC.GetAllocatedBytesForCurrentThread(); - for (int i = 0; i < iterations; i++) Drain(action()); - var allocated = GC.GetAllocatedBytesForCurrentThread() - before; - - var perCall = allocated / iterations; - var totalBudget = (long)budgetBytes * iterations; - if (allocated > totalBudget) - { - throw new InvalidOperationException( - $"AllocationGate: {label} allocated {allocated} B total over {iterations} iterations " + - $"(~{perCall} B/call avg), budget is {budgetBytes} B/call ({totalBudget} B total). " + - "Use BenchmarkDotNet [MemoryDiagnoser] locally to find the culprit."); - } - } -} diff --git a/samples/ZeroAlloc.Mediator.AotSmoke/ZeroAlloc.Mediator.AotSmoke.csproj b/samples/ZeroAlloc.Mediator.AotSmoke/ZeroAlloc.Mediator.AotSmoke.csproj index 036d703..b3d0464 100644 --- a/samples/ZeroAlloc.Mediator.AotSmoke/ZeroAlloc.Mediator.AotSmoke.csproj +++ b/samples/ZeroAlloc.Mediator.AotSmoke/ZeroAlloc.Mediator.AotSmoke.csproj @@ -24,5 +24,9 @@ AuthorizerFor dispatchers for the AOT scenario's [Policy]/[RequirePolicy] types. --> + + all + contentfiles;build + diff --git a/tests/ZeroAlloc.Mediator.Authorization.Tests/AllocationBudgetTests.cs b/tests/ZeroAlloc.Mediator.Authorization.Tests/AllocationBudgetTests.cs index 45768b8..c6b080d 100644 --- a/tests/ZeroAlloc.Mediator.Authorization.Tests/AllocationBudgetTests.cs +++ b/tests/ZeroAlloc.Mediator.Authorization.Tests/AllocationBudgetTests.cs @@ -4,7 +4,7 @@ using Microsoft.Extensions.DependencyInjection; using ZeroAlloc.Authorization; using ZeroAlloc.Authorization.Generated; -using ZeroAlloc.Mediator.AotSmoke.Internal; +using ZeroAlloc.TestHelpers; using ZeroAlloc.Mediator.Authorization; using ZeroAlloc.Results; diff --git a/tests/ZeroAlloc.Mediator.Authorization.Tests/ZeroAlloc.Mediator.Authorization.Tests.csproj b/tests/ZeroAlloc.Mediator.Authorization.Tests/ZeroAlloc.Mediator.Authorization.Tests.csproj index 9b37f71..0a1c679 100644 --- a/tests/ZeroAlloc.Mediator.Authorization.Tests/ZeroAlloc.Mediator.Authorization.Tests.csproj +++ b/tests/ZeroAlloc.Mediator.Authorization.Tests/ZeroAlloc.Mediator.Authorization.Tests.csproj @@ -37,8 +37,9 @@ [Policy]/[RequirePolicy]-decorated type in this compilation. A direct (non-transitive) package reference is required so the bundled analyzer DLL flows into this project. --> - - - + + all + contentfiles;build +