From 673e979ead2637220df227b9bb42bbe1002961e2 Mon Sep 17 00:00:00 2001 From: Farhaz565 Date: Wed, 29 Jul 2026 16:45:47 +0530 Subject: [PATCH 1/2] Add CancellationToken support to synchronous benchmark runner APIs --- .../Running/BenchmarkRunnerDirty.cs | 66 ++++++++++++++----- .../Running/BenchmarkSwitcher.cs | 12 ++-- 2 files changed, 54 insertions(+), 24 deletions(-) diff --git a/src/BenchmarkDotNet/Running/BenchmarkRunnerDirty.cs b/src/BenchmarkDotNet/Running/BenchmarkRunnerDirty.cs index 8c2bc2737e..3c7b9e9189 100644 --- a/src/BenchmarkDotNet/Running/BenchmarkRunnerDirty.cs +++ b/src/BenchmarkDotNet/Running/BenchmarkRunnerDirty.cs @@ -15,54 +15,81 @@ namespace BenchmarkDotNet.Running public static class BenchmarkRunner { [PublicAPI] - public static Summary Run(IConfig? config = null, string[]? args = null) + public static Summary Run( + IConfig? config = null, + string[]? args = null, + CancellationToken cancellationToken = default) { using var context = BenchmarkSynchronizationContext.CreateAndSetCurrent(); - return context.ExecuteUntilComplete(RunAsync(config, args)); + return context.ExecuteUntilComplete( + RunAsync(config, args, cancellationToken)); } [PublicAPI] - public static Summary Run(Type type, IConfig? config = null, string[]? args = null) + public static Summary Run( + Type type, + IConfig? config = null, + string[]? args = null, + CancellationToken cancellationToken = default) { using var context = BenchmarkSynchronizationContext.CreateAndSetCurrent(); - return context.ExecuteUntilComplete(RunAsync(type, config, args)); + return context.ExecuteUntilComplete( + RunAsync(type, config, args, cancellationToken)); } [PublicAPI] - public static Summary[] Run(Type[] types, IConfig? config = null, string[]? args = null) + public static Summary[] Run( + Type[] types, + IConfig? config = null, + string[]? args = null, + CancellationToken cancellationToken = default) { using var context = BenchmarkSynchronizationContext.CreateAndSetCurrent(); - return context.ExecuteUntilComplete(RunAsync(types, config, args)); + return context.ExecuteUntilComplete( + RunAsync(types, config, args, cancellationToken)); } [PublicAPI] - public static Summary Run(Type type, MethodInfo[] methods, IConfig? config = null) + public static Summary Run( + Type type, + MethodInfo[] methods, + IConfig? config = null, + CancellationToken cancellationToken = default) { using var context = BenchmarkSynchronizationContext.CreateAndSetCurrent(); - return context.ExecuteUntilComplete(RunAsync(type, methods, config)); + return context.ExecuteUntilComplete( + RunAsync(type, methods, config, cancellationToken)); } [PublicAPI] - public static Summary[] Run(Assembly assembly, IConfig? config = null, string[]? args = null) + public static Summary[] Run( + Assembly assembly, + IConfig? config = null, + string[]? args = null, + CancellationToken cancellationToken = default) { using var context = BenchmarkSynchronizationContext.CreateAndSetCurrent(); - return context.ExecuteUntilComplete(RunAsync(assembly, config, args)); + return context.ExecuteUntilComplete( + RunAsync(assembly, config, args, cancellationToken)); } [PublicAPI] - public static Summary Run(BenchmarkRunInfo benchmarkRunInfo) + public static Summary Run(BenchmarkRunInfo benchmarkRunInfo, CancellationToken cancellationToken = default) { using var context = BenchmarkSynchronizationContext.CreateAndSetCurrent(); - return context.ExecuteUntilComplete(RunAsync(benchmarkRunInfo)); + return context.ExecuteUntilComplete( + RunAsync(benchmarkRunInfo, cancellationToken)); } [PublicAPI] - public static Summary[] Run(BenchmarkRunInfo[] benchmarkRunInfos) + public static Summary[] Run( + BenchmarkRunInfo[] benchmarkRunInfos, + CancellationToken cancellationToken = default) { using var context = BenchmarkSynchronizationContext.CreateAndSetCurrent(); - return context.ExecuteUntilComplete(RunAsync(benchmarkRunInfos)); + return context.ExecuteUntilComplete( + RunAsync(benchmarkRunInfos, cancellationToken)); } - [PublicAPI] public static ValueTask RunAsync(IConfig? config = null, string[]? args = null, CancellationToken cancellationToken = default) => RunAsync(typeof(T), config, args, cancellationToken); @@ -102,10 +129,13 @@ public static async ValueTask RunAsync(Assembly assembly, IConfig? co return await RunWithExceptionHandling(() => RunWithDirtyAssemblyResolveHelper(assembly, config, args, cancellationToken)).ConfigureAwait(false); } } - [PublicAPI] - public static async ValueTask RunAsync(BenchmarkRunInfo benchmarkRunInfo) - => (await RunAsync([benchmarkRunInfo]).ConfigureAwait(false)).Single(); + public static async ValueTask RunAsync( + BenchmarkRunInfo benchmarkRunInfo, + CancellationToken cancellationToken = default) + => (await RunAsync([benchmarkRunInfo], cancellationToken) + .ConfigureAwait(false)) + .Single(); [PublicAPI] public static async ValueTask RunAsync(BenchmarkRunInfo[] benchmarkRunInfos, CancellationToken cancellationToken = default) diff --git a/src/BenchmarkDotNet/Running/BenchmarkSwitcher.cs b/src/BenchmarkDotNet/Running/BenchmarkSwitcher.cs index 8697a5086e..5b23a63a55 100644 --- a/src/BenchmarkDotNet/Running/BenchmarkSwitcher.cs +++ b/src/BenchmarkDotNet/Running/BenchmarkSwitcher.cs @@ -52,27 +52,27 @@ public class BenchmarkSwitcher /// Run all available benchmarks. /// [PublicAPI] - public IEnumerable RunAll(IConfig? config = null, string[]? args = null) + public IEnumerable RunAll(IConfig? config = null, string[]? args = null, CancellationToken cancellationToken = default) { using var context = BenchmarkSynchronizationContext.CreateAndSetCurrent(); - return context.ExecuteUntilComplete(RunAllAsync(config, args)); + return context.ExecuteUntilComplete(RunAllAsync(config, args, cancellationToken)); } /// /// Run all available benchmarks and join them to a single summary /// [PublicAPI] - public Summary RunAllJoined(IConfig? config = null, string[]? args = null) + public Summary RunAllJoined(IConfig? config = null, string[]? args = null, CancellationToken cancellationToken = default) { using var context = BenchmarkSynchronizationContext.CreateAndSetCurrent(); - return context.ExecuteUntilComplete(RunAllJoinedAsync(config, args)); + return context.ExecuteUntilComplete(RunAllJoinedAsync(config, args, cancellationToken)); } [PublicAPI] - public IEnumerable Run(string[]? args = null, IConfig? config = null) + public IEnumerable Run(string[]? args = null, IConfig? config = null, CancellationToken cancellationToken = default) { using var context = BenchmarkSynchronizationContext.CreateAndSetCurrent(); - return context.ExecuteUntilComplete(RunAsync(args, config)); + return context.ExecuteUntilComplete(RunAsync(args, config, cancellationToken)); } /// From 58c2be277cf5cbd9aad111f8a96095f0ac9c295e Mon Sep 17 00:00:00 2001 From: Farhaz565 Date: Thu, 30 Jul 2026 11:32:19 +0530 Subject: [PATCH 2/2] Fix indentation --- .../Running/BenchmarkRunnerDirty.cs | 44 +++++++++---------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/src/BenchmarkDotNet/Running/BenchmarkRunnerDirty.cs b/src/BenchmarkDotNet/Running/BenchmarkRunnerDirty.cs index 3c7b9e9189..573930b2fc 100644 --- a/src/BenchmarkDotNet/Running/BenchmarkRunnerDirty.cs +++ b/src/BenchmarkDotNet/Running/BenchmarkRunnerDirty.cs @@ -16,9 +16,9 @@ public static class BenchmarkRunner { [PublicAPI] public static Summary Run( - IConfig? config = null, - string[]? args = null, - CancellationToken cancellationToken = default) + IConfig? config = null, + string[]? args = null, + CancellationToken cancellationToken = default) { using var context = BenchmarkSynchronizationContext.CreateAndSetCurrent(); return context.ExecuteUntilComplete( @@ -27,10 +27,10 @@ public static Summary Run( [PublicAPI] public static Summary Run( - Type type, - IConfig? config = null, - string[]? args = null, - CancellationToken cancellationToken = default) + Type type, + IConfig? config = null, + string[]? args = null, + CancellationToken cancellationToken = default) { using var context = BenchmarkSynchronizationContext.CreateAndSetCurrent(); return context.ExecuteUntilComplete( @@ -39,10 +39,10 @@ public static Summary Run( [PublicAPI] public static Summary[] Run( - Type[] types, - IConfig? config = null, - string[]? args = null, - CancellationToken cancellationToken = default) + Type[] types, + IConfig? config = null, + string[]? args = null, + CancellationToken cancellationToken = default) { using var context = BenchmarkSynchronizationContext.CreateAndSetCurrent(); return context.ExecuteUntilComplete( @@ -51,10 +51,10 @@ public static Summary[] Run( [PublicAPI] public static Summary Run( - Type type, - MethodInfo[] methods, - IConfig? config = null, - CancellationToken cancellationToken = default) + Type type, + MethodInfo[] methods, + IConfig? config = null, + CancellationToken cancellationToken = default) { using var context = BenchmarkSynchronizationContext.CreateAndSetCurrent(); return context.ExecuteUntilComplete( @@ -63,10 +63,10 @@ public static Summary Run( [PublicAPI] public static Summary[] Run( - Assembly assembly, - IConfig? config = null, - string[]? args = null, - CancellationToken cancellationToken = default) + Assembly assembly, + IConfig? config = null, + string[]? args = null, + CancellationToken cancellationToken = default) { using var context = BenchmarkSynchronizationContext.CreateAndSetCurrent(); return context.ExecuteUntilComplete( @@ -78,13 +78,13 @@ public static Summary Run(BenchmarkRunInfo benchmarkRunInfo, CancellationToken c { using var context = BenchmarkSynchronizationContext.CreateAndSetCurrent(); return context.ExecuteUntilComplete( - RunAsync(benchmarkRunInfo, cancellationToken)); + RunAsync(benchmarkRunInfo, cancellationToken)); } [PublicAPI] public static Summary[] Run( - BenchmarkRunInfo[] benchmarkRunInfos, - CancellationToken cancellationToken = default) + BenchmarkRunInfo[] benchmarkRunInfos, + CancellationToken cancellationToken = default) { using var context = BenchmarkSynchronizationContext.CreateAndSetCurrent(); return context.ExecuteUntilComplete(