Skip to content

Commit 58ee5c7

Browse files
Add the args parameter to RunAll and RunAllJoined (#2339)
* Add args parameter to RunAll and RunAllJoined * Fix test * Fix NRE
1 parent 2e1774d commit 58ee5c7

File tree

4 files changed

+68
-3
lines changed

4 files changed

+68
-3
lines changed

src/BenchmarkDotNet/BenchmarkDotNet.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<EmbeddedResource Include="Environments\microarchitectures.txt" />
1818
</ItemGroup>
1919
<ItemGroup>
20-
<PackageReference Include="CommandLineParser" Version="2.4.3" />
20+
<PackageReference Include="CommandLineParser" Version="2.9.1" />
2121
<PackageReference Include="Gee.External.Capstone" Version="2.3.0" />
2222
<PackageReference Include="Iced" Version="1.17.0" />
2323
<PackageReference Include="Microsoft.Diagnostics.Runtime" Version="2.2.332302" />

src/BenchmarkDotNet/ConsoleArguments/ConfigParser.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,31 @@ public static (bool isSuccess, IConfig config, CommandLineOptions options) Parse
8686
return result;
8787
}
8888

89+
internal static bool TryUpdateArgs(string[] args, out string[]? updatedArgs, Action<CommandLineOptions> updater)
90+
{
91+
(bool isSuccess, CommandLineOptions options) result = default;
92+
93+
ILogger logger = NullLogger.Instance;
94+
using (var parser = CreateParser(logger))
95+
{
96+
parser
97+
.ParseArguments<CommandLineOptions>(args)
98+
.WithParsed(options => result = Validate(options, logger) ? (true, options) : (false, default))
99+
.WithNotParsed(errors => result = (false, default));
100+
101+
if (!result.isSuccess)
102+
{
103+
updatedArgs = null;
104+
return false;
105+
}
106+
107+
updater(result.options);
108+
109+
updatedArgs = parser.FormatCommandLine(result.options, settings => settings.SkipDefault = true).Split();
110+
return true;
111+
}
112+
}
113+
89114
private static Parser CreateParser(ILogger logger)
90115
=> new Parser(settings =>
91116
{

src/BenchmarkDotNet/Running/BenchmarkSwitcher.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,26 @@ public class BenchmarkSwitcher
5353
/// <summary>
5454
/// Run all available benchmarks.
5555
/// </summary>
56-
[PublicAPI] public IEnumerable<Summary> RunAll(IConfig? config = null) => Run(new[] { "--filter", "*" }, config);
56+
[PublicAPI] public IEnumerable<Summary> RunAll(IConfig? config = null, string[]? args = null)
57+
{
58+
args ??= Array.Empty<string>();
59+
if (ConfigParser.TryUpdateArgs(args, out var updatedArgs, options => options.Filters = new[] { "*" }))
60+
args = updatedArgs;
61+
62+
return Run(args, config);
63+
}
5764

5865
/// <summary>
5966
/// Run all available benchmarks and join them to a single summary
6067
/// </summary>
61-
[PublicAPI] public Summary RunAllJoined(IConfig? config = null) => Run(new[] { "--filter", "*", "--join" }, config).Single();
68+
[PublicAPI] public Summary RunAllJoined(IConfig? config = null, string[]? args = null)
69+
{
70+
args ??= Array.Empty<string>();
71+
if (ConfigParser.TryUpdateArgs(args, out var updatedArgs, options => (options.Join, options.Filters) = (true, new[] { "*" })))
72+
args = updatedArgs;
73+
74+
return Run(args, config).Single();
75+
}
6276

6377
[PublicAPI]
6478
public IEnumerable<Summary> Run(string[]? args = null, IConfig? config = null)

tests/BenchmarkDotNet.Tests/ConfigParserTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,5 +586,31 @@ public void UsersCanSpecifyWithoutOverheadEvalution()
586586
Assert.False(job.Accuracy.EvaluateOverhead);
587587
}
588588
}
589+
590+
[Theory]
591+
[InlineData("--filter abc", "--filter *")]
592+
[InlineData("-f abc", "--filter *")]
593+
[InlineData("-f *", "--filter *")]
594+
[InlineData("--runtimes net7.0 --join", "--filter * --join --runtimes net7.0")]
595+
[InlineData("--join abc", "--filter * --join")]
596+
public void CheckUpdateValidArgs(string strArgs, string expected)
597+
{
598+
var args = strArgs.Split();
599+
_ = ConfigParser.TryUpdateArgs(args, out var updatedArgs, options => options.Filters = new[] { "*" });
600+
601+
Assert.Equal(expected.Split(), updatedArgs);
602+
}
603+
604+
[Theory]
605+
[InlineData("--filter abc -f abc")]
606+
[InlineData("--runtimes net")]
607+
public void CheckUpdateInvalidArgs(string strArgs)
608+
{
609+
var args = strArgs.Split();
610+
bool isSuccess = ConfigParser.TryUpdateArgs(args, out var updatedArgs, options => options.Filters = new[] { "*" });
611+
612+
Assert.Null(updatedArgs);
613+
Assert.False(isSuccess);
614+
}
589615
}
590616
}

0 commit comments

Comments
 (0)