|
| 1 | +using ModularityKit.Mutator.Abstractions.Changes; |
| 2 | +using ModularityKit.Mutator.Abstractions.Context; |
| 3 | +using ModularityKit.Mutator.Abstractions; |
| 4 | +using ModularityKit.Mutator.Abstractions.Engine; |
| 5 | +using ModularityKit.Mutator.Abstractions.Intent; |
| 6 | +using ModularityKit.Mutator.Abstractions.Policies; |
| 7 | +using ModularityKit.Mutator.Abstractions.Results; |
| 8 | +using ModularityKit.Mutator.Benchmarks.Engine; |
| 9 | +using ModularityKit.Mutator.Governance.Abstractions.Execution.Contracts; |
| 10 | +using ModularityKit.Mutator.Governance.Abstractions.Lifecycle.Model; |
| 11 | +using ModularityKit.Mutator.Governance.Abstractions.Requests.Factory; |
| 12 | +using ModularityKit.Mutator.Governance.Abstractions.Requests.Model; |
| 13 | +using ModularityKit.Mutator.Governance.Abstractions.Resolution.Strategies; |
| 14 | +using ModularityKit.Mutator.Governance.Runtime.Execution.Orchestration; |
| 15 | +using ModularityKit.Mutator.Governance.Runtime.Resolution.Execution; |
| 16 | +using ModularityKit.Mutator.Governance.Runtime.Storage; |
| 17 | + |
| 18 | +namespace ModularityKit.Mutator.Benchmarks.Governance.Execution.Support; |
| 19 | + |
| 20 | +/// <summary> |
| 21 | +/// Builds repeatable governed execution benchmark fixtures. |
| 22 | +/// </summary> |
| 23 | +internal static class GovernedExecutionBenchmarkSupport |
| 24 | +{ |
| 25 | + public const string StateId = "governance-benchmark:execution"; |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Creates a fresh execution fixture for a single scenario run. |
| 29 | + /// </summary> |
| 30 | + public static GovernedExecutionBenchmarkFixture CreateFixture( |
| 31 | + string requestId, |
| 32 | + string currentStateVersion, |
| 33 | + string nextStateVersion) |
| 34 | + { |
| 35 | + var engine = MutationEngineBenchmarkSupport.BuildEngine(MutationEngineOptions.Strict); |
| 36 | + var store = new InMemoryMutationRequestStore(); |
| 37 | + var resolutionManager = new MutationRequestVersionResolutionManager(store, new MutationRequestVersionResolver()); |
| 38 | + var executionManager = new GovernanceExecutionManager(store, resolutionManager, engine); |
| 39 | + var request = store.Create(CreateApprovedRequest<GovernedExecutionState, IncrementValueMutation>(requestId)) |
| 40 | + .GetAwaiter() |
| 41 | + .GetResult(); |
| 42 | + |
| 43 | + return new GovernedExecutionBenchmarkFixture( |
| 44 | + ExecutionManager: executionManager, |
| 45 | + Request: request, |
| 46 | + State: new GovernedExecutionState(StateId, 42, currentStateVersion), |
| 47 | + Mutation: new IncrementValueMutation( |
| 48 | + MutationContext.User("operator", "Operator", "Execute governed request"), |
| 49 | + nextStateVersion)); |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Creates the approved request used by governed execution benchmarks. |
| 54 | + /// </summary> |
| 55 | + private static MutationRequest CreateApprovedRequest<TState, TMutation>(string requestId) |
| 56 | + where TMutation : IMutation<TState> |
| 57 | + => MutationRequestFactory.Approved<TState, TMutation>( |
| 58 | + stateId: StateId, |
| 59 | + intent: CreateIntent(), |
| 60 | + context: MutationContext.User("requester", "Requester", "Need governed execution"), |
| 61 | + expectedStateVersion: "v10") |
| 62 | + with |
| 63 | + { |
| 64 | + RequestId = requestId |
| 65 | + }; |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Creates the intent used by governed execution scenarios. |
| 69 | + /// </summary> |
| 70 | + private static MutationIntent CreateIntent() |
| 71 | + => new() |
| 72 | + { |
| 73 | + OperationName = "ExecuteGovernedRequest", |
| 74 | + Category = "Governance", |
| 75 | + Description = "Execute a governed request through orchestration", |
| 76 | + RiskLevel = MutationRiskLevel.Low, |
| 77 | + IsReversible = true |
| 78 | + }; |
| 79 | + |
| 80 | + /// <summary> |
| 81 | + /// Minimal versioned state used by governed execution benchmarks. |
| 82 | + /// </summary> |
| 83 | + /// <param name="StateId">Stable state identifier.</param> |
| 84 | + /// <param name="Value">Benchmark counter value.</param> |
| 85 | + /// <param name="Version">Current state version.</param> |
| 86 | + internal sealed record GovernedExecutionState(string StateId, int Value, string Version) : IVersionedState; |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// Minimal mutation used to measure governed execution orchestration. |
| 90 | + /// </summary> |
| 91 | + internal sealed class IncrementValueMutation(MutationContext context, string nextVersion) |
| 92 | + : IMutation<GovernedExecutionState> |
| 93 | + { |
| 94 | + public MutationIntent Intent { get; } = new() |
| 95 | + { |
| 96 | + OperationName = "IncrementGovernedValue", |
| 97 | + Category = "Governance", |
| 98 | + Description = "Increment a governed benchmark value", |
| 99 | + RiskLevel = MutationRiskLevel.Low, |
| 100 | + IsReversible = true |
| 101 | + }; |
| 102 | + |
| 103 | + public MutationContext Context { get; } = context; |
| 104 | + |
| 105 | + public MutationResult<GovernedExecutionState> Apply(GovernedExecutionState state) |
| 106 | + { |
| 107 | + var next = state with |
| 108 | + { |
| 109 | + Value = state.Value + 1, |
| 110 | + Version = nextVersion |
| 111 | + }; |
| 112 | + |
| 113 | + return MutationResult<GovernedExecutionState>.Success( |
| 114 | + next, |
| 115 | + ChangeSet.Single(StateChange.Modified(nameof(GovernedExecutionState.Value), state.Value, next.Value))); |
| 116 | + } |
| 117 | + |
| 118 | + public ValidationResult Validate(GovernedExecutionState state) |
| 119 | + { |
| 120 | + return state.Value < 0 |
| 121 | + ? ValidationResult.WithError(nameof(GovernedExecutionState.Value), "Value must be non-negative.") |
| 122 | + : ValidationResult.Success(); |
| 123 | + } |
| 124 | + |
| 125 | + public MutationResult<GovernedExecutionState> Simulate(GovernedExecutionState state) => Apply(state); |
| 126 | + } |
| 127 | + |
| 128 | + /// <summary> |
| 129 | + /// Shared execution fixture for a benchmark scenario. |
| 130 | + /// </summary> |
| 131 | + internal sealed record GovernedExecutionBenchmarkFixture( |
| 132 | + GovernanceExecutionManager ExecutionManager, |
| 133 | + MutationRequest Request, |
| 134 | + GovernedExecutionState State, |
| 135 | + IncrementValueMutation Mutation); |
| 136 | +} |
0 commit comments