|
| 1 | +using Microsoft.Extensions.DependencyInjection; |
| 2 | +using ModularityKit.Mutator.Abstractions; |
| 3 | +using ModularityKit.Mutator.Abstractions.Changes; |
| 4 | +using ModularityKit.Mutator.Abstractions.Context; |
| 5 | +using ModularityKit.Mutator.Abstractions.Engine; |
| 6 | +using ModularityKit.Mutator.Abstractions.Intent; |
| 7 | +using ModularityKit.Mutator.Abstractions.Policies; |
| 8 | +using ModularityKit.Mutator.Abstractions.Results; |
| 9 | +using ModularityKit.Mutator.Runtime; |
| 10 | + |
| 11 | +namespace ModularityKit.Mutator.Benchmarks.Policy; |
| 12 | + |
| 13 | +/// <summary> |
| 14 | +/// Shared support types for policy evaluation benchmark scenarios. |
| 15 | +/// </summary> |
| 16 | +internal static class PolicyBenchmarkSupport |
| 17 | +{ |
| 18 | + public const string StateId = "policy-benchmark-state"; |
| 19 | + |
| 20 | + public static IMutationEngine BuildEngine(Action<IMutationEngine>? configure = null) |
| 21 | + { |
| 22 | + var services = new ServiceCollection(); |
| 23 | + services.AddMutators(MutationEngineOptions.Performance); |
| 24 | + |
| 25 | + var engine = services |
| 26 | + .BuildServiceProvider() |
| 27 | + .GetRequiredService<IMutationEngine>(); |
| 28 | + |
| 29 | + configure?.Invoke(engine); |
| 30 | + return engine; |
| 31 | + } |
| 32 | + |
| 33 | + public static MinimalPolicyMutation CreateMutation() |
| 34 | + { |
| 35 | + var context = MutationContext.System("policy-benchmark") |
| 36 | + with |
| 37 | + { |
| 38 | + StateId = StateId, |
| 39 | + Mode = MutationMode.Commit, |
| 40 | + CorrelationId = $"{StateId}:policy-pass" |
| 41 | + }; |
| 42 | + |
| 43 | + return new MinimalPolicyMutation(context); |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Minimal state used by policy evaluation benchmarks. |
| 48 | + /// </summary> |
| 49 | + /// <param name="Name">The logical state name.</param> |
| 50 | + /// <param name="Counter">The mutable numeric field exercised by the benchmark mutation.</param> |
| 51 | + public sealed record PolicyBenchmarkState(string Name, int Counter); |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Minimal mutation used to isolate policy pipeline overhead from unrelated runtime work. |
| 55 | + /// </summary> |
| 56 | + public sealed class MinimalPolicyMutation(MutationContext context) : IMutation<PolicyBenchmarkState> |
| 57 | + { |
| 58 | + public MutationIntent Intent { get; } = new() |
| 59 | + { |
| 60 | + OperationName = "MinimalPolicyMutation", |
| 61 | + Category = "Benchmark", |
| 62 | + Description = "Minimal mutation used to isolate policy evaluation overhead.", |
| 63 | + RiskLevel = MutationRiskLevel.Low, |
| 64 | + IsReversible = true |
| 65 | + }; |
| 66 | + |
| 67 | + public MutationContext Context { get; } = context; |
| 68 | + |
| 69 | + public MutationResult<PolicyBenchmarkState> Apply(PolicyBenchmarkState state) |
| 70 | + { |
| 71 | + var nextState = state with { Counter = state.Counter + 1 }; |
| 72 | + |
| 73 | + return MutationResult<PolicyBenchmarkState>.Success( |
| 74 | + nextState, |
| 75 | + ChangeSet.Single(StateChange.Modified(nameof(PolicyBenchmarkState.Counter), state.Counter, nextState.Counter))); |
| 76 | + } |
| 77 | + |
| 78 | + public ValidationResult Validate(PolicyBenchmarkState state) => ValidationResult.Success(); |
| 79 | + |
| 80 | + public MutationResult<PolicyBenchmarkState> Simulate(PolicyBenchmarkState state) => Apply(state); |
| 81 | + } |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// Synchronous allow policy used in benchmark scenarios. |
| 85 | + /// </summary> |
| 86 | + public sealed class SyncAllowBenchmarkPolicy : IMutationPolicy<PolicyBenchmarkState> |
| 87 | + { |
| 88 | + public SyncAllowBenchmarkPolicy(int priority) => Priority = priority; |
| 89 | + |
| 90 | + public string Name => $"{nameof(SyncAllowBenchmarkPolicy)}_{Priority}"; |
| 91 | + |
| 92 | + public int Priority { get; } |
| 93 | + |
| 94 | + public string? Description => "Synchronous allow policy for benchmark measurements."; |
| 95 | + |
| 96 | + public PolicyDecision Evaluate(IMutation<PolicyBenchmarkState> mutation, PolicyBenchmarkState state) |
| 97 | + => PolicyDecision.Allow(Name, "Synchronous benchmark policy allowed the mutation."); |
| 98 | + } |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Asynchronous allow policy used in benchmark scenarios. |
| 102 | + /// </summary> |
| 103 | + public sealed class AsyncAllowBenchmarkPolicy : IMutationPolicy<PolicyBenchmarkState> |
| 104 | + { |
| 105 | + public AsyncAllowBenchmarkPolicy(int priority) => Priority = priority; |
| 106 | + |
| 107 | + public string Name => $"{nameof(AsyncAllowBenchmarkPolicy)}_{Priority}"; |
| 108 | + |
| 109 | + public int Priority { get; } |
| 110 | + |
| 111 | + public string? Description => "Asynchronous allow policy for benchmark measurements."; |
| 112 | + |
| 113 | + public async Task<PolicyDecision> EvaluateAsync( |
| 114 | + IMutation<PolicyBenchmarkState> mutation, |
| 115 | + PolicyBenchmarkState state, |
| 116 | + CancellationToken cancellationToken = default) |
| 117 | + { |
| 118 | + await Task.CompletedTask; |
| 119 | + cancellationToken.ThrowIfCancellationRequested(); |
| 120 | + |
| 121 | + return PolicyDecision.Allow(Name, "Asynchronous benchmark policy allowed the mutation."); |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments