-
-
Notifications
You must be signed in to change notification settings - Fork 984
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce CategoryDiscoverer, fix #2306
- Loading branch information
1 parent
716f450
commit f69568b
Showing
14 changed files
with
266 additions
and
41 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,26 @@ | ||
--- | ||
uid: BenchmarkDotNet.Samples.IntroCategoryDiscoverer | ||
--- | ||
|
||
## Sample: IntroCategoryDiscoverer | ||
|
||
The category discovery strategy can be overridden using an instance of `ICategoryDiscoverer`. | ||
|
||
### Source code | ||
|
||
[!code-csharp[IntroCategoryDiscoverer.cs](../../../samples/BenchmarkDotNet.Samples/IntroCategoryDiscoverer.cs)] | ||
|
||
### Output | ||
|
||
```markdown | ||
| Method | Categories | Mean | Error | | ||
|------- |----------- |---------:|------:| | ||
| Bar | All,B | 126.5 us | NA | | ||
| Foo | All,F | 114.0 us | NA | | ||
``` | ||
|
||
### Links | ||
|
||
* The permanent link to this sample: @BenchmarkDotNet.Samples.IntroCategoryDiscoverer | ||
|
||
--- |
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
46 changes: 46 additions & 0 deletions
46
samples/BenchmarkDotNet.Samples/IntroCategoryDiscoverer.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,46 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using BenchmarkDotNet.Attributes; | ||
using BenchmarkDotNet.Configs; | ||
using BenchmarkDotNet.Running; | ||
|
||
namespace BenchmarkDotNet.Samples | ||
{ | ||
[DryJob] | ||
[CategoriesColumn] | ||
[CustomCategoryDiscoverer] | ||
public class IntroCategoryDiscoverer | ||
{ | ||
private class CustomCategoryDiscoverer : DefaultCategoryDiscoverer | ||
{ | ||
public override string[] GetCategories(MethodInfo method) | ||
{ | ||
var categories = new List<string>(); | ||
categories.AddRange(base.GetCategories(method)); | ||
categories.Add("All"); | ||
categories.Add(method.Name.Substring(0, 1)); | ||
return categories.ToArray(); | ||
} | ||
} | ||
|
||
[AttributeUsage(AttributeTargets.Class)] | ||
private class CustomCategoryDiscovererAttribute : Attribute, IConfigSource | ||
{ | ||
public CustomCategoryDiscovererAttribute() | ||
{ | ||
Config = ManualConfig.CreateEmpty() | ||
.WithCategoryDiscoverer(new CustomCategoryDiscoverer()); | ||
} | ||
|
||
public IConfig Config { get; } | ||
} | ||
|
||
|
||
[Benchmark] | ||
public void Foo() { } | ||
|
||
[Benchmark] | ||
public void Bar() { } | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/BenchmarkDotNet/Attributes/CategoryDiscovererAttribute.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,17 @@ | ||
using System; | ||
using BenchmarkDotNet.Configs; | ||
using BenchmarkDotNet.Running; | ||
|
||
namespace BenchmarkDotNet.Attributes | ||
{ | ||
[AttributeUsage(AttributeTargets.Class)] | ||
public class CategoryDiscovererAttribute : Attribute, IConfigSource | ||
{ | ||
public CategoryDiscovererAttribute(bool inherit = true) | ||
{ | ||
Config = ManualConfig.CreateEmpty().WithCategoryDiscoverer(new DefaultCategoryDiscoverer(inherit)); | ||
} | ||
|
||
public IConfig Config { get; } | ||
} | ||
} |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using BenchmarkDotNet.Attributes; | ||
|
||
namespace BenchmarkDotNet.Running | ||
{ | ||
public class DefaultCategoryDiscoverer : ICategoryDiscoverer | ||
{ | ||
public static readonly ICategoryDiscoverer Instance = new DefaultCategoryDiscoverer(); | ||
|
||
private readonly bool inherit; | ||
|
||
public DefaultCategoryDiscoverer(bool inherit = true) | ||
{ | ||
this.inherit = inherit; | ||
} | ||
|
||
public virtual string[] GetCategories(MethodInfo method) | ||
{ | ||
var attributes = new List<BenchmarkCategoryAttribute>(); | ||
attributes.AddRange(method.GetCustomAttributes(typeof(BenchmarkCategoryAttribute), inherit).OfType<BenchmarkCategoryAttribute>()); | ||
var type = method.ReflectedType; | ||
if (type != null) | ||
{ | ||
attributes.AddRange(type.GetTypeInfo().GetCustomAttributes(typeof(BenchmarkCategoryAttribute), inherit).OfType<BenchmarkCategoryAttribute>()); | ||
attributes.AddRange(type.GetTypeInfo().Assembly.GetCustomAttributes().OfType<BenchmarkCategoryAttribute>()); | ||
} | ||
if (attributes.Count == 0) | ||
return Array.Empty<string>(); | ||
return attributes.SelectMany(attr => attr.Categories).Distinct(StringComparer.OrdinalIgnoreCase).ToArray(); | ||
} | ||
} | ||
} |
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 System.Reflection; | ||
|
||
namespace BenchmarkDotNet.Running | ||
{ | ||
public interface ICategoryDiscoverer | ||
{ | ||
string[] GetCategories(MethodInfo method); | ||
} | ||
} |
Oops, something went wrong.