|
| 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.Engine; |
| 12 | + |
| 13 | +/// <summary> |
| 14 | +/// Shared support types for core engine benchmark scenarios. |
| 15 | +/// </summary> |
| 16 | +internal static class MutationEngineBenchmarkSupport |
| 17 | +{ |
| 18 | + public const string CounterStateId = "benchmark-counter"; |
| 19 | + |
| 20 | + public static IMutationEngine BuildEngine( |
| 21 | + MutationEngineOptions options, |
| 22 | + Action<IMutationEngine>? configure = null) |
| 23 | + { |
| 24 | + var services = new ServiceCollection(); |
| 25 | + services.AddMutators(options); |
| 26 | + |
| 27 | + var engine = services |
| 28 | + .BuildServiceProvider() |
| 29 | + .GetRequiredService<IMutationEngine>(); |
| 30 | + |
| 31 | + configure?.Invoke(engine); |
| 32 | + return engine; |
| 33 | + } |
| 34 | + |
| 35 | + public static IncrementCounterMutation CreateCounterMutation(MutationMode mode, string operationSuffix) |
| 36 | + { |
| 37 | + var context = MutationContext.System("benchmark") |
| 38 | + with |
| 39 | + { |
| 40 | + StateId = CounterStateId, |
| 41 | + Mode = mode, |
| 42 | + CorrelationId = $"{CounterStateId}:{operationSuffix}" |
| 43 | + }; |
| 44 | + |
| 45 | + return new IncrementCounterMutation(context); |
| 46 | + } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Minimal counter state used by engine benchmark scenarios. |
| 50 | + /// </summary> |
| 51 | + /// <param name="Value">The current counter value.</param> |
| 52 | + public sealed record CounterState(int Value); |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Minimal counter mutation shared by core engine benchmark scenarios. |
| 56 | + /// </summary> |
| 57 | + public sealed class IncrementCounterMutation(MutationContext context) : IMutation<CounterState> |
| 58 | + { |
| 59 | + public MutationIntent Intent { get; } = new() |
| 60 | + { |
| 61 | + OperationName = "IncrementCounter", |
| 62 | + Category = "Benchmark", |
| 63 | + Description = "Increment the benchmark counter by one", |
| 64 | + RiskLevel = MutationRiskLevel.Low, |
| 65 | + IsReversible = true |
| 66 | + }; |
| 67 | + |
| 68 | + public MutationContext Context { get; } = context; |
| 69 | + |
| 70 | + public MutationResult<CounterState> Apply(CounterState state) |
| 71 | + { |
| 72 | + var next = state with { Value = state.Value + 1 }; |
| 73 | + |
| 74 | + return MutationResult<CounterState>.Success( |
| 75 | + next, |
| 76 | + ChangeSet.Single(StateChange.Modified(nameof(CounterState.Value), state.Value, next.Value))); |
| 77 | + } |
| 78 | + |
| 79 | + public ValidationResult Validate(CounterState state) |
| 80 | + { |
| 81 | + var result = ValidationResult.Success(); |
| 82 | + |
| 83 | + if (state.Value < 0) |
| 84 | + result.AddError(nameof(CounterState.Value), "Counter value must be non-negative."); |
| 85 | + |
| 86 | + return result; |
| 87 | + } |
| 88 | + |
| 89 | + public MutationResult<CounterState> Simulate(CounterState state) |
| 90 | + { |
| 91 | + var next = state with { Value = state.Value + 1 }; |
| 92 | + |
| 93 | + return MutationResult<CounterState>.Success( |
| 94 | + next, |
| 95 | + ChangeSet.Single(StateChange.Modified(nameof(CounterState.Value), state.Value, next.Value))); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /// <summary> |
| 100 | + /// Trivial allow policy used to measure policy-aware engine paths. |
| 101 | + /// </summary> |
| 102 | + public sealed class AllowAllCounterPolicy : IMutationPolicy<CounterState> |
| 103 | + { |
| 104 | + public string Name => nameof(AllowAllCounterPolicy); |
| 105 | + |
| 106 | + public int Priority => 0; |
| 107 | + |
| 108 | + public string? Description => "Always allows the benchmark counter mutation."; |
| 109 | + |
| 110 | + public PolicyDecision Evaluate(IMutation<CounterState> mutation, CounterState state) |
| 111 | + => PolicyDecision.Allow(Name, "Benchmark policy allows all mutations."); |
| 112 | + } |
| 113 | +} |
0 commit comments