diff --git a/src/BenchmarkDotNet/Running/BenchmarkRunnerDirty.cs b/src/BenchmarkDotNet/Running/BenchmarkRunnerDirty.cs index 8c2bc2737e..573930b2fc 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)); } ///