Skip to content

Instantly share code, notes, and snippets.

@cbarraco
Last active November 7, 2024 18:52
Show Gist options
  • Save cbarraco/4b478834c8a474fb0472d05ba1f870ab to your computer and use it in GitHub Desktop.
Save cbarraco/4b478834c8a474fb0472d05ba1f870ab to your computer and use it in GitHub Desktop.
Discovers all classes in the assembly that have [Benchmark] attributes and runs them with BenchmarkRunner (BenchmarkDotNet)
static Type[] GetAllTypesThatHaveBenchmarkMethods()
{
return Assembly.GetExecutingAssembly().GetTypes().Where(
type => type.GetMethods().Any(
method => method.GetCustomAttributes(typeof(BenchmarkAttribute)).Any()
)
).ToArray();
}
var benchmarkTypes = GetAllTypesThatHaveBenchmarkMethods();
foreach (var type in benchmarkTypes)
{
Console.WriteLine($"Benchmarking {type.FullName}");
BenchmarkRunner.Run(type);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment