Skip to content

Allow for benchmark categories composition using inheritance #2306

@BladeWise

Description

@BladeWise

I am writing some benchmarks over generic classes, and to do so I created base generic benchmark, something along these lines

[BenchmarkCategory("Number")]
public class MyBenchmark<T> where T : unmanaged, INumberBase<T>
{
    public T V1;
    public T V2;
    
    [BenchmarkCategory("Add")]
    public T Add()
    {
         return V1 + V2;
    }

    [BenchmarkCategory("Multiply")]
    public T Multiply()
    {
         return V1 * V2;
    }

   [GlobalSetup]
   public virtual void Setup()
   {
         V1 = T.One + T.One;
         V2 = T.One + T.One + T.One;
   }
}

Actual benchmarks are specialized over the generic type

[BenchmarkCategory("Float")]
public class MyFloatBenchmark : MyBenchmark<float>
{
    [BenchmarkCategory("Sqrt")]
    public float Sqrt()
    {
        return float.Sqrt(V1);
    }
}

The problem is that the category defined on the derived class is not assigned to the methods declared on the base class, because BenchmarkConverter only looks at the method declaring type. Moreover, the attribute on the derived class replaces the one defined on the base class, because the current resolution strategy ignores inherited attributes.

Is there a reason why method.ReflectedType is not used in this case and inherited attributes are explicitly ignored (both at the class and method level)?

So far, the only option is to replicate the categories on both the derived class and overridden method.

Would it be possible to add an extension point to the BenchmarkConverter (e.g., an ICategoryResolver or a CategoryResolver delegate) to plug-in a different categories discovery logic?

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions