Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
86 changes: 81 additions & 5 deletions samples/BenchmarkDotNet.Samples/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,85 @@
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Engines;
using BenchmarkDotNet.Exporters;
using BenchmarkDotNet.Loggers;
using BenchmarkDotNet.Reports;
using BenchmarkDotNet.Running;
using System;
using System.Collections.Generic;
using System.Linq;

namespace BenchmarkDotNet.Samples
namespace BenchmarkDotNet.Samples;

public class Program
{
public class Program
public static int Main(string[] args)
{
#if DEBUG
ConsoleLogger.Default.WriteLineWarning("Benchmark is executed with DEBUG configuration.");
ConsoleLogger.Default.WriteLine();
#endif

if (args.Length != 0)
{
ConsoleLogger.Default.WriteLine($"Start benchmarks with args: {string.Join(" ", args)}");
ConsoleLogger.Default.WriteLine();
}

IConfig? config = GetConfig(ref args);

var summaries = BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly)
.Run(args, config)
.ToArray();

if (summaries.HasError())
return 1;

return 0;
}

private static IConfig? GetConfig(ref string[] args)
{
public static void Main(string[] args) => BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
#if !DEBUG
return null; // `DefaultConfig.Instance` is used.
#else
bool isInProcess = args.Contains("--inProcess");
if (isInProcess)
args = args.Where(x => x != "--inProcess").ToArray();

DebugConfig config = isInProcess
? new DebugInProcessConfig()
: new DebugBuildConfig();

return config.AddAnalyser(DefaultConfig.Instance.GetAnalysers().ToArray())
.AddDiagnoser(
MemoryDiagnoser.Default,
#if NET30_OR_GREATER
new ThreadingDiagnoser(new ThreadingDiagnoserConfig(displayCompletedWorkItemCountWhenZero: false, displayLockContentionWhenZero: false)),
#endif
new ExceptionDiagnoser(new ExceptionDiagnoserConfig(displayExceptionsIfZeroValue: false))
)
.AddExporter(MarkdownExporter.Default)
.AddValidator(DefaultConfig.Instance.GetValidators().ToArray())
.WithArtifactsPath(DefaultConfig.Instance.ArtifactsPath);
#endif
}
}

file static class ExtensionMethods
{
public static bool HasError(this Summary[] summaries)
{
if (summaries.Length == 0)
{
var hashSet = new HashSet<string>(["--help", "--list", "--info", "--version"]);
return !Environment.GetCommandLineArgs().Any(hashSet.Contains);
}

if (summaries.Any(x => x.HasCriticalValidationErrors))
return true;

return summaries.Any(x => x.Reports.Any(r => !r.Success));
}
}
}
35 changes: 35 additions & 0 deletions samples/BenchmarkDotNet.Samples/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"profiles": {
"Default": {
"commandName": "Project",
"commandLineArgs": "",
"workingDirectory": ".",
"environmentVariables": {
}
},
"Run IntroBasic Benchmarks": {
"commandName": "Project",
"commandLineArgs": "--filter *IntroBasic*",
// "commandLineArgs": "--filter *IntroBasic* --inProcess --strategy Monitoring", // Use this setting for in-process debug
"workingDirectory": ".",
"environmentVariables": {
}
},
"--list": {
"commandName": "Project",
"commandLineArgs": "--list"
},
"--info": {
"commandName": "Project",
"commandLineArgs": "--info"
},
"--help": {
"commandName": "Project",
"commandLineArgs": "--help"
},
"--version": {
"commandName": "Project",
"commandLineArgs": "--version"
}
}
}