-
-
Notifications
You must be signed in to change notification settings - Fork 960
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fe3032b
commit 2e7427c
Showing
7 changed files
with
106 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System.Threading; | ||
using BenchmarkDotNet.Attributes; | ||
using BenchmarkDotNet.Attributes.Columns; | ||
using BenchmarkDotNet.Attributes.Jobs; | ||
|
||
namespace BenchmarkDotNet.Samples.Intro | ||
{ | ||
[DryJob] | ||
[CategoriesColumn] | ||
[BenchmarkCategory("ClassCategory")] | ||
public class IntroCategories | ||
{ | ||
[Benchmark] | ||
[BenchmarkCategory("CategoryA")] | ||
public void A() => Thread.Sleep(10); | ||
|
||
[Benchmark] | ||
[BenchmarkCategory("CategoryB")] | ||
public void B() => Thread.Sleep(10); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/BenchmarkDotNet.Core/Attributes/BenchmarkCategoryAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System; | ||
|
||
namespace BenchmarkDotNet.Attributes | ||
{ | ||
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly)] | ||
public class BenchmarkCategoryAttribute : Attribute | ||
{ | ||
public string[] Categories { get; } | ||
|
||
// CLS-Compliant Code requires a constuctor without an array in the argument list | ||
protected BenchmarkCategoryAttribute() { } | ||
|
||
public BenchmarkCategoryAttribute(params string[] categories) | ||
{ | ||
Categories = categories; | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/BenchmarkDotNet.Core/Attributes/Columns/CategoriesColumnAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using BenchmarkDotNet.Columns; | ||
|
||
namespace BenchmarkDotNet.Attributes.Columns | ||
{ | ||
public class CategoriesColumnAttribute : ColumnConfigBaseAttribute | ||
{ | ||
public CategoriesColumnAttribute() : base(CategoriesColumn.Default) { } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System.Linq; | ||
using BenchmarkDotNet.Extensions; | ||
using BenchmarkDotNet.Reports; | ||
using BenchmarkDotNet.Running; | ||
|
||
namespace BenchmarkDotNet.Columns | ||
{ | ||
public class CategoriesColumn : IColumn | ||
{ | ||
public static readonly IColumn Default = new CategoriesColumn(); | ||
|
||
public string Id => nameof(CategoriesColumn); | ||
public string ColumnName => "Categories"; | ||
public string GetValue(Summary summary, Benchmark benchmark) => string.Join(",", benchmark.Target.Categories); | ||
public string GetValue(Summary summary, Benchmark benchmark, ISummaryStyle style) => GetValue(summary, benchmark); | ||
public bool IsDefault(Summary summary, Benchmark benchmark) => false; | ||
public bool IsAvailable(Summary summary) => summary.Benchmarks.Any(b => !b.Target.Categories.IsEmpty()); | ||
public bool AlwaysShow => false; | ||
public ColumnCategory Category => ColumnCategory.Job; | ||
public int PriorityInCategory => 100; | ||
public bool IsNumeric => false; | ||
public UnitType UnitType => UnitType.Dimensionless; | ||
public string Legend => "All categories of the corresponded method, class, and assembly"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,15 +65,22 @@ private static IEnumerable<Target> GetTargets(MethodInfo[] targetMethods, Type t | |
Where(m => m.HasAttribute<BenchmarkAttribute>()). | ||
Select(methodInfo => CreateTarget(type, setupMethod, methodInfo, cleanupMethod, methodInfo.ResolveAttribute<BenchmarkAttribute>(), targetMethods)); | ||
|
||
private static Target CreateTarget(Type type, MethodInfo setupMethod, MethodInfo methodInfo, MethodInfo cleanupMethod, BenchmarkAttribute attr, MethodInfo[] targetMethods) | ||
private static Target CreateTarget( | ||
Type type, | ||
MethodInfo setupMethod, | ||
MethodInfo methodInfo, | ||
MethodInfo cleanupMethod, | ||
BenchmarkAttribute attr, | ||
MethodInfo[] targetMethods) | ||
{ | ||
var target = new Target( | ||
type, | ||
methodInfo, | ||
type, | ||
methodInfo, | ||
setupMethod, | ||
cleanupMethod, | ||
attr.Description, | ||
baseline: attr.Baseline, | ||
attr.Description, | ||
baseline: attr.Baseline, | ||
categories: GetCategories(methodInfo), | ||
operationsPerInvoke: attr.OperationsPerInvoke, | ||
methodIndex: Array.IndexOf(targetMethods, methodInfo)); | ||
AssertMethodHasCorrectSignature("Benchmark", methodInfo); | ||
|
@@ -131,6 +138,21 @@ private static MethodInfo GetWrappingMethod<T>(MethodInfo[] methods, string meth | |
return setupMethod; | ||
} | ||
|
||
private static string[] GetCategories(MethodInfo method) | ||
{ | ||
var attributes = new List<BenchmarkCategoryAttribute>(); | ||
attributes.AddRange(method.GetCustomAttributes(typeof(BenchmarkCategoryAttribute), false).OfType<BenchmarkCategoryAttribute>()); | ||
var type = method.DeclaringType; | ||
if (type != null) | ||
{ | ||
attributes.AddRange(type.GetTypeInfo().GetCustomAttributes(typeof(BenchmarkCategoryAttribute), false).OfType<BenchmarkCategoryAttribute>()); | ||
attributes.AddRange(type.GetTypeInfo().Assembly.GetCustomAttributes().OfType<BenchmarkCategoryAttribute>()); | ||
} | ||
if (attributes.Count == 0) | ||
return Array.Empty<string>(); | ||
return attributes.SelectMany(attr => attr.Categories).ToArray(); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
AndreyAkinshin
Author
Member
|
||
} | ||
|
||
private static void AssertMethodHasCorrectSignature(string methodType, MethodInfo methodInfo) | ||
{ | ||
if (methodInfo.GetParameters().Any()) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
should we add
.Distinct()
here?