|
| 1 | +using BenchmarkDotNet.Attributes; |
| 2 | +using Microsoft.Extensions.DependencyInjection; |
| 3 | +using ModularityKit.Mutator.Abstractions.Audit; |
| 4 | +using ModularityKit.Mutator.Abstractions.Engine; |
| 5 | +using ModularityKit.Mutator.Abstractions.History; |
| 6 | +using ModularityKit.Mutator.Benchmarks.Concurrency.Support; |
| 7 | +using ModularityKit.Mutator.Benchmarks.Diagnostics.Support; |
| 8 | + |
| 9 | +namespace ModularityKit.Mutator.Benchmarks.Concurrency; |
| 10 | + |
| 11 | +/// <summary> |
| 12 | +/// Benchmarks concurrent batch executions competing for limited runtime availability. |
| 13 | +/// </summary> |
| 14 | +[BenchmarkCategory("Concurrency")] |
| 15 | +[MemoryDiagnoser] |
| 16 | +[InProcess] |
| 17 | +public class BatchSchedulingBenchmarks |
| 18 | +{ |
| 19 | + private const int RuntimeSlots = 2; |
| 20 | + |
| 21 | + private IMutationEngine _engine = null!; |
| 22 | + private BatchScenario[] _scenarios = null!; |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Controls how many batch executions compete for runtime slots during a single benchmark iteration. |
| 26 | + /// </summary> |
| 27 | + [Params(2, 4)] |
| 28 | + public int ConcurrentBatches { get; set; } |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Controls how many mutations each competing batch executes. |
| 32 | + /// </summary> |
| 33 | + [Params(4, 16)] |
| 34 | + public int BatchSize { get; set; } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Prepares the engine and precomputed batch scenarios for the selected parameters. |
| 38 | + /// </summary> |
| 39 | + [GlobalSetup] |
| 40 | + public void Setup() |
| 41 | + { |
| 42 | + _engine = ConcurrencyBenchmarkScenario.BuildEngine( |
| 43 | + RuntimeSlots, |
| 44 | + services => |
| 45 | + { |
| 46 | + services.AddSingleton<IMutationAuditor, NoOpAuditor>(); |
| 47 | + services.AddSingleton<IMutationHistoryStore, NoOpHistoryStore>(); |
| 48 | + }); |
| 49 | + |
| 50 | + _scenarios = Enumerable |
| 51 | + .Range(0, ConcurrentBatches) |
| 52 | + .Select(CreateBatchScenario) |
| 53 | + .ToArray(); |
| 54 | + } |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// Measures scheduler pressure when several ordered batches compete for a limited number of engine slots. |
| 58 | + /// </summary> |
| 59 | + [Benchmark] |
| 60 | + public async Task ConcurrentBatches_LimitedRuntimeAvailability() |
| 61 | + { |
| 62 | + var tasks = new Task[ConcurrentBatches]; |
| 63 | + |
| 64 | + for (var index = 0; index < ConcurrentBatches; index++) |
| 65 | + { |
| 66 | + var scenario = _scenarios[index]; |
| 67 | + tasks[index] = _engine.ExecuteBatchAsync(scenario.Mutations, scenario.State); |
| 68 | + } |
| 69 | + |
| 70 | + await Task.WhenAll(tasks); |
| 71 | + GC.KeepAlive(tasks); |
| 72 | + } |
| 73 | + |
| 74 | + private BatchScenario CreateBatchScenario(int batchIndex) |
| 75 | + { |
| 76 | + var stateId = $"batch-state-{batchIndex}"; |
| 77 | + var mutations = Enumerable |
| 78 | + .Range(0, BatchSize) |
| 79 | + .Select(step => (IMutation<ConcurrencyState>)ConcurrencyBenchmarkScenario.CreateCommitMutation(stateId, $"batch-{batchIndex}-{step}")) |
| 80 | + .ToArray(); |
| 81 | + |
| 82 | + return new BatchScenario(new ConcurrencyState(batchIndex, 0), mutations); |
| 83 | + } |
| 84 | + |
| 85 | + private sealed record BatchScenario( |
| 86 | + ConcurrencyState State, |
| 87 | + IReadOnlyList<IMutation<ConcurrencyState>> Mutations); |
| 88 | +} |
0 commit comments