Skip to content

Commit 848196a

Browse files
authored
chore: Update BenchmarkDotNet.Samples project (dotnet#2898)
* chore: update samples project * chore-add-support-DebugBuildConfig * chore: fix ThreadingDiagnoser error when running benchmark on .NET Framework * chore: fix wrong #if directive * chore: reflect review comments and cleanup codes
1 parent 53d4963 commit 848196a

File tree

3 files changed

+107
-5
lines changed

3 files changed

+107
-5
lines changed
Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,58 @@
1-
using BenchmarkDotNet.Running;
1+
using BenchmarkDotNet.Configs;
2+
using BenchmarkDotNet.Engines;
3+
using BenchmarkDotNet.Exporters;
4+
using BenchmarkDotNet.Extensions;
5+
using BenchmarkDotNet.Loggers;
6+
using BenchmarkDotNet.Running;
7+
using System;
8+
using System.Linq;
29

3-
namespace BenchmarkDotNet.Samples
10+
namespace BenchmarkDotNet.Samples;
11+
12+
public class Program
413
{
5-
public class Program
14+
public static int Main(string[] args)
615
{
7-
public static void Main(string[] args) => BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
16+
#if DEBUG
17+
ConsoleLogger.Default.WriteLineWarning("Benchmark is executed with DEBUG configuration.");
18+
ConsoleLogger.Default.WriteLine();
19+
#endif
20+
21+
if (args.Length != 0)
22+
{
23+
ConsoleLogger.Default.WriteLine($"Start benchmarks with args: {string.Join(" ", args)}");
24+
ConsoleLogger.Default.WriteLine();
25+
}
26+
27+
IConfig? config = GetConfig(ref args);
28+
29+
var summaries = BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly)
30+
.Run(args, config)
31+
.ToArray();
32+
33+
if (summaries.HasError())
34+
return 1;
35+
36+
return 0;
37+
}
38+
39+
private static IConfig? GetConfig(ref string[] args)
40+
{
41+
#if !DEBUG
42+
return null; // `DefaultConfig.Instance` is used.
43+
#else
44+
bool isInProcess = args.Contains("--inProcess");
45+
if (isInProcess)
46+
args = args.Where(x => x != "--inProcess").ToArray();
47+
48+
DebugConfig config = isInProcess
49+
? new DebugInProcessConfig()
50+
: new DebugBuildConfig();
51+
52+
return config.AddAnalyser(DefaultConfig.Instance.GetAnalysers().ToArray())
53+
.AddExporter(MarkdownExporter.Default)
54+
.AddValidator(DefaultConfig.Instance.GetValidators().ToArray())
55+
.WithArtifactsPath(DefaultConfig.Instance.ArtifactsPath);
56+
#endif
857
}
9-
}
58+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"profiles": {
3+
"Default": {
4+
"commandName": "Project",
5+
"commandLineArgs": "",
6+
"environmentVariables": {
7+
}
8+
},
9+
"Run IntroBasic Benchmarks": {
10+
"commandName": "Project",
11+
"commandLineArgs": "--filter *IntroBasic*",
12+
"environmentVariables": {
13+
}
14+
},
15+
"Run IntroBasic Benchmarks with --inProcess": {
16+
"commandName": "Project",
17+
"commandLineArgs": "--filter *IntroBasic* --inProcess",
18+
"environmentVariables": {
19+
}
20+
},
21+
"--list": {
22+
"commandName": "Project",
23+
"commandLineArgs": "--list"
24+
},
25+
"--info": {
26+
"commandName": "Project",
27+
"commandLineArgs": "--info"
28+
},
29+
"--help": {
30+
"commandName": "Project",
31+
"commandLineArgs": "--help"
32+
},
33+
"--version": {
34+
"commandName": "Project",
35+
"commandLineArgs": "--version"
36+
}
37+
}
38+
}

src/BenchmarkDotNet/Extensions/ReportExtensions.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,20 @@ public static Statistics GetStatistics(this IReadOnlyCollection<Measurement> run
3434

3535
public static Statistics GetStatistics(this IEnumerable<Measurement> runs) =>
3636
GetStatistics(runs.ToList());
37+
38+
public static bool HasError(this IEnumerable<Summary> summaries)
39+
{
40+
if (summaries.Count() == 0)
41+
{
42+
// When following argument specified. BenchmarkDotNet show information only.
43+
var knownArguments = new HashSet<string>(["--help", "--list", "--info", "--version"]);
44+
return !Environment.GetCommandLineArgs().Any(knownArguments.Contains);
45+
}
46+
47+
if (summaries.Any(x => x.HasCriticalValidationErrors))
48+
return true;
49+
50+
return summaries.Any(x => x.Reports.Any(r => !r.Success));
51+
}
3752
}
3853
}

0 commit comments

Comments
 (0)