Skip to content

Commit 6a151fe

Browse files
chan18adamsitnik
andauthored
passed args to benchmark runner (#1292)
* passed args to benchmark runner * added requested changes, after first code review Co-Authored-By: Adam Sitnik <[email protected]>
1 parent 9f5d706 commit 6a151fe

File tree

3 files changed

+36
-15
lines changed

3 files changed

+36
-15
lines changed

src/BenchmarkDotNet/ConsoleArguments/ListBenchmarks/BenchmarkCasesPrinter.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using BenchmarkDotNet.Configs;
25
using BenchmarkDotNet.Loggers;
6+
using BenchmarkDotNet.Running;
37

48
namespace BenchmarkDotNet.ConsoleArguments.ListBenchmarks
59
{
@@ -14,6 +18,18 @@ public BenchmarkCasesPrinter(ListBenchmarkCaseMode listBenchmarkCaseMode)
1418
: new FlatBenchmarkCasesPrinter();
1519
}
1620

21+
public static void PrintList(ILogger nonNullLogger, IConfig effectiveConfig, IReadOnlyList<Type> allAvailableTypesWithRunnableBenchmarks, CommandLineOptions options)
22+
{
23+
var printer = new BenchmarkCasesPrinter(options.ListBenchmarkCaseMode);
24+
25+
var testNames = TypeFilter.Filter(effectiveConfig, allAvailableTypesWithRunnableBenchmarks)
26+
.SelectMany(p => p.BenchmarksCases)
27+
.Select(p => p.Descriptor.GetFilterName())
28+
.Distinct();
29+
30+
printer.Print(testNames, nonNullLogger);
31+
}
32+
1733
public void Print(IEnumerable<string> testNames, ILogger logger) => printer.Print(testNames, logger);
1834
}
1935
}

src/BenchmarkDotNet/Running/BenchmarkRunnerDirty.cs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ namespace BenchmarkDotNet.Running
1717
public static class BenchmarkRunner
1818
{
1919
[PublicAPI]
20-
public static Summary Run<T>(IConfig config = null)
20+
public static Summary Run<T>(IConfig config = null, string[] args = null)
2121
{
2222
using (DirtyAssemblyResolveHelper.Create())
23-
return RunWithExceptionHandling(() => RunWithDirtyAssemblyResolveHelper(typeof(T), config));
23+
return RunWithExceptionHandling(() => RunWithDirtyAssemblyResolveHelper(typeof(T), config, args));
2424
}
2525

2626
[PublicAPI]
27-
public static Summary Run(Type type, IConfig config = null)
27+
public static Summary Run(Type type, IConfig config = null, string[] args = null)
2828
{
2929
using (DirtyAssemblyResolveHelper.Create())
30-
return RunWithExceptionHandling(() => RunWithDirtyAssemblyResolveHelper(type, config));
30+
return RunWithExceptionHandling(() => RunWithDirtyAssemblyResolveHelper(type, config, args));
3131
}
3232

3333
[PublicAPI]
@@ -38,10 +38,10 @@ public static Summary Run(Type type, MethodInfo[] methods, IConfig config = null
3838
}
3939

4040
[PublicAPI]
41-
public static Summary[] Run(Assembly assembly, IConfig config = null)
41+
public static Summary[] Run(Assembly assembly, IConfig config = null, string[] args = null)
4242
{
4343
using (DirtyAssemblyResolveHelper.Create())
44-
return RunWithExceptionHandling(() => RunWithDirtyAssemblyResolveHelper(assembly, config));
44+
return RunWithExceptionHandling(() => RunWithDirtyAssemblyResolveHelper(assembly, config, args));
4545
}
4646

4747
[PublicAPI]
@@ -73,16 +73,21 @@ public static Summary RunSource(string source, IConfig config = null)
7373
}
7474

7575
[MethodImpl(MethodImplOptions.NoInlining)]
76-
private static Summary RunWithDirtyAssemblyResolveHelper(Type type, IConfig config)
77-
=> BenchmarkRunnerClean.Run(new[] { BenchmarkConverter.TypeToBenchmarks(type, config) }).Single();
76+
private static Summary RunWithDirtyAssemblyResolveHelper(Type type, IConfig config, string[] args)
77+
=> (args == null
78+
? BenchmarkRunnerClean.Run(new[] { BenchmarkConverter.TypeToBenchmarks(type, config) })
79+
: new BenchmarkSwitcher(new[] { type }).RunWithDirtyAssemblyResolveHelper(args, config, false))
80+
.Single();
7881

7982
[MethodImpl(MethodImplOptions.NoInlining)]
8083
private static Summary RunWithDirtyAssemblyResolveHelper(Type type, MethodInfo[] methods, IConfig config = null)
8184
=> BenchmarkRunnerClean.Run(new[] { BenchmarkConverter.MethodsToBenchmarks(type, methods, config) }).Single();
8285

8386
[MethodImpl(MethodImplOptions.NoInlining)]
84-
private static Summary[] RunWithDirtyAssemblyResolveHelper(Assembly assembly, IConfig config = null)
85-
=> BenchmarkRunnerClean.Run(assembly.GetRunnableBenchmarks().Select(type => BenchmarkConverter.TypeToBenchmarks(type, config)).ToArray());
87+
private static Summary[] RunWithDirtyAssemblyResolveHelper(Assembly assembly, IConfig config, string[] args)
88+
=> args == null
89+
? BenchmarkRunnerClean.Run(assembly.GetRunnableBenchmarks().Select(type => BenchmarkConverter.TypeToBenchmarks(type, config)).ToArray())
90+
: new BenchmarkSwitcher(assembly).RunWithDirtyAssemblyResolveHelper(args, config, false).ToArray();
8691

8792
[MethodImpl(MethodImplOptions.NoInlining)]
8893
private static Summary[] RunWithDirtyAssemblyResolveHelper(BenchmarkRunInfo[] benchmarkRunInfos)
@@ -126,4 +131,4 @@ private static Summary[] RunWithExceptionHandling(Func<Summary[]> run)
126131
}
127132
}
128133
}
129-
}
134+
}

src/BenchmarkDotNet/Running/BenchmarkSwitcher.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ public IEnumerable<Summary> Run(string[] args = null, IConfig config = null)
6464
// we need to keep the logic that uses it in a separate method and create DirtyAssemblyResolveHelper first
6565
// so it can ignore the version mismatch ;)
6666
using (DirtyAssemblyResolveHelper.Create())
67-
return RunWithDirtyAssemblyResolveHelper(args, config);
67+
return RunWithDirtyAssemblyResolveHelper(args, config, true);
6868
}
6969

7070
[MethodImpl(MethodImplOptions.NoInlining)]
71-
private IEnumerable<Summary> RunWithDirtyAssemblyResolveHelper(string[] args, IConfig config)
71+
internal IEnumerable<Summary> RunWithDirtyAssemblyResolveHelper(string[] args, IConfig config, bool askUserForInput)
7272
{
7373
var notNullArgs = args ?? Array.Empty<string>();
7474
var notNullConfig = config ?? DefaultConfig.Instance;
@@ -105,7 +105,7 @@ private IEnumerable<Summary> RunWithDirtyAssemblyResolveHelper(string[] args, IC
105105
return Array.Empty<Summary>();
106106
}
107107

108-
var benchmarksToFilter = options.UserProvidedFilters
108+
var benchmarksToFilter = options.UserProvidedFilters || !askUserForInput
109109
? allAvailableTypesWithRunnableBenchmarks
110110
: userInteraction.AskUser(allAvailableTypesWithRunnableBenchmarks, logger);
111111

0 commit comments

Comments
 (0)