|
| 1 | +using ModularityKit.Mutator.Abstractions.Audit; |
| 2 | +using ModularityKit.Mutator.Abstractions.Changes; |
| 3 | +using ModularityKit.Mutator.Abstractions.Context; |
| 4 | +using ModularityKit.Mutator.Abstractions.Effects; |
| 5 | +using ModularityKit.Mutator.Abstractions.Engine; |
| 6 | +using ModularityKit.Mutator.Abstractions.Intent; |
| 7 | +using ModularityKit.Mutator.Abstractions.Results; |
| 8 | + |
| 9 | +namespace ModularityKit.Mutator.Benchmarks.Results.Support; |
| 10 | + |
| 11 | +/// <summary> |
| 12 | +/// Shared support types for result materialization benchmark scenarios. |
| 13 | +/// </summary> |
| 14 | +public static class ResultsBenchmarkSupport |
| 15 | +{ |
| 16 | + /// <summary> |
| 17 | + /// Gets the shared state identifier used by result benchmark cases. |
| 18 | + /// </summary> |
| 19 | + public const string StateId = "results-benchmark-state"; |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Creates a reusable mutation context for result benchmarks. |
| 23 | + /// </summary> |
| 24 | + /// <param name="operationSuffix">The suffix used to distinguish benchmark cases.</param> |
| 25 | + /// <returns>A system mutation context bound to the shared benchmark state.</returns> |
| 26 | + public static MutationContext CreateContext(string operationSuffix) |
| 27 | + { |
| 28 | + return MutationContext.System("results-benchmark") |
| 29 | + with |
| 30 | + { |
| 31 | + StateId = StateId, |
| 32 | + Mode = MutationMode.Commit, |
| 33 | + CorrelationId = $"{StateId}:{operationSuffix}" |
| 34 | + }; |
| 35 | + } |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Creates a reusable mutation intent for result benchmarks. |
| 39 | + /// </summary> |
| 40 | + /// <param name="operationName">The operation name reported by the mutation.</param> |
| 41 | + /// <param name="description">The human-readable description.</param> |
| 42 | + /// <returns>A benchmark mutation intent.</returns> |
| 43 | + public static MutationIntent CreateIntent(string operationName, string description) |
| 44 | + => new() |
| 45 | + { |
| 46 | + OperationName = operationName, |
| 47 | + Category = "Benchmark", |
| 48 | + Description = description, |
| 49 | + RiskLevel = MutationRiskLevel.Low, |
| 50 | + IsReversible = true |
| 51 | + }; |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Creates a change set with a stable revision marker and a configurable number of appended slot updates. |
| 55 | + /// </summary> |
| 56 | + /// <param name="revision">The revision number before mutation.</param> |
| 57 | + /// <param name="updates">The number of slot updates to include.</param> |
| 58 | + /// <returns>A populated change set.</returns> |
| 59 | + public static ChangeSet CreateChangeSet(int revision, int updates) |
| 60 | + { |
| 61 | + var changes = new List<StateChange>(updates + 1) |
| 62 | + { |
| 63 | + StateChange.Modified(nameof(ResultBenchmarkState.Revision), revision, revision + 1) |
| 64 | + }; |
| 65 | + |
| 66 | + for (var index = 0; index < updates; index++) |
| 67 | + { |
| 68 | + changes.Add(StateChange.Modified( |
| 69 | + $"Slots[{index}]", |
| 70 | + index, |
| 71 | + index + 1)); |
| 72 | + } |
| 73 | + |
| 74 | + return ChangeSet.FromChanges([.. changes]); |
| 75 | + } |
| 76 | + |
| 77 | + /// <summary> |
| 78 | + /// Creates a fixed list of side effects with predictable payload shape. |
| 79 | + /// </summary> |
| 80 | + /// <param name="count">The number of side effects to create.</param> |
| 81 | + /// <returns>A read-only side effect list.</returns> |
| 82 | + public static IReadOnlyList<SideEffect> CreateSideEffects(int count) |
| 83 | + { |
| 84 | + var sideEffects = new List<SideEffect>(count); |
| 85 | + |
| 86 | + for (var index = 0; index < count; index++) |
| 87 | + { |
| 88 | + sideEffects.Add(SideEffect.Create( |
| 89 | + "ResultMaterialization", |
| 90 | + $"Side effect #{index}", |
| 91 | + new SideEffectPayload(index, $"payload-{index}"), |
| 92 | + SideEffectSeverity.Info)); |
| 93 | + } |
| 94 | + |
| 95 | + return sideEffects; |
| 96 | + } |
| 97 | + |
| 98 | + /// <summary> |
| 99 | + /// Creates a reusable mutation result used as the input for output materialization benchmarks. |
| 100 | + /// </summary> |
| 101 | + /// <param name="sideEffectCount">The number of side effects to attach to the result.</param> |
| 102 | + /// <param name="changeCount">The number of slot changes to include in the result.</param> |
| 103 | + /// <returns>A mutation result prepopulated with changes and side effects.</returns> |
| 104 | + public static MutationResult<ResultBenchmarkState> CreateExecutedResult( |
| 105 | + int sideEffectCount, |
| 106 | + int changeCount) |
| 107 | + { |
| 108 | + var state = new ResultBenchmarkState(42, 0); |
| 109 | + var nextState = state with { Revision = state.Revision + 1 }; |
| 110 | + |
| 111 | + return MutationResult<ResultBenchmarkState>.Success( |
| 112 | + nextState, |
| 113 | + CreateChangeSet(state.Revision, changeCount), |
| 114 | + CreateSideEffects(sideEffectCount)); |
| 115 | + } |
| 116 | + |
| 117 | + /// <summary> |
| 118 | + /// Minimal state used by result benchmark scenarios. |
| 119 | + /// </summary> |
| 120 | + /// <param name="Revision">The revision counter advanced on each benchmark mutation.</param> |
| 121 | + /// <param name="Value">The mutable numeric value exercised by the benchmark mutation.</param> |
| 122 | + public sealed record ResultBenchmarkState(int Revision, int Value); |
| 123 | + |
| 124 | + /// <summary> |
| 125 | + /// Typed payload used to give side effects realistic materialization shape. |
| 126 | + /// </summary> |
| 127 | + /// <param name="Index">The ordinal of the side effect.</param> |
| 128 | + /// <param name="Token">A stable payload token.</param> |
| 129 | + public sealed record SideEffectPayload(int Index, string Token); |
| 130 | +} |
0 commit comments