Skip to content

Commit

Permalink
Enabled category inheritance, fix #2306
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreyAkinshin committed May 14, 2023
1 parent f32a2e7 commit 9602893
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace BenchmarkDotNet.Attributes
{
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly)]
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly, AllowMultiple = true)]
public class BenchmarkCategoryAttribute : Attribute
{
public string[] Categories { get; }
Expand Down
6 changes: 3 additions & 3 deletions src/BenchmarkDotNet/Running/BenchmarkConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,11 @@ private static IEnumerable<ParameterInstances> GetArgumentsDefinitions(MethodInf
private static string[] GetCategories(MethodInfo method)
{
var attributes = new List<BenchmarkCategoryAttribute>();
attributes.AddRange(method.GetCustomAttributes(typeof(BenchmarkCategoryAttribute), false).OfType<BenchmarkCategoryAttribute>());
var type = method.DeclaringType;
attributes.AddRange(method.GetCustomAttributes(typeof(BenchmarkCategoryAttribute), true).OfType<BenchmarkCategoryAttribute>());
var type = method.ReflectedType;
if (type != null)
{
attributes.AddRange(type.GetTypeInfo().GetCustomAttributes(typeof(BenchmarkCategoryAttribute), false).OfType<BenchmarkCategoryAttribute>());
attributes.AddRange(type.GetTypeInfo().GetCustomAttributes(typeof(BenchmarkCategoryAttribute), true).OfType<BenchmarkCategoryAttribute>());
attributes.AddRange(type.GetTypeInfo().Assembly.GetCustomAttributes().OfType<BenchmarkCategoryAttribute>());
}
if (attributes.Count == 0)
Expand Down
55 changes: 55 additions & 0 deletions tests/BenchmarkDotNet.Tests/Configs/CategoriesTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.Linq;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using Xunit;
using Xunit.Abstractions;

namespace BenchmarkDotNet.Tests.Configs
{
public class CategoriesTests
{
private readonly ITestOutputHelper output;

public CategoriesTests(ITestOutputHelper output)
{
this.output = output;
}

[Fact]
public void CategoryInheritanceTest()
{
string Format(BenchmarkCase benchmarkCase) =>
benchmarkCase.Descriptor.WorkloadMethod.Name + ": " +
string.Join("+", benchmarkCase.Descriptor.Categories.OrderBy(category => category));

var benchmarkCases = BenchmarkConverter
.TypeToBenchmarks(typeof(DerivedClass))
.BenchmarksCases
.OrderBy(x => x.Descriptor.WorkloadMethod.Name)
.ToList();
Assert.Equal(2, benchmarkCases.Count);

output.WriteLine(Format(benchmarkCases[0]));
output.WriteLine(Format(benchmarkCases[1]));

Assert.Equal("BaseMethod: BaseClassCategory+BaseMethodCategory+DerivedClassCategory", Format(benchmarkCases[0]));
Assert.Equal("DerivedMethod: BaseClassCategory+DerivedClassCategory+DerivedMethodCategory", Format(benchmarkCases[1]));
}

[BenchmarkCategory("BaseClassCategory")]
public class BaseClass
{
[Benchmark]
[BenchmarkCategory("BaseMethodCategory")]
public void BaseMethod() { }
}

[BenchmarkCategory("DerivedClassCategory")]
public class DerivedClass : BaseClass
{
[Benchmark]
[BenchmarkCategory("DerivedMethodCategory")]
public void DerivedMethod() { }
}
}
}

0 comments on commit 9602893

Please sign in to comment.