|
| 1 | +using ModularityKit.Mutator.Abstractions; |
| 2 | +using ModularityKit.Mutator.Abstractions.Changes; |
| 3 | +using ModularityKit.Mutator.Abstractions.Context; |
| 4 | +using ModularityKit.Mutator.Abstractions.Engine; |
| 5 | +using ModularityKit.Mutator.Abstractions.Intent; |
| 6 | +using ModularityKit.Mutator.Abstractions.Results; |
| 7 | +using ModularityKit.Mutator.Benchmarks.Engine; |
| 8 | +using ModularityKit.Mutator.Governance.Abstractions.Execution.Contracts; |
| 9 | +using ModularityKit.Mutator.Governance.Abstractions.Requests.Factory; |
| 10 | +using ModularityKit.Mutator.Governance.Abstractions.Requests.Model; |
| 11 | +using ModularityKit.Mutator.Governance.Runtime.Resolution.Execution; |
| 12 | +using ModularityKit.Mutator.Governance.Runtime.Storage; |
| 13 | + |
| 14 | +namespace ModularityKit.Mutator.Benchmarks.Governance.Resolution.Support; |
| 15 | + |
| 16 | +/// <summary> |
| 17 | +/// Builds repeatable governance version resolution benchmark fixtures. |
| 18 | +/// </summary> |
| 19 | +internal static class GovernanceVersionResolutionBenchmarkSupport |
| 20 | +{ |
| 21 | + public const string StateId = "governance-benchmark:resolution"; |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Creates a fresh resolution fixture for a single scenario run. |
| 25 | + /// </summary> |
| 26 | + public static GovernanceVersionResolutionBenchmarkFixture CreateFixture( |
| 27 | + string requestId, |
| 28 | + string expectedStateVersion, |
| 29 | + string currentStateVersion) |
| 30 | + { |
| 31 | + _ = MutationEngineBenchmarkSupport.BuildEngine(MutationEngineOptions.Strict); |
| 32 | + |
| 33 | + var store = new InMemoryMutationRequestStore(); |
| 34 | + var resolver = new MutationRequestVersionResolver(); |
| 35 | + var resolutionManager = new MutationRequestVersionResolutionManager(store, resolver); |
| 36 | + var request = store.Create(CreateApprovedRequest<GovernanceVersionResolutionState, NoOpGovernanceResolutionMutation>( |
| 37 | + requestId, |
| 38 | + expectedStateVersion)) |
| 39 | + .GetAwaiter() |
| 40 | + .GetResult(); |
| 41 | + |
| 42 | + return new GovernanceVersionResolutionBenchmarkFixture( |
| 43 | + ResolutionManager: resolutionManager, |
| 44 | + Request: request, |
| 45 | + CurrentStateVersion: currentStateVersion, |
| 46 | + ResolutionContext: MutationContext.System("governance-resolution-benchmark")); |
| 47 | + } |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Creates the approved request used by governance version resolution benchmarks. |
| 51 | + /// </summary> |
| 52 | + private static MutationRequest CreateApprovedRequest<TState, TMutation>( |
| 53 | + string requestId, |
| 54 | + string expectedStateVersion) |
| 55 | + where TMutation : IMutation<TState> |
| 56 | + => MutationRequestFactory.Approved<TState, TMutation>( |
| 57 | + stateId: StateId, |
| 58 | + intent: CreateIntent(), |
| 59 | + context: MutationContext.User("requester", "Requester", "Need governed version resolution"), |
| 60 | + expectedStateVersion: expectedStateVersion) |
| 61 | + with |
| 62 | + { |
| 63 | + RequestId = requestId |
| 64 | + }; |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Creates the intent used by governance version resolution scenarios. |
| 68 | + /// </summary> |
| 69 | + private static MutationIntent CreateIntent() |
| 70 | + => new() |
| 71 | + { |
| 72 | + OperationName = "ResolveGovernedVersion", |
| 73 | + Category = "Governance", |
| 74 | + Description = "Resolve governance request version against the current state snapshot", |
| 75 | + RiskLevel = MutationRiskLevel.Low, |
| 76 | + IsReversible = true |
| 77 | + }; |
| 78 | + |
| 79 | + /// <summary> |
| 80 | + /// Minimal versioned state used by governance version resolution benchmarks. |
| 81 | + /// </summary> |
| 82 | + /// <param name="StateId">Stable state identifier.</param> |
| 83 | + /// <param name="Version">Current state version.</param> |
| 84 | + internal sealed record GovernanceVersionResolutionState(string StateId, string Version) : IVersionedState; |
| 85 | + |
| 86 | + /// <summary> |
| 87 | + /// Minimal mutation type used to anchor governed request metadata for version resolution benchmarks. |
| 88 | + /// </summary> |
| 89 | + internal sealed class NoOpGovernanceResolutionMutation : IMutation<GovernanceVersionResolutionState> |
| 90 | + { |
| 91 | + public MutationIntent Intent { get; } = new() |
| 92 | + { |
| 93 | + OperationName = "NoOpGovernanceResolution", |
| 94 | + Category = "Governance", |
| 95 | + Description = "Anchor request metadata for governed version resolution benchmarks", |
| 96 | + RiskLevel = MutationRiskLevel.Low, |
| 97 | + IsReversible = true |
| 98 | + }; |
| 99 | + |
| 100 | + public MutationContext Context { get; } = MutationContext.System("governance-resolution-benchmark"); |
| 101 | + |
| 102 | + public MutationResult<GovernanceVersionResolutionState> Apply(GovernanceVersionResolutionState state) |
| 103 | + => MutationResult<GovernanceVersionResolutionState>.Success( |
| 104 | + state, |
| 105 | + ChangeSet.Empty); |
| 106 | + |
| 107 | + public ValidationResult Validate(GovernanceVersionResolutionState state) => ValidationResult.Success(); |
| 108 | + |
| 109 | + public MutationResult<GovernanceVersionResolutionState> Simulate(GovernanceVersionResolutionState state) => Apply(state); |
| 110 | + } |
| 111 | + |
| 112 | + /// <summary> |
| 113 | + /// Shared fixture for a governance version resolution scenario. |
| 114 | + /// </summary> |
| 115 | + internal sealed record GovernanceVersionResolutionBenchmarkFixture( |
| 116 | + MutationRequestVersionResolutionManager ResolutionManager, |
| 117 | + MutationRequest Request, |
| 118 | + string CurrentStateVersion, |
| 119 | + MutationContext ResolutionContext); |
| 120 | +} |
0 commit comments