Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using BenchmarkDotNet.Attributes;
using ModularityKit.Mutator.Governance.Abstractions.Execution.Model;
using ModularityKit.Mutator.Governance.Abstractions.Resolution.Model;
using ModularityKit.Mutator.Governance.Abstractions.Resolution.Strategies;
using ModularityKit.Mutator.Benchmarks.Governance.Execution.Support;

namespace ModularityKit.Mutator.Benchmarks.Governance.Execution;

/// <summary>
/// Benchmarks governed execution orchestration paths in the governance runtime.
/// </summary>
[BenchmarkCategory("Governance")]
[MemoryDiagnoser]
[InProcess]
public class GovernedExecutionOrchestrationBenchmarks
{
private const string ApprovedRequestId = "governance-execution-approved";
private const string StaleRequestId = "governance-execution-stale";
private const string RevalidateRequestId = "governance-execution-revalidate";

private GovernedExecutionBenchmarkSupport.GovernedExecutionBenchmarkFixture _approvedFixture = null!;
private GovernedExecutionBenchmarkSupport.GovernedExecutionBenchmarkFixture _staleFixture = null!;
private GovernedExecutionBenchmarkSupport.GovernedExecutionBenchmarkFixture _revalidateFixture = null!;

/// <summary>
/// Prepares fresh fixtures for each benchmark iteration.
/// </summary>
[IterationSetup]
public void Setup()
{
_approvedFixture = GovernedExecutionBenchmarkSupport.CreateFixture(
ApprovedRequestId,
currentStateVersion: "v10",
nextStateVersion: "v11");

_staleFixture = GovernedExecutionBenchmarkSupport.CreateFixture(
StaleRequestId,
currentStateVersion: "v15",
nextStateVersion: "v16");

_revalidateFixture = GovernedExecutionBenchmarkSupport.CreateFixture(
RevalidateRequestId,
currentStateVersion: "v15",
nextStateVersion: "v16");
}

/// <summary>
/// Measures an approved request executing through governed orchestration with matching state version.
/// </summary>
[Benchmark(Baseline = true)]
public async Task ExecuteApproved_MatchingVersion()
{
var result = await _approvedFixture.ExecutionManager.ExecuteApproved(
_approvedFixture.Request.RequestId,
_approvedFixture.Mutation,
_approvedFixture.State,
governanceContext: _approvedFixture.Mutation.Context,
strategy: VersionedRequestResolutionStrategy.RejectStale).ConfigureAwait(false);

GC.KeepAlive(result);
}

/// <summary>
/// Measures an approved request being rejected as stale before the core engine executes.
/// </summary>
[Benchmark]
public async Task ExecuteApproved_RejectStale()
{
var result = await _staleFixture.ExecutionManager.ExecuteApproved(
_staleFixture.Request.RequestId,
_staleFixture.Mutation,
_staleFixture.State,
governanceContext: _staleFixture.Mutation.Context,
strategy: VersionedRequestResolutionStrategy.RejectStale).ConfigureAwait(false);

GC.KeepAlive(result);
}

/// <summary>
/// Measures an approved request being revalidated against the latest state and then executed.
/// </summary>
[Benchmark]
public async Task ExecuteApproved_RevalidateAndExecute()
{
var result = await _revalidateFixture.ExecutionManager.ExecuteApproved(
_revalidateFixture.Request.RequestId,
_revalidateFixture.Mutation,
_revalidateFixture.State,
governanceContext: _revalidateFixture.Mutation.Context,
strategy: VersionedRequestResolutionStrategy.RevalidateOnLatestState).ConfigureAwait(false);

GC.KeepAlive(result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
using ModularityKit.Mutator.Abstractions.Changes;
using ModularityKit.Mutator.Abstractions.Context;
using ModularityKit.Mutator.Abstractions;
using ModularityKit.Mutator.Abstractions.Engine;
using ModularityKit.Mutator.Abstractions.Intent;
using ModularityKit.Mutator.Abstractions.Policies;
using ModularityKit.Mutator.Abstractions.Results;
using ModularityKit.Mutator.Benchmarks.Engine;
using ModularityKit.Mutator.Governance.Abstractions.Execution.Contracts;
using ModularityKit.Mutator.Governance.Abstractions.Lifecycle.Model;
using ModularityKit.Mutator.Governance.Abstractions.Requests.Factory;
using ModularityKit.Mutator.Governance.Abstractions.Requests.Model;
using ModularityKit.Mutator.Governance.Abstractions.Resolution.Strategies;
using ModularityKit.Mutator.Governance.Runtime.Execution.Orchestration;
using ModularityKit.Mutator.Governance.Runtime.Resolution.Execution;
using ModularityKit.Mutator.Governance.Runtime.Storage;

namespace ModularityKit.Mutator.Benchmarks.Governance.Execution.Support;

/// <summary>
/// Builds repeatable governed execution benchmark fixtures.
/// </summary>
internal static class GovernedExecutionBenchmarkSupport
{
public const string StateId = "governance-benchmark:execution";

/// <summary>
/// Creates a fresh execution fixture for a single scenario run.
/// </summary>
public static GovernedExecutionBenchmarkFixture CreateFixture(
string requestId,
string currentStateVersion,
string nextStateVersion)
{
var engine = MutationEngineBenchmarkSupport.BuildEngine(MutationEngineOptions.Strict);
var store = new InMemoryMutationRequestStore();
var resolutionManager = new MutationRequestVersionResolutionManager(store, new MutationRequestVersionResolver());
var executionManager = new GovernanceExecutionManager(store, resolutionManager, engine);
var request = store.Create(CreateApprovedRequest<GovernedExecutionState, IncrementValueMutation>(requestId))
.GetAwaiter()
.GetResult();

return new GovernedExecutionBenchmarkFixture(
ExecutionManager: executionManager,
Request: request,
State: new GovernedExecutionState(StateId, 42, currentStateVersion),
Mutation: new IncrementValueMutation(
MutationContext.User("operator", "Operator", "Execute governed request"),
nextStateVersion));
}

/// <summary>
/// Creates the approved request used by governed execution benchmarks.
/// </summary>
private static MutationRequest CreateApprovedRequest<TState, TMutation>(string requestId)
where TMutation : IMutation<TState>
=> MutationRequestFactory.Approved<TState, TMutation>(
stateId: StateId,
intent: CreateIntent(),
context: MutationContext.User("requester", "Requester", "Need governed execution"),
expectedStateVersion: "v10")
with
{
RequestId = requestId
};

/// <summary>
/// Creates the intent used by governed execution scenarios.
/// </summary>
private static MutationIntent CreateIntent()
=> new()
{
OperationName = "ExecuteGovernedRequest",
Category = "Governance",
Description = "Execute a governed request through orchestration",
RiskLevel = MutationRiskLevel.Low,
IsReversible = true
};

/// <summary>
/// Minimal versioned state used by governed execution benchmarks.
/// </summary>
/// <param name="StateId">Stable state identifier.</param>
/// <param name="Value">Benchmark counter value.</param>
/// <param name="Version">Current state version.</param>
internal sealed record GovernedExecutionState(string StateId, int Value, string Version) : IVersionedState;

/// <summary>
/// Minimal mutation used to measure governed execution orchestration.
/// </summary>
internal sealed class IncrementValueMutation(MutationContext context, string nextVersion)
: IMutation<GovernedExecutionState>
{
public MutationIntent Intent { get; } = new()
{
OperationName = "IncrementGovernedValue",
Category = "Governance",
Description = "Increment a governed benchmark value",
RiskLevel = MutationRiskLevel.Low,
IsReversible = true
};

public MutationContext Context { get; } = context;

public MutationResult<GovernedExecutionState> Apply(GovernedExecutionState state)
{
var next = state with
{
Value = state.Value + 1,
Version = nextVersion
};

return MutationResult<GovernedExecutionState>.Success(
next,
ChangeSet.Single(StateChange.Modified(nameof(GovernedExecutionState.Value), state.Value, next.Value)));
}

public ValidationResult Validate(GovernedExecutionState state)
{
return state.Value < 0
? ValidationResult.WithError(nameof(GovernedExecutionState.Value), "Value must be non-negative.")
: ValidationResult.Success();
}

public MutationResult<GovernedExecutionState> Simulate(GovernedExecutionState state) => Apply(state);
}

/// <summary>
/// Shared execution fixture for a benchmark scenario.
/// </summary>
internal sealed record GovernedExecutionBenchmarkFixture(
GovernanceExecutionManager ExecutionManager,
MutationRequest Request,
GovernedExecutionState State,
IncrementValueMutation Mutation);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using BenchmarkDotNet.Attributes;
using ModularityKit.Mutator.Governance.Abstractions.Resolution.Model;
using ModularityKit.Mutator.Governance.Abstractions.Resolution.Strategies;
using ModularityKit.Mutator.Benchmarks.Governance.Resolution.Support;

namespace ModularityKit.Mutator.Benchmarks.Governance.Resolution;

/// <summary>
/// Benchmarks governance version resolution paths in the governance runtime.
/// </summary>
[BenchmarkCategory("Governance")]
[MemoryDiagnoser]
[InProcess]
public class GovernanceVersionResolutionBenchmarks
{
private const string MatchingRequestId = "governance-resolution-matching";
private const string StaleRequestId = "governance-resolution-stale";
private const string RevalidateRequestId = "governance-resolution-revalidate";

private GovernanceVersionResolutionBenchmarkSupport.GovernanceVersionResolutionBenchmarkFixture _matchingFixture = null!;
private GovernanceVersionResolutionBenchmarkSupport.GovernanceVersionResolutionBenchmarkFixture _staleFixture = null!;
private GovernanceVersionResolutionBenchmarkSupport.GovernanceVersionResolutionBenchmarkFixture _revalidateFixture = null!;

/// <summary>
/// Prepares fresh fixtures for each benchmark iteration.
/// </summary>
[IterationSetup]
public void Setup()
{
_matchingFixture = GovernanceVersionResolutionBenchmarkSupport.CreateFixture(
MatchingRequestId,
expectedStateVersion: "v10",
currentStateVersion: "v10");

_staleFixture = GovernanceVersionResolutionBenchmarkSupport.CreateFixture(
StaleRequestId,
expectedStateVersion: "v10",
currentStateVersion: "v15");

_revalidateFixture = GovernanceVersionResolutionBenchmarkSupport.CreateFixture(
RevalidateRequestId,
expectedStateVersion: "v10",
currentStateVersion: "v15");
}

/// <summary>
/// Measures version comparison when approved request matches the current state version.
/// </summary>
[Benchmark(Baseline = true)]
public async Task ResolveApproved_MatchingVersion()
{
var result = await _matchingFixture.ResolutionManager.ResolveAndStore(
_matchingFixture.Request.RequestId,
currentStateVersion: _matchingFixture.CurrentStateVersion,
resolutionContext: _matchingFixture.ResolutionContext,
strategy: VersionedRequestResolutionStrategy.RejectStale).ConfigureAwait(false);

GC.KeepAlive(result);
}

/// <summary>
/// Measures stale request detection and classification during version resolution.
/// </summary>
[Benchmark]
public async Task ResolveApproved_RejectStale()
{
var result = await _staleFixture.ResolutionManager.ResolveAndStore(
_staleFixture.Request.RequestId,
currentStateVersion: _staleFixture.CurrentStateVersion,
resolutionContext: _staleFixture.ResolutionContext,
strategy: VersionedRequestResolutionStrategy.RejectStale).ConfigureAwait(false);

GC.KeepAlive(result);
}

/// <summary>
/// Measures revalidation-driven request resolution against the latest state version.
/// </summary>
[Benchmark]
public async Task ResolveApproved_Revalidate()
{
var result = await _revalidateFixture.ResolutionManager.ResolveAndStore(
_revalidateFixture.Request.RequestId,
currentStateVersion: _revalidateFixture.CurrentStateVersion,
resolutionContext: _revalidateFixture.ResolutionContext,
strategy: VersionedRequestResolutionStrategy.RevalidateOnLatestState).ConfigureAwait(false);

GC.KeepAlive(result);
}
}
Loading
Loading