diff --git a/src/BenchmarkDotNet/ConsoleArguments/ListBenchmarks/BenchmarkCasesPrinter.cs b/src/BenchmarkDotNet/ConsoleArguments/ListBenchmarks/BenchmarkCasesPrinter.cs index 14b6bafa6d..9f955aa1bc 100644 --- a/src/BenchmarkDotNet/ConsoleArguments/ListBenchmarks/BenchmarkCasesPrinter.cs +++ b/src/BenchmarkDotNet/ConsoleArguments/ListBenchmarks/BenchmarkCasesPrinter.cs @@ -1,5 +1,9 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; +using System.Linq; +using BenchmarkDotNet.Configs; using BenchmarkDotNet.Loggers; +using BenchmarkDotNet.Running; namespace BenchmarkDotNet.ConsoleArguments.ListBenchmarks { @@ -14,6 +18,18 @@ public BenchmarkCasesPrinter(ListBenchmarkCaseMode listBenchmarkCaseMode) : new FlatBenchmarkCasesPrinter(); } + public static void PrintList(ILogger nonNullLogger, IConfig effectiveConfig, IReadOnlyList allAvailableTypesWithRunnableBenchmarks, CommandLineOptions options) + { + var printer = new BenchmarkCasesPrinter(options.ListBenchmarkCaseMode); + + var testNames = TypeFilter.Filter(effectiveConfig, allAvailableTypesWithRunnableBenchmarks) + .SelectMany(p => p.BenchmarksCases) + .Select(p => p.Descriptor.GetFilterName()) + .Distinct(); + + printer.Print(testNames, nonNullLogger); + } + public void Print(IEnumerable testNames, ILogger logger) => printer.Print(testNames, logger); } } \ No newline at end of file diff --git a/src/BenchmarkDotNet/Running/BenchmarkRunnerDirty.cs b/src/BenchmarkDotNet/Running/BenchmarkRunnerDirty.cs index 122dc5b835..14534d87e2 100644 --- a/src/BenchmarkDotNet/Running/BenchmarkRunnerDirty.cs +++ b/src/BenchmarkDotNet/Running/BenchmarkRunnerDirty.cs @@ -17,17 +17,17 @@ namespace BenchmarkDotNet.Running public static class BenchmarkRunner { [PublicAPI] - public static Summary Run(IConfig config = null) + public static Summary Run(IConfig config = null, string[] args = null) { using (DirtyAssemblyResolveHelper.Create()) - return RunWithExceptionHandling(() => RunWithDirtyAssemblyResolveHelper(typeof(T), config)); + return RunWithExceptionHandling(() => RunWithDirtyAssemblyResolveHelper(typeof(T), config, args)); } [PublicAPI] - public static Summary Run(Type type, IConfig config = null) + public static Summary Run(Type type, IConfig config = null, string[] args = null) { using (DirtyAssemblyResolveHelper.Create()) - return RunWithExceptionHandling(() => RunWithDirtyAssemblyResolveHelper(type, config)); + return RunWithExceptionHandling(() => RunWithDirtyAssemblyResolveHelper(type, config, args)); } [PublicAPI] @@ -38,10 +38,10 @@ public static Summary Run(Type type, MethodInfo[] methods, IConfig config = null } [PublicAPI] - public static Summary[] Run(Assembly assembly, IConfig config = null) + public static Summary[] Run(Assembly assembly, IConfig config = null, string[] args = null) { using (DirtyAssemblyResolveHelper.Create()) - return RunWithExceptionHandling(() => RunWithDirtyAssemblyResolveHelper(assembly, config)); + return RunWithExceptionHandling(() => RunWithDirtyAssemblyResolveHelper(assembly, config, args)); } [PublicAPI] @@ -73,16 +73,21 @@ public static Summary RunSource(string source, IConfig config = null) } [MethodImpl(MethodImplOptions.NoInlining)] - private static Summary RunWithDirtyAssemblyResolveHelper(Type type, IConfig config) - => BenchmarkRunnerClean.Run(new[] { BenchmarkConverter.TypeToBenchmarks(type, config) }).Single(); + private static Summary RunWithDirtyAssemblyResolveHelper(Type type, IConfig config, string[] args) + => (args == null + ? BenchmarkRunnerClean.Run(new[] { BenchmarkConverter.TypeToBenchmarks(type, config) }) + : new BenchmarkSwitcher(new[] { type }).RunWithDirtyAssemblyResolveHelper(args, config, false)) + .Single(); [MethodImpl(MethodImplOptions.NoInlining)] private static Summary RunWithDirtyAssemblyResolveHelper(Type type, MethodInfo[] methods, IConfig config = null) => BenchmarkRunnerClean.Run(new[] { BenchmarkConverter.MethodsToBenchmarks(type, methods, config) }).Single(); [MethodImpl(MethodImplOptions.NoInlining)] - private static Summary[] RunWithDirtyAssemblyResolveHelper(Assembly assembly, IConfig config = null) - => BenchmarkRunnerClean.Run(assembly.GetRunnableBenchmarks().Select(type => BenchmarkConverter.TypeToBenchmarks(type, config)).ToArray()); + private static Summary[] RunWithDirtyAssemblyResolveHelper(Assembly assembly, IConfig config, string[] args) + => args == null + ? BenchmarkRunnerClean.Run(assembly.GetRunnableBenchmarks().Select(type => BenchmarkConverter.TypeToBenchmarks(type, config)).ToArray()) + : new BenchmarkSwitcher(assembly).RunWithDirtyAssemblyResolveHelper(args, config, false).ToArray(); [MethodImpl(MethodImplOptions.NoInlining)] private static Summary[] RunWithDirtyAssemblyResolveHelper(BenchmarkRunInfo[] benchmarkRunInfos) @@ -126,4 +131,4 @@ private static Summary[] RunWithExceptionHandling(Func run) } } } -} \ No newline at end of file +} diff --git a/src/BenchmarkDotNet/Running/BenchmarkSwitcher.cs b/src/BenchmarkDotNet/Running/BenchmarkSwitcher.cs index 7d918a6b4a..d265cc7477 100644 --- a/src/BenchmarkDotNet/Running/BenchmarkSwitcher.cs +++ b/src/BenchmarkDotNet/Running/BenchmarkSwitcher.cs @@ -64,11 +64,11 @@ public IEnumerable Run(string[] args = null, IConfig config = null) // we need to keep the logic that uses it in a separate method and create DirtyAssemblyResolveHelper first // so it can ignore the version mismatch ;) using (DirtyAssemblyResolveHelper.Create()) - return RunWithDirtyAssemblyResolveHelper(args, config); + return RunWithDirtyAssemblyResolveHelper(args, config, true); } [MethodImpl(MethodImplOptions.NoInlining)] - private IEnumerable RunWithDirtyAssemblyResolveHelper(string[] args, IConfig config) + internal IEnumerable RunWithDirtyAssemblyResolveHelper(string[] args, IConfig config, bool askUserForInput) { var notNullArgs = args ?? Array.Empty(); var notNullConfig = config ?? DefaultConfig.Instance; @@ -105,7 +105,7 @@ private IEnumerable RunWithDirtyAssemblyResolveHelper(string[] args, IC return Array.Empty(); } - var benchmarksToFilter = options.UserProvidedFilters + var benchmarksToFilter = options.UserProvidedFilters || !askUserForInput ? allAvailableTypesWithRunnableBenchmarks : userInteraction.AskUser(allAvailableTypesWithRunnableBenchmarks, logger);