Class BenchmarkWorkspaceOptionsExtensions
- Namespace
- Codebelt.Extensions.BenchmarkDotNet
- Assembly
- Codebelt.Extensions.BenchmarkDotNet.dll
Extension methods for the BenchmarkWorkspaceOptions class.
public static class BenchmarkWorkspaceOptionsExtensions
- Inheritance
-
BenchmarkWorkspaceOptionsExtensions
Examples
The following example shows ConfigureBenchmarkDotNet being used to add a second BenchmarkDotNet job to the default IConfig carried by BenchmarkWorkspaceOptions. The helper takes care of forcing the default configuration, passing the current IConfig to the delegate, and assigning the returned configuration back onto the options instance — which is otherwise awkward because BenchmarkDotNet's AddJob / AddColumn / AddDiagnoser methods return a new configuration object rather than mutating the receiver.
using System;
using System.Linq;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Jobs;
using Codebelt.Extensions.BenchmarkDotNet;
using Perfolizer.Horology;
namespace MyBenchmarks;
public static class Program
{
public static void Main()
{
var options = new BenchmarkWorkspaceOptions();
// fluent IConfig mutations normally require explicit reassignment; this helper does it for you
options.ConfigureBenchmarkDotNet(c => c.AddJob(
Job.Default
.WithWarmupCount(2)
.WithIterationTime(TimeInterval.FromMilliseconds(500))
.WithMaxIterationCount(25)
.WithId("LongRunning")));
Console.WriteLine($"Jobs: {string.Join(", ", options.Configuration.GetJobs().Select(j => j.Id))}");
}
}
Methods
ConfigureBenchmarkDotNet(BenchmarkWorkspaceOptions, Func<IConfig, IConfig>)
Configures the BenchmarkDotNet configuration for the specified options.
public static BenchmarkWorkspaceOptions ConfigureBenchmarkDotNet(this BenchmarkWorkspaceOptions options, Func<IConfig, IConfig> configure)
Parameters
optionsBenchmarkWorkspaceOptionsThe BenchmarkWorkspaceOptions to extend.
configureFunc<IConfig, IConfig>The function delegate that configures the BenchmarkDotNet.Configs.IConfig.
Returns
- BenchmarkWorkspaceOptions
The
optionsinstance for chaining.
Remarks
BenchmarkDotNet's configuration model is intentionally immutable-ish: methods such as
AddJob, AddColumn, and AddDiagnoser do not mutate the
incoming BenchmarkDotNet.Configs.IConfig instance. Instead, they produce and return a new
configuration object. This behavior is powerful but can be unintuitive when used inside
option delegates where callers naturally expect fluent configuration to modify the
underlying options instance.
Without this helper, callers would need to explicitly reassign:
options.Configuration = options.Configuration.AddJob(job);
This extension method abstracts away that requirement by:
- Forcing initialization of the default configuration if needed,
- Passing the current configuration to the delegate for fluent BenchmarkDotNet operations,
- Assigning the delegate’s returned configuration back to Configuration.
This preserves BenchmarkDotNet's design while providing an intuitive experience for users configuring BenchmarkWorkspaceOptions via delegates.
Exceptions
- ArgumentNullException
optionscannot be null -or-configurecannot be null.- InvalidOperationException
configuremust not return null.- ArgumentNullException
optionscannot be null -or-configurecannot be null.- InvalidOperationException
configuremust not return null.