When writing unit tests, we can add more test cases by simply adding elements to a collection, for example using TestCaseSource
in NUnit.
Is it possible to do something similar in BenchmarkDotNet, and create a set of benchmarks from a collection?
This would save a lot of boilerplate, especially when benchmarking multiple combinations of inputs or doing additional testing of the benchmarked methods.
Minimal example code:
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
public interface IAlgorithm
{
public void DoWork();
}
public class AlgorithmA: IAlgorithm
{
public void DoWork() { } // something slow
}
public class AlgorithmB : IAlgorithm
{
public void DoWork() { } // something slow
}
public class MyBenchmarks
{
AlgorithmA classA = new AlgorithmA();
AlgorithmB classB = new AlgorithmB();
[Benchmark]
public void A() => classA.DoWork();
[Benchmark]
public void B() => classB.DoWork();
}
public class WhatIWouldLike
{
IAlgorithm[] classes = new IAlgorithm[] { new AlgorithmA(), new AlgorithmB() };
// ...automatically create a benchmark for DoWork() on element of the array
}
class Program
{
static void Main(string[] args)
{
BenchmarkRunner.Run<MyBenchmarks>();
}
}