Skip to content

Commit 583bb66

Browse files
committed
Display nice name instead tfm when set runtimes from cmd
1 parent abbd2b0 commit 583bb66

4 files changed

Lines changed: 61 additions & 19 deletions

File tree

src/BenchmarkDotNet/ConsoleArguments/ConfigParser.cs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ private static bool Validate(CommandLineOptions options, ILogger logger)
106106

107107
foreach (string runtime in options.Runtimes)
108108
{
109-
if (!TryParse(runtime, out RuntimeMoniker runtimeMoniker))
109+
if (!TryParse(runtime, out RuntimeMoniker runtimeMoniker, out _))
110110
{
111111
logger.WriteLineError($"The provided runtime \"{runtime}\" is invalid. Available options are: {string.Join(", ", Enum.GetNames(typeof(RuntimeMoniker)).Select(name => name.ToLower()))}.");
112112
return false;
@@ -359,7 +359,7 @@ private static IEnumerable<Job> Expand(Job baseJob, CommandLineOptions options,
359359

360360
private static Job CreateJobForGivenRuntime(Job baseJob, string runtimeId, CommandLineOptions options)
361361
{
362-
if (!TryParse(runtimeId, out RuntimeMoniker runtimeMoniker))
362+
if (!TryParse(runtimeId, out RuntimeMoniker runtimeMoniker, out string platformSpecificPostfix))
363363
{
364364
throw new InvalidOperationException("Impossible, already validated by the Validate method");
365365
}
@@ -373,9 +373,10 @@ private static Job CreateJobForGivenRuntime(Job baseJob, string runtimeId, Comma
373373
case RuntimeMoniker.Net472:
374374
case RuntimeMoniker.Net48:
375375
case RuntimeMoniker.Net481:
376+
var clrRuntime = runtimeMoniker.GetRuntime();
376377
return baseJob
377-
.WithRuntime(runtimeMoniker.GetRuntime())
378-
.WithToolchain(CsProjClassicNetToolchain.From(runtimeId, options.RestorePath?.FullName));
378+
.WithRuntime(clrRuntime)
379+
.WithToolchain(CsProjClassicNetToolchain.From(clrRuntime.MsBuildMoniker, clrRuntime.Name, options.RestorePath?.FullName));
379380
case RuntimeMoniker.NetCoreApp20:
380381
case RuntimeMoniker.NetCoreApp21:
381382
case RuntimeMoniker.NetCoreApp22:
@@ -387,9 +388,10 @@ private static Job CreateJobForGivenRuntime(Job baseJob, string runtimeId, Comma
387388
case RuntimeMoniker.Net50:
388389
case RuntimeMoniker.Net60:
389390
case RuntimeMoniker.Net70:
391+
var coreRuntime = runtimeMoniker.GetRuntime();
390392
return baseJob
391-
.WithRuntime(runtimeMoniker.GetRuntime())
392-
.WithToolchain(CsProjCoreToolchain.From(new NetCoreAppSettings(runtimeId, null, runtimeId, options.CliPath?.FullName, options.RestorePath?.FullName)));
393+
.WithRuntime(coreRuntime)
394+
.WithToolchain(CsProjCoreToolchain.From(new NetCoreAppSettings(coreRuntime.MsBuildMoniker + platformSpecificPostfix, null, coreRuntime.Name + platformSpecificPostfix, options.CliPath?.FullName, options.RestorePath?.FullName)));
393395
case RuntimeMoniker.Mono:
394396
return baseJob.WithRuntime(new MonoRuntime("Mono", options.MonoPath?.FullName));
395397
case RuntimeMoniker.NativeAot60:
@@ -432,6 +434,7 @@ private static Job CreateAotJob(Job baseJob, CommandLineOptions options, Runtime
432434
builder.UseNuGet(ilCompilerVersion, nuGetFeedUrl);
433435

434436
var runtime = runtimeMoniker.GetRuntime();
437+
builder.DisplayName(runtime.Name);
435438
builder.TargetFrameworkMoniker(runtime.MsBuildMoniker);
436439

437440
return baseJob.WithRuntime(runtime).WithToolchain(builder.ToToolchain());
@@ -564,13 +567,20 @@ private static string GetCoreRunToolchainDisplayName(IReadOnlyList<FileInfo> pat
564567
return coreRunPath.FullName.Substring(lastCommonDirectorySeparatorIndex);
565568
}
566569

567-
private static bool TryParse(string runtime, out RuntimeMoniker runtimeMoniker)
570+
private static bool TryParse(string runtime, out RuntimeMoniker runtimeMoniker, out string platformSpecificPostfix)
568571
{
569572
int index = runtime.IndexOf('-');
570573

571-
return index < 0
572-
? Enum.TryParse<RuntimeMoniker>(runtime.Replace(".", string.Empty), ignoreCase: true, out runtimeMoniker)
573-
: Enum.TryParse<RuntimeMoniker>(runtime.Substring(0, index).Replace(".", string.Empty), ignoreCase: true, out runtimeMoniker);
574+
if (index < 0)
575+
{
576+
platformSpecificPostfix = "";
577+
return Enum.TryParse<RuntimeMoniker>(runtime.Replace(".", string.Empty), ignoreCase: true, out runtimeMoniker);
578+
}
579+
else
580+
{
581+
platformSpecificPostfix = runtime.Substring(index).ToLower();
582+
return Enum.TryParse<RuntimeMoniker>(runtime.Substring(0, index).Replace(".", string.Empty), ignoreCase: true, out runtimeMoniker);
583+
}
574584
}
575585
}
576586
}

src/BenchmarkDotNet/Toolchains/CsProj/CsProjClassicNetToolchain.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ private CsProjClassicNetToolchain(string targetFrameworkMoniker, string name, st
3030
{
3131
}
3232

33-
public static IToolchain From(string targetFrameworkMoniker, string packagesPath = null)
34-
=> new CsProjClassicNetToolchain(targetFrameworkMoniker, targetFrameworkMoniker, packagesPath);
33+
public static IToolchain From(string targetFrameworkMoniker, string name, string packagesPath = null)
34+
=> new CsProjClassicNetToolchain(targetFrameworkMoniker, name, packagesPath);
3535

3636
public override bool IsSupported(BenchmarkCase benchmarkCase, ILogger logger, IResolver resolver)
3737
{

src/BenchmarkDotNet/Toolchains/ToolchainExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ internal static IToolchain GetToolchain(this Runtime runtime, Descriptor descrip
3838
if (RuntimeInformation.IsNetCore || preferMsBuildToolchains)
3939
return clrRuntime.RuntimeMoniker != RuntimeMoniker.NotRecognized
4040
? GetToolchain(clrRuntime.RuntimeMoniker)
41-
: CsProjClassicNetToolchain.From(clrRuntime.MsBuildMoniker);
41+
: CsProjClassicNetToolchain.From(clrRuntime.MsBuildMoniker, clrRuntime.Name);
4242

4343
return RoslynToolchain.Instance;
4444

tests/BenchmarkDotNet.Tests/ConfigParserTests.cs

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -370,22 +370,39 @@ public void NetFrameworkMonikerParsedCorrectly(string tfm)
370370
}
371371

372372
[Theory]
373-
[InlineData("net50")]
374-
[InlineData("net60")]
375-
[InlineData("net70")]
376-
public void NetMonikersAreRecognizedAsNetCoreMonikers(string tfm)
373+
[InlineData("net50", "net5.0")]
374+
[InlineData("net60", "net6.0")]
375+
[InlineData("net70", "net7.0")]
376+
public void NetMonikersAreRecognizedAsNetCoreMonikers(string input, string expected)
377377
{
378-
var config = ConfigParser.Parse(new[] { "-r", tfm }, new OutputLogger(Output)).config;
378+
var config = ConfigParser.Parse(new[] { "-r", input }, new OutputLogger(Output)).config;
379379

380380
Assert.Single(config.GetJobs());
381381
CsProjCoreToolchain toolchain = config.GetJobs().Single().GetToolchain() as CsProjCoreToolchain;
382382
Assert.NotNull(toolchain);
383-
Assert.Equal(tfm, ((DotNetCliGenerator)toolchain.Generator).TargetFrameworkMoniker);
383+
Assert.Equal(expected, ((DotNetCliGenerator)toolchain.Generator).TargetFrameworkMoniker);
384+
}
385+
386+
[Theory]
387+
[InlineData("net481", ".NET Framework 4.8.1")]
388+
[InlineData("net7.0", ".NET 7.0")]
389+
[InlineData("nativeaot7.0", "NativeAOT 7.0")]
390+
public void NetMonikersHaveNiceNames(string input, string expected)
391+
{
392+
// CoreRuntime.
393+
var config = ConfigParser.Parse(new[] { "-r", input }, new OutputLogger(Output)).config;
394+
395+
Assert.Single(config.GetJobs());
396+
var job = config.GetJobs().Single();
397+
Assert.NotNull(job);
398+
Assert.Equal(expected, job.Environment.Runtime.Name);
399+
Assert.Equal(expected, job.Infrastructure.Toolchain.Name);
384400
}
385401

386402
[Theory]
387403
[InlineData("net5.0-windows")]
388404
[InlineData("net5.0-ios")]
405+
[InlineData("net5.0-ios15.0")]
389406
public void PlatformSpecificMonikersAreSupported(string msBuildMoniker)
390407
{
391408
var config = ConfigParser.Parse(new[] { "-r", msBuildMoniker }, new OutputLogger(Output)).config;
@@ -396,6 +413,21 @@ public void PlatformSpecificMonikersAreSupported(string msBuildMoniker)
396413
Assert.Equal(msBuildMoniker, ((DotNetCliGenerator)toolchain.Generator).TargetFrameworkMoniker);
397414
}
398415

416+
[Theory]
417+
[InlineData("net7.0-ios", ".NET 7.0-ios")]
418+
[InlineData("net7.0-IOS", ".NET 7.0-ios")]
419+
[InlineData("net7.0-ios15.0", ".NET 7.0-ios15.0")]
420+
public void PlatformSpecificMonikersHaveNiceNames(string input, string expected)
421+
{
422+
var config = ConfigParser.Parse(new[] { "-r", input }, new OutputLogger(Output)).config;
423+
424+
Assert.Single(config.GetJobs());
425+
var job = config.GetJobs().Single();
426+
Assert.NotNull(job);
427+
//todo: add postfix to runtime name instead toolchain name
428+
Assert.Equal(expected, job.Infrastructure.Toolchain.Name);
429+
}
430+
399431
[Fact]
400432
public void CanCompareFewDifferentRuntimes()
401433
{

0 commit comments

Comments
 (0)