Class BenchmarkProgram
- Namespace
- Codebelt.Extensions.BenchmarkDotNet.Console
- Assembly
- Codebelt.Extensions.BenchmarkDotNet.Console.dll
Entry point helper for hosting and running benchmarks using BenchmarkDotNet.
public class BenchmarkProgram : MinimalConsoleProgram<BenchmarkProgram>
- Inheritance
-
BenchmarkProgram
- Inherited Members
Examples
The following example shows the typical Program.cs of a benchmark host project: the entry point forwards the command-line arguments to BenchmarkProgram.RunAsync and supplies a setup delegate that customizes the resolved BenchmarkWorkspaceOptions before the host is built. BenchmarkProgram derives from Codebelt.Bootstrapper.Console.MinimalConsoleProgram<BenchmarkProgram>, so the Main plumbing — host configuration, service registration, lifecycle — is inherited; the only thing the host project has to write is the call site and the workspace configuration.
using System.Threading.Tasks;
using Codebelt.Extensions.BenchmarkDotNet;
using Codebelt.Extensions.BenchmarkDotNet.Console;
namespace MyBenchmarks;
public static class Program
{
// minimal benchmark host: forwards args into the auto-discovered benchmark workspace
public static Task Main(string[] args) => BenchmarkProgram.RunAsync(args, setup: options =>
{
options.BenchmarkProjectSuffix = "MyBench";
options.AllowDebugBuild = false;
options.SkipBenchmarksWithReports = true;
});
}
Properties
BuildConfiguration
Gets the build configuration of the entry assembly.
public static string BuildConfiguration { get; }
Property Value
- string
The value is either
DebugorRelease.
IsDebugBuild
Gets a value indicating whether the entry assembly was built in Debug configuration.
public static bool IsDebugBuild { get; }
Property Value
- bool
trueif the entry assembly was compiled with debugging information; otherwise,false.
Methods
Run(string[], Action<BenchmarkWorkspaceOptions>)
Runs benchmarks using the default BenchmarkWorkspace implementation.
public static void Run(string[] args, Action<BenchmarkWorkspaceOptions> setup = null)
Parameters
argsstring[]The command-line arguments passed to the application.
setupAction<BenchmarkWorkspaceOptions>The BenchmarkWorkspaceOptions which may be configured.
Remarks
This method configures the host builder with the necessary services, builds the host, and runs it to execute benchmarks.
Run(string[], Action<IServiceCollection>, Action<BenchmarkWorkspaceOptions>)
Runs benchmarks using the default BenchmarkWorkspace implementation.
public static void Run(string[] args, Action<IServiceCollection> serviceConfigurator = null, Action<BenchmarkWorkspaceOptions> setup = null)
Parameters
argsstring[]The command-line arguments passed to the application.
serviceConfiguratorAction<IServiceCollection>The delegate that will be invoked to configure additional services in the IServiceCollection.
setupAction<BenchmarkWorkspaceOptions>The BenchmarkWorkspaceOptions which may be configured.
Remarks
This method configures the host builder with the necessary services, builds the host, and runs it to execute benchmarks.
RunAsync(IServiceProvider, CancellationToken)
Runs the actual benchmarks as envisioned by BenchmarkDotNet.
public override Task RunAsync(IServiceProvider serviceProvider, CancellationToken cancellationToken)
Parameters
serviceProviderIServiceProviderThe service provider.
cancellationTokenCancellationTokenThe cancellation token.
Returns
- Task
A completed task when benchmark execution has finished.
Remarks
When arguments are provided, they are forwarded to BenchmarkDotNet.Running.BenchmarkSwitcher for selective execution. After execution completes, artifact post-processing is performed.
RunAsync(string[], Action<BenchmarkWorkspaceOptions>)
Runs benchmarks asynchronously using the default BenchmarkWorkspace implementation.
public static Task RunAsync(string[] args, Action<BenchmarkWorkspaceOptions> setup = null)
Parameters
argsstring[]The command-line arguments passed to the application.
setupAction<BenchmarkWorkspaceOptions>The BenchmarkWorkspaceOptions which may be configured.
Returns
- Task
A task that represents the asynchronous benchmark host operation.
Remarks
This method configures the host builder with the necessary services, builds the host, and runs it asynchronously to execute benchmarks.
RunAsync(string[], Action<IServiceCollection>, Action<BenchmarkWorkspaceOptions>)
Runs benchmarks asynchronously using the default BenchmarkWorkspace implementation.
public static Task RunAsync(string[] args, Action<IServiceCollection> serviceConfigurator = null, Action<BenchmarkWorkspaceOptions> setup = null)
Parameters
argsstring[]The command-line arguments passed to the application.
serviceConfiguratorAction<IServiceCollection>The delegate that will be invoked to configure additional services in the IServiceCollection.
setupAction<BenchmarkWorkspaceOptions>The BenchmarkWorkspaceOptions which may be configured.
Returns
- Task
A task that represents the asynchronous benchmark host operation.
Remarks
This method configures the host builder with the necessary services, builds the host, and runs it asynchronously to execute benchmarks.
RunAsync<TWorkspace>(string[], Action<BenchmarkWorkspaceOptions>)
Runs benchmarks asynchronously using a custom implementation of IBenchmarkWorkspace.
public static Task RunAsync<TWorkspace>(string[] args, Action<BenchmarkWorkspaceOptions> setup = null) where TWorkspace : class, IBenchmarkWorkspace
Parameters
argsstring[]The command-line arguments passed to the application.
setupAction<BenchmarkWorkspaceOptions>The BenchmarkWorkspaceOptions which may be configured.
Returns
- Task
A task that represents the asynchronous benchmark host operation.
Type Parameters
TWorkspaceThe type of the workspace that implements IBenchmarkWorkspace.
Remarks
This method configures the host builder with the necessary services, builds the host, and runs it asynchronously to execute benchmarks.
RunAsync<TWorkspace>(string[], Action<IServiceCollection>, Action<BenchmarkWorkspaceOptions>)
Runs benchmarks asynchronously using a custom implementation of IBenchmarkWorkspace.
public static Task RunAsync<TWorkspace>(string[] args, Action<IServiceCollection> serviceConfigurator = null, Action<BenchmarkWorkspaceOptions> setup = null) where TWorkspace : class, IBenchmarkWorkspace
Parameters
argsstring[]The command-line arguments passed to the application.
serviceConfiguratorAction<IServiceCollection>The delegate that will be invoked to configure additional services in the IServiceCollection.
setupAction<BenchmarkWorkspaceOptions>The BenchmarkWorkspaceOptions which may be configured.
Returns
- Task
A task that represents the asynchronous benchmark host operation.
Type Parameters
TWorkspaceThe type of the workspace that implements IBenchmarkWorkspace.
Remarks
This method configures the host builder with the necessary services, builds the host, and runs it asynchronously to execute benchmarks.
Run<TWorkspace>(string[], Action<BenchmarkWorkspaceOptions>)
Runs benchmarks using a custom implementation of IBenchmarkWorkspace.
public static void Run<TWorkspace>(string[] args, Action<BenchmarkWorkspaceOptions> setup = null) where TWorkspace : class, IBenchmarkWorkspace
Parameters
argsstring[]The command-line arguments passed to the application.
setupAction<BenchmarkWorkspaceOptions>The BenchmarkWorkspaceOptions which may be configured.
Type Parameters
TWorkspaceThe type of the workspace that implements IBenchmarkWorkspace.
Remarks
This method configures the host builder with the necessary services, builds the host, and runs it to execute benchmarks.
Run<TWorkspace>(string[], Action<IServiceCollection>, Action<BenchmarkWorkspaceOptions>)
Runs benchmarks using a custom implementation of IBenchmarkWorkspace.
public static void Run<TWorkspace>(string[] args, Action<IServiceCollection> serviceConfigurator = null, Action<BenchmarkWorkspaceOptions> setup = null) where TWorkspace : class, IBenchmarkWorkspace
Parameters
argsstring[]The command-line arguments passed to the application.
serviceConfiguratorAction<IServiceCollection>The delegate that will be invoked to configure additional services in the IServiceCollection.
setupAction<BenchmarkWorkspaceOptions>The BenchmarkWorkspaceOptions which may be configured.
Type Parameters
TWorkspaceThe type of the workspace that implements IBenchmarkWorkspace.
Remarks
This method configures the host builder with the necessary services, builds the host, and runs it to execute benchmarks.