Skip to content

Commit a0cadb9

Browse files
committed
chore: update samples project
1 parent 0818e53 commit a0cadb9

File tree

2 files changed

+105
-5
lines changed

2 files changed

+105
-5
lines changed
Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,75 @@
1-
using BenchmarkDotNet.Running;
1+
using BenchmarkDotNet.Configs;
2+
using BenchmarkDotNet.Engines;
3+
using BenchmarkDotNet.Jobs;
4+
using BenchmarkDotNet.Loggers;
5+
using BenchmarkDotNet.Reports;
6+
using BenchmarkDotNet.Running;
7+
using BenchmarkDotNet.Toolchains.InProcess.Emit;
8+
using System;
9+
using System.Collections.Generic;
10+
using System.Linq;
211

3-
namespace BenchmarkDotNet.Samples
12+
namespace BenchmarkDotNet.Samples;
13+
14+
public class Program
415
{
5-
public class Program
16+
public static int Main(string[] args)
617
{
7-
public static void Main(string[] args) => BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
18+
#if DEBUG
19+
ConsoleLogger.Default.WriteLineWarning("Benchmark is executed with DEBUG configuration.");
20+
ConsoleLogger.Default.WriteLine();
21+
#endif
22+
23+
if (args.Length != 0)
24+
{
25+
ConsoleLogger.Default.WriteLine($"Start benchmarks with args: {string.Join(" ", args)}");
26+
ConsoleLogger.Default.WriteLine();
27+
}
28+
29+
IConfig config;
30+
#if DEBUG
31+
config = GetDebugConfig();
32+
#else
33+
config = null; // `DefaultConfig.Instance` is used.
34+
#endif
35+
36+
var summaries = BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly)
37+
.Run(args, config)
38+
.ToArray();
39+
40+
if (summaries.HasError())
41+
return 1;
42+
43+
return 0;
44+
}
45+
46+
private static ManualConfig GetDebugConfig()
47+
{
48+
return DefaultConfig.Instance
49+
.WithOptions(ConfigOptions.DisableOptimizationsValidator)
50+
.WithOptions(ConfigOptions.StopOnFirstError)
51+
.AddJob(
52+
Job.Default
53+
.WithId("WithDebugConfiguration")
54+
.WithToolchain(InProcessEmitToolchain.Instance)
55+
.WithStrategy(RunStrategy.Monitoring)
56+
);
57+
}
58+
}
59+
60+
file static class ExtensionMethods
61+
{
62+
public static bool HasError(this Summary[] summaries)
63+
{
64+
if (summaries.Length == 0)
65+
{
66+
var hashSet = new HashSet<string>(["--help", "--list", "--info", "--version"]);
67+
return !Environment.GetCommandLineArgs().Any(hashSet.Contains);
68+
}
69+
70+
if (summaries.Any(x => x.HasCriticalValidationErrors))
71+
return true;
72+
73+
return summaries.Any(x => x.Reports.Any(r => !r.Success));
874
}
9-
}
75+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"profiles": {
3+
"Default": {
4+
"commandName": "Project",
5+
"commandLineArgs": "",
6+
"workingDirectory": ".",
7+
"environmentVariables": {
8+
}
9+
},
10+
"Run IntroBasic Benchmarks": {
11+
"commandName": "Project",
12+
"commandLineArgs": "--filter *IntroBasic*",
13+
"workingDirectory": ".",
14+
"environmentVariables": {
15+
}
16+
},
17+
"--list": {
18+
"commandName": "Project",
19+
"commandLineArgs": "--list"
20+
},
21+
"--info": {
22+
"commandName": "Project",
23+
"commandLineArgs": "--info"
24+
},
25+
"--help": {
26+
"commandName": "Project",
27+
"commandLineArgs": "--help"
28+
},
29+
"--version": {
30+
"commandName": "Project",
31+
"commandLineArgs": "--version"
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)