Total Number of Benchmarks #2564
RealDotNetDave
started this conversation in
General
Replies: 1 comment
-
Something like this. This was adapted from the internal source code. You will need to account for the generic benchmarks count if you need it. public static class BenchmarkHelper
{
public static int GetRunnableBenchmarksCount(this Assembly assembly)
=> assembly
.GetTypes()
.Where(type => type.ContainsRunnableBenchmarks())
.SelectMany(type => type.GetTypeInfo().GetBenchmarks())
.Count();
private static bool ContainsRunnableBenchmarks(this Type type)
{
var typeInfo = type.GetTypeInfo();
if (typeInfo.IsAbstract
|| typeInfo.IsSealed
|| typeInfo.IsNotPublic
|| typeInfo.IsGenericType && !IsRunnableGenericType(typeInfo))
return false;
return typeInfo.GetBenchmarks().Any();
}
private static MethodInfo[] GetBenchmarks(this TypeInfo typeInfo)
=> typeInfo
.GetMethods(BindingFlags.Instance | BindingFlags.Public)
.Where(method => method.GetCustomAttributes(true).OfType<BenchmarkAttribute>().Any())
.ToArray();
private static bool IsRunnableGenericType(TypeInfo typeInfo)
=> (!typeInfo.IsGenericTypeDefinition || typeInfo.GenericTypeArguments.Any() || typeInfo.GetCustomAttributes(true).OfType<GenericTypeArgumentsAttribute>().Any())
&& typeInfo.DeclaredConstructors.Any(ctor => ctor.IsPublic && ctor.GetParameters().Length == 0);
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Is there a way to get the total benchmark count for a project?
Beta Was this translation helpful? Give feedback.
All reactions