Skip to content

passed args to benchmark runner #1292

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Jan 29, 2021
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -14,6 +18,18 @@ public BenchmarkCasesPrinter(ListBenchmarkCaseMode listBenchmarkCaseMode)
: new FlatBenchmarkCasesPrinter();
}

public static void PrintList(ILogger nonNullLogger, IConfig effectiveConfig, IReadOnlyList<Type> 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<string> testNames, ILogger logger) => printer.Print(testNames, logger);
}
}
85 changes: 77 additions & 8 deletions src/BenchmarkDotNet/Running/BenchmarkRunnerDirty.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.ConsoleArguments;
using BenchmarkDotNet.ConsoleArguments.ListBenchmarks;
using BenchmarkDotNet.Environments;
using BenchmarkDotNet.Extensions;
using BenchmarkDotNet.Loggers;
using BenchmarkDotNet.Portability;
Expand All @@ -17,17 +21,24 @@ namespace BenchmarkDotNet.Running
public static class BenchmarkRunner
{
[PublicAPI]
public static Summary Run<T>(IConfig config = null)
public static Summary Run<T>(IConfig config = null, string[] args = null)
{
using (DirtyAssemblyResolveHelper.Create())
return RunWithExceptionHandling(() => RunWithDirtyAssemblyResolveHelper(typeof(T), config));
return RunWithExceptionHandling(() => RunWithDirtyAssemblyResolveHelper(typeof(T), config, args ?? Array.Empty<string>()));
}

[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 ?? Array.Empty<string>()));
}

[PublicAPI]
public static Summary Run(Assembly assembly, IConfig config = null, string[] args = null)
{
using (DirtyAssemblyResolveHelper.Create())
return RunWithExceptionHandling(() => RunWithDirtyAssemblyResolveHelper(assembly, config, args ?? Array.Empty<string>()));
}

[PublicAPI]
Expand Down Expand Up @@ -73,13 +84,17 @@ 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)
=> RunWithDirtyAssemblyResolveHelper(new[] { type }, Array.Empty<Assembly>(), config, args);

[MethodImpl(MethodImplOptions.NoInlining)]
private static Summary RunWithDirtyAssemblyResolveHelper(Assembly assembly, IConfig config, string[] args)
=> RunWithDirtyAssemblyResolveHelper(assembly.GetRunnableBenchmarks().ToArray(), new[] { assembly }, config, args);

[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());
Expand Down Expand Up @@ -125,5 +140,59 @@ private static Summary[] RunWithExceptionHandling(Func<Summary[]> run)
return new[] { Summary.NothingToRun(e.Message, string.Empty, string.Empty) };
}
}

private static Summary RunWithDirtyAssemblyResolveHelper(IEnumerable<Type> types, IEnumerable<Assembly> assemblies, IConfig config, string[] args)
{
var logger = config.GetNonNullCompositeLogger();
var userInteraction = new UserInteraction();
var (isParsingSuccess, parsedConfig, options) = ConfigParser.Parse(args, logger, config);
if (!isParsingSuccess) // invalid console args, the ConfigParser printed the error
return null;

if (options.PrintInformation)
{
logger.WriteLine(HostEnvironmentInfo.GetInformation());
return null;
}

var effectiveConfig = ManualConfig.Union(config, parsedConfig);
var (allTypesValid, allAvailableTypesWithRunnableBenchmarks) = TypeFilter.GetTypesWithRunnableBenchmarks(types, assemblies, logger);

if (!allTypesValid) // there were some invalid and TypeFilter printed errors
return null;

if (allAvailableTypesWithRunnableBenchmarks.IsEmpty())
{
userInteraction.PrintNoBenchmarksError(logger);
return null;
}

if (options.ListBenchmarkCaseMode != ListBenchmarkCaseMode.Disabled)
{
BenchmarkCasesPrinter.PrintList(logger, effectiveConfig, allAvailableTypesWithRunnableBenchmarks, options);
return null;
}

var BenchmarkRunInfos = new List<BenchmarkRunInfo[]>();

foreach (Type type in types)
{
var BenchmarkRunInfo = options.UserProvidedFilters
? TypeFilter.Filter(effectiveConfig, allAvailableTypesWithRunnableBenchmarks)
: new BenchmarkRunInfo[] { BenchmarkConverter.TypeToBenchmarks(type, config) };

BenchmarkRunInfos.Add(BenchmarkRunInfo);
}

Summary benchmark = BenchmarkRunnerClean.Run(BenchmarkRunInfos.First()).Single();

foreach (var BenchmarkRunInfo in BenchmarkRunInfos.Skip(1))
{
benchmark = BenchmarkRunnerClean.Run(BenchmarkRunInfo).Single();
}

return benchmark;
}

}
}
}
16 changes: 2 additions & 14 deletions src/BenchmarkDotNet/Running/BenchmarkSwitcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private IEnumerable<Summary> RunWithDirtyAssemblyResolveHelper(string[] args, IC

if (options.ListBenchmarkCaseMode != ListBenchmarkCaseMode.Disabled)
{
PrintList(logger, effectiveConfig, allAvailableTypesWithRunnableBenchmarks, options);
BenchmarkCasesPrinter.PrintList(logger, effectiveConfig, allAvailableTypesWithRunnableBenchmarks, options);
return Array.Empty<Summary>();
}

Expand All @@ -108,17 +108,5 @@ private IEnumerable<Summary> RunWithDirtyAssemblyResolveHelper(string[] args, IC

return BenchmarkRunnerClean.Run(filteredBenchmarks);
}

private static void PrintList(ILogger nonNullLogger, IConfig effectiveConfig, IReadOnlyList<Type> 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);
}
}
}
}