Skip to content

Commit 5589986

Browse files
chan18chan18
chan18
authored and
chan18
committed
added requested changes, after first code review
1 parent 916dc35 commit 5589986

File tree

3 files changed

+29
-44
lines changed

3 files changed

+29
-44
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: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ namespace BenchmarkDotNet.Running
2020
// so it can ignore the version mismatch ;)
2121
public static class BenchmarkRunner
2222
{
23-
private static readonly IUserInteraction userInteraction = new UserInteraction();
24-
private static readonly List<Type> types = new List<Type>();
25-
private static readonly List<Assembly> assemblies = new List<Assembly>();
26-
2723
[PublicAPI]
2824
public static Summary Run<T>(IConfig config = null, string[] args = null)
2925
{
@@ -84,6 +80,7 @@ public static Summary RunSource(string source, IConfig config = null)
8480
private static Summary RunWithDirtyAssemblyResolveHelper(Type type, IConfig config, string[] args)
8581
{
8682
var logger = config.GetNonNullCompositeLogger();
83+
var userInteraction = new UserInteraction();
8784
var (isParsingSuccess, parsedConfig, options) = ConfigParser.Parse(args, logger, config);
8885
if (!isParsingSuccess) // invalid console args, the ConfigParser printed the error
8986
return null;
@@ -96,8 +93,9 @@ private static Summary RunWithDirtyAssemblyResolveHelper(Type type, IConfig conf
9693

9794
var effectiveConfig = ManualConfig.Union(config, parsedConfig);
9895

96+
var types = new List<Type>();
9997
types.Add(type);
100-
var (allTypesValid, allAvailableTypesWithRunnableBenchmarks) = TypeFilter.GetTypesWithRunnableBenchmarks(types, assemblies, logger);
98+
var (allTypesValid, allAvailableTypesWithRunnableBenchmarks) = TypeFilter.GetTypesWithRunnableBenchmarks(types, new List<Assembly>(), logger);
10199
if (!allTypesValid) // there were some invalid and TypeFilter printed errors
102100
return null;
103101

@@ -109,22 +107,15 @@ private static Summary RunWithDirtyAssemblyResolveHelper(Type type, IConfig conf
109107

110108
if (options.ListBenchmarkCaseMode != ListBenchmarkCaseMode.Disabled)
111109
{
112-
PrintList(logger, effectiveConfig, allAvailableTypesWithRunnableBenchmarks, options);
110+
BenchmarkCasesPrinter.PrintList(logger, effectiveConfig, allAvailableTypesWithRunnableBenchmarks, options);
113111
return null;
114112
}
115113

116-
var benchmarksToFilter = options.UserProvidedFilters
117-
? allAvailableTypesWithRunnableBenchmarks
118-
: userInteraction.AskUser(allAvailableTypesWithRunnableBenchmarks, logger);
119-
120-
var filteredBenchmarks = TypeFilter.Filter(effectiveConfig, benchmarksToFilter);
121-
122-
if (filteredBenchmarks.IsEmpty())
123-
{
124-
return BenchmarkRunnerClean.Run(new[] { BenchmarkConverter.TypeToBenchmarks(type, config) }).Single();
125-
}
114+
var benchmarks = options.UserProvidedFilters
115+
? TypeFilter.Filter(effectiveConfig, allAvailableTypesWithRunnableBenchmarks)
116+
: new BenchmarkRunInfo[] { BenchmarkConverter.TypeToBenchmarks(type, config) };
126117

127-
return BenchmarkRunnerClean.Run(filteredBenchmarks).Single();
118+
return BenchmarkRunnerClean.Run(benchmarks).Single();
128119
}
129120

130121

@@ -177,16 +168,6 @@ private static Summary[] RunWithExceptionHandling(Func<Summary[]> run)
177168
return new[] { Summary.NothingToRun(e.Message, string.Empty, string.Empty) };
178169
}
179170
}
180-
private static void PrintList(ILogger nonNullLogger, IConfig effectiveConfig, IReadOnlyList<Type> allAvailableTypesWithRunnableBenchmarks, CommandLineOptions options)
181-
{
182-
var printer = new BenchmarkCasesPrinter(options.ListBenchmarkCaseMode);
183171

184-
var testNames = TypeFilter.Filter(effectiveConfig, allAvailableTypesWithRunnableBenchmarks)
185-
.SelectMany(p => p.BenchmarksCases)
186-
.Select(p => p.Descriptor.GetFilterName())
187-
.Distinct();
188-
189-
printer.Print(testNames, nonNullLogger);
190-
}
191172
}
192173
}

src/BenchmarkDotNet/Running/BenchmarkSwitcher.cs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
using JetBrains.Annotations;
1414

1515
namespace BenchmarkDotNet.Running
16-
{
17-
public class BenchmarkSwitcher
16+
{ public class BenchmarkSwitcher
1817
{
1918
private readonly IUserInteraction userInteraction = new UserInteraction();
2019
private readonly List<Type> types = new List<Type>();
@@ -65,7 +64,7 @@ public IEnumerable<Summary> Run(string[] args = null, IConfig config = null)
6564
[MethodImpl(MethodImplOptions.NoInlining)]
6665
private IEnumerable<Summary> RunWithDirtyAssemblyResolveHelper(string[] args, IConfig config)
6766
{
68-
var logger = config.GetNonNullCompositeLogger();
67+
var logger = config.GetNonNullCompositeLogger();
6968
var (isParsingSuccess, parsedConfig, options) = ConfigParser.Parse(args, logger, config);
7069
if (!isParsingSuccess) // invalid console args, the ConfigParser printed the error
7170
return Array.Empty<Summary>();
@@ -90,7 +89,7 @@ private IEnumerable<Summary> RunWithDirtyAssemblyResolveHelper(string[] args, IC
9089

9190
if (options.ListBenchmarkCaseMode != ListBenchmarkCaseMode.Disabled)
9291
{
93-
PrintList(logger, effectiveConfig, allAvailableTypesWithRunnableBenchmarks, options);
92+
BenchmarkCasesPrinter.PrintList(logger, effectiveConfig, allAvailableTypesWithRunnableBenchmarks, options);
9493
return Array.Empty<Summary>();
9594
}
9695

@@ -108,17 +107,6 @@ private IEnumerable<Summary> RunWithDirtyAssemblyResolveHelper(string[] args, IC
108107

109108
return BenchmarkRunnerClean.Run(filteredBenchmarks);
110109
}
111-
112-
private static void PrintList(ILogger nonNullLogger, IConfig effectiveConfig, IReadOnlyList<Type> allAvailableTypesWithRunnableBenchmarks, CommandLineOptions options)
113-
{
114-
var printer = new BenchmarkCasesPrinter(options.ListBenchmarkCaseMode);
115-
116-
var testNames = TypeFilter.Filter(effectiveConfig, allAvailableTypesWithRunnableBenchmarks)
117-
.SelectMany(p => p.BenchmarksCases)
118-
.Select(p => p.Descriptor.GetFilterName())
119-
.Distinct();
120-
121-
printer.Print(testNames, nonNullLogger);
122-
}
110+
123111
}
124112
}

0 commit comments

Comments
 (0)