Skip to content

Commit 9602893

Browse files
Enabled category inheritance, fix #2306
1 parent f32a2e7 commit 9602893

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

src/BenchmarkDotNet.Annotations/Attributes/BenchmarkCategoryAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace BenchmarkDotNet.Attributes
55
{
6-
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly)]
6+
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly, AllowMultiple = true)]
77
public class BenchmarkCategoryAttribute : Attribute
88
{
99
public string[] Categories { get; }

src/BenchmarkDotNet/Running/BenchmarkConverter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,11 @@ private static IEnumerable<ParameterInstances> GetArgumentsDefinitions(MethodInf
248248
private static string[] GetCategories(MethodInfo method)
249249
{
250250
var attributes = new List<BenchmarkCategoryAttribute>();
251-
attributes.AddRange(method.GetCustomAttributes(typeof(BenchmarkCategoryAttribute), false).OfType<BenchmarkCategoryAttribute>());
252-
var type = method.DeclaringType;
251+
attributes.AddRange(method.GetCustomAttributes(typeof(BenchmarkCategoryAttribute), true).OfType<BenchmarkCategoryAttribute>());
252+
var type = method.ReflectedType;
253253
if (type != null)
254254
{
255-
attributes.AddRange(type.GetTypeInfo().GetCustomAttributes(typeof(BenchmarkCategoryAttribute), false).OfType<BenchmarkCategoryAttribute>());
255+
attributes.AddRange(type.GetTypeInfo().GetCustomAttributes(typeof(BenchmarkCategoryAttribute), true).OfType<BenchmarkCategoryAttribute>());
256256
attributes.AddRange(type.GetTypeInfo().Assembly.GetCustomAttributes().OfType<BenchmarkCategoryAttribute>());
257257
}
258258
if (attributes.Count == 0)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System.Linq;
2+
using BenchmarkDotNet.Attributes;
3+
using BenchmarkDotNet.Running;
4+
using Xunit;
5+
using Xunit.Abstractions;
6+
7+
namespace BenchmarkDotNet.Tests.Configs
8+
{
9+
public class CategoriesTests
10+
{
11+
private readonly ITestOutputHelper output;
12+
13+
public CategoriesTests(ITestOutputHelper output)
14+
{
15+
this.output = output;
16+
}
17+
18+
[Fact]
19+
public void CategoryInheritanceTest()
20+
{
21+
string Format(BenchmarkCase benchmarkCase) =>
22+
benchmarkCase.Descriptor.WorkloadMethod.Name + ": " +
23+
string.Join("+", benchmarkCase.Descriptor.Categories.OrderBy(category => category));
24+
25+
var benchmarkCases = BenchmarkConverter
26+
.TypeToBenchmarks(typeof(DerivedClass))
27+
.BenchmarksCases
28+
.OrderBy(x => x.Descriptor.WorkloadMethod.Name)
29+
.ToList();
30+
Assert.Equal(2, benchmarkCases.Count);
31+
32+
output.WriteLine(Format(benchmarkCases[0]));
33+
output.WriteLine(Format(benchmarkCases[1]));
34+
35+
Assert.Equal("BaseMethod: BaseClassCategory+BaseMethodCategory+DerivedClassCategory", Format(benchmarkCases[0]));
36+
Assert.Equal("DerivedMethod: BaseClassCategory+DerivedClassCategory+DerivedMethodCategory", Format(benchmarkCases[1]));
37+
}
38+
39+
[BenchmarkCategory("BaseClassCategory")]
40+
public class BaseClass
41+
{
42+
[Benchmark]
43+
[BenchmarkCategory("BaseMethodCategory")]
44+
public void BaseMethod() { }
45+
}
46+
47+
[BenchmarkCategory("DerivedClassCategory")]
48+
public class DerivedClass : BaseClass
49+
{
50+
[Benchmark]
51+
[BenchmarkCategory("DerivedMethodCategory")]
52+
public void DerivedMethod() { }
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)