Skip to content

Commit 521b7fd

Browse files
Introduce CategoryDiscoverer, fix #2306
1 parent 9602893 commit 521b7fd

File tree

14 files changed

+266
-41
lines changed

14 files changed

+266
-41
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
uid: BenchmarkDotNet.Samples.IntroCategoryDiscoverer
3+
---
4+
5+
## Sample: IntroCategoryDiscoverer
6+
7+
The category discovery strategy can be overridden using an instance of `ICategoryDiscoverer`.
8+
9+
### Source code
10+
11+
[!code-csharp[IntroCategoryDiscoverer.cs](../../../samples/BenchmarkDotNet.Samples/IntroCategoryDiscoverer.cs)]
12+
13+
### Output
14+
15+
```markdown
16+
| Method | Categories | Mean | Error |
17+
|------- |----------- |---------:|------:|
18+
| Bar | All,B | 126.5 us | NA |
19+
| Foo | All,F | 114.0 us | NA |
20+
```
21+
22+
### Links
23+
24+
* The permanent link to this sample: @BenchmarkDotNet.Samples.IntroCategoryDiscoverer
25+
26+
---

docs/articles/samples/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
href: IntroCategories.md
1313
- name: IntroCategoryBaseline
1414
href: IntroCategoryBaseline.md
15+
- name: IntroCategoryDiscoverer
16+
href: IntroCategoryDiscoverer.md
1517
- name: IntroColdStart
1618
href: IntroColdStart.md
1719
- name: IntroComparableComplexParam
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Reflection;
4+
using BenchmarkDotNet.Attributes;
5+
using BenchmarkDotNet.Configs;
6+
using BenchmarkDotNet.Running;
7+
8+
namespace BenchmarkDotNet.Samples
9+
{
10+
[DryJob]
11+
[CategoriesColumn]
12+
[CustomCategoryDiscoverer]
13+
public class IntroCategoryDiscoverer
14+
{
15+
private class CustomCategoryDiscoverer : DefaultCategoryDiscoverer
16+
{
17+
public override string[] GetCategories(MethodInfo method)
18+
{
19+
var categories = new List<string>();
20+
categories.AddRange(base.GetCategories(method));
21+
categories.Add("All");
22+
categories.Add(method.Name.Substring(0, 1));
23+
return categories.ToArray();
24+
}
25+
}
26+
27+
[AttributeUsage(AttributeTargets.Class)]
28+
private class CustomCategoryDiscovererAttribute : Attribute, IConfigSource
29+
{
30+
public CustomCategoryDiscovererAttribute()
31+
{
32+
Config = ManualConfig.CreateEmpty()
33+
.WithCategoryDiscoverer(new CustomCategoryDiscoverer());
34+
}
35+
36+
public IConfig Config { get; }
37+
}
38+
39+
40+
[Benchmark]
41+
public void Foo() { }
42+
43+
[Benchmark]
44+
public void Bar() { }
45+
}
46+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using BenchmarkDotNet.Configs;
3+
using BenchmarkDotNet.Running;
4+
5+
namespace BenchmarkDotNet.Attributes
6+
{
7+
[AttributeUsage(AttributeTargets.Class)]
8+
public class CategoryDiscovererAttribute : Attribute, IConfigSource
9+
{
10+
public CategoryDiscovererAttribute(bool inherit = true)
11+
{
12+
Config = ManualConfig.CreateEmpty().WithCategoryDiscoverer(new DefaultCategoryDiscoverer(inherit));
13+
}
14+
15+
public IConfig Config { get; }
16+
}
17+
}

src/BenchmarkDotNet/Configs/DebugConfig.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using BenchmarkDotNet.Order;
1313
using BenchmarkDotNet.Portability;
1414
using BenchmarkDotNet.Reports;
15+
using BenchmarkDotNet.Running;
1516
using BenchmarkDotNet.Toolchains.InProcess.Emit;
1617
using BenchmarkDotNet.Validators;
1718

@@ -66,6 +67,7 @@ public abstract class DebugConfig : IConfig
6667
public IEnumerable<IColumnHidingRule> GetColumnHidingRules() => Array.Empty<IColumnHidingRule>();
6768

6869
public IOrderer Orderer => DefaultOrderer.Instance;
70+
public ICategoryDiscoverer? CategoryDiscoverer => DefaultCategoryDiscoverer.Instance;
6971
public SummaryStyle SummaryStyle => SummaryStyle.Default;
7072
public ConfigUnionRule UnionRule => ConfigUnionRule.Union;
7173
public TimeSpan BuildTimeout => DefaultConfig.Instance.BuildTimeout;

src/BenchmarkDotNet/Configs/DefaultConfig.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using BenchmarkDotNet.Order;
1414
using BenchmarkDotNet.Portability;
1515
using BenchmarkDotNet.Reports;
16+
using BenchmarkDotNet.Running;
1617
using BenchmarkDotNet.Validators;
1718

1819
namespace BenchmarkDotNet.Configs
@@ -72,6 +73,7 @@ public IEnumerable<IValidator> GetValidators()
7273
}
7374

7475
public IOrderer Orderer => null;
76+
public ICategoryDiscoverer? CategoryDiscoverer => null;
7577

7678
public ConfigUnionRule UnionRule => ConfigUnionRule.Union;
7779

src/BenchmarkDotNet/Configs/IConfig.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using BenchmarkDotNet.Loggers;
1111
using BenchmarkDotNet.Order;
1212
using BenchmarkDotNet.Reports;
13+
using BenchmarkDotNet.Running;
1314
using BenchmarkDotNet.Validators;
1415
using JetBrains.Annotations;
1516

@@ -30,6 +31,7 @@ public interface IConfig
3031
IEnumerable<IColumnHidingRule> GetColumnHidingRules();
3132

3233
IOrderer? Orderer { get; }
34+
ICategoryDiscoverer? CategoryDiscoverer { get; }
3335

3436
SummaryStyle SummaryStyle { get; }
3537

@@ -57,4 +59,4 @@ public interface IConfig
5759
/// </summary>
5860
IReadOnlyList<Conclusion> ConfigAnalysisConclusion { get; }
5961
}
60-
}
62+
}

src/BenchmarkDotNet/Configs/ImmutableConfig.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
using BenchmarkDotNet.Reports;
1515
using BenchmarkDotNet.Running;
1616
using BenchmarkDotNet.Validators;
17-
using JetBrains.Annotations;
1817
using RunMode = BenchmarkDotNet.Diagnosers.RunMode;
1918

2019
namespace BenchmarkDotNet.Configs
@@ -50,6 +49,7 @@ internal ImmutableConfig(
5049
string artifactsPath,
5150
CultureInfo cultureInfo,
5251
IOrderer orderer,
52+
ICategoryDiscoverer categoryDiscoverer,
5353
SummaryStyle summaryStyle,
5454
ConfigOptions options,
5555
TimeSpan buildTimeout,
@@ -70,6 +70,7 @@ internal ImmutableConfig(
7070
ArtifactsPath = artifactsPath;
7171
CultureInfo = cultureInfo;
7272
Orderer = orderer;
73+
CategoryDiscoverer = categoryDiscoverer;
7374
SummaryStyle = summaryStyle;
7475
Options = options;
7576
BuildTimeout = buildTimeout;
@@ -81,6 +82,7 @@ internal ImmutableConfig(
8182
public CultureInfo CultureInfo { get; }
8283
public ConfigOptions Options { get; }
8384
public IOrderer Orderer { get; }
85+
public ICategoryDiscoverer CategoryDiscoverer { get; }
8486
public SummaryStyle SummaryStyle { get; }
8587
public TimeSpan BuildTimeout { get; }
8688

src/BenchmarkDotNet/Configs/ImmutableConfigBuilder.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using BenchmarkDotNet.Jobs;
88
using BenchmarkDotNet.Order;
99
using BenchmarkDotNet.Reports;
10+
using BenchmarkDotNet.Running;
1011
using BenchmarkDotNet.Validators;
1112

1213
namespace BenchmarkDotNet.Configs
@@ -69,6 +70,7 @@ public static ImmutableConfig Create(IConfig source)
6970
source.ArtifactsPath ?? DefaultConfig.Instance.ArtifactsPath,
7071
source.CultureInfo,
7172
source.Orderer ?? DefaultOrderer.Instance,
73+
source.CategoryDiscoverer ?? DefaultCategoryDiscoverer.Instance,
7274
source.SummaryStyle ?? SummaryStyle.Default,
7375
source.Options,
7476
source.BuildTimeout,

src/BenchmarkDotNet/Configs/ManualConfig.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using BenchmarkDotNet.Loggers;
1414
using BenchmarkDotNet.Order;
1515
using BenchmarkDotNet.Reports;
16+
using BenchmarkDotNet.Running;
1617
using BenchmarkDotNet.Validators;
1718
using JetBrains.Annotations;
1819

@@ -51,6 +52,7 @@ public class ManualConfig : IConfig
5152
[PublicAPI] public string ArtifactsPath { get; set; }
5253
[PublicAPI] public CultureInfo CultureInfo { get; set; }
5354
[PublicAPI] public IOrderer Orderer { get; set; }
55+
[PublicAPI] public ICategoryDiscoverer CategoryDiscoverer { get; set; }
5456
[PublicAPI] public SummaryStyle SummaryStyle { get; set; }
5557
[PublicAPI] public TimeSpan BuildTimeout { get; set; } = DefaultConfig.Instance.BuildTimeout;
5658

@@ -92,6 +94,12 @@ public ManualConfig WithOrderer(IOrderer orderer)
9294
return this;
9395
}
9496

97+
public ManualConfig WithCategoryDiscoverer(ICategoryDiscoverer categoryDiscoverer)
98+
{
99+
CategoryDiscoverer = categoryDiscoverer;
100+
return this;
101+
}
102+
95103
public ManualConfig WithBuildTimeout(TimeSpan buildTimeout)
96104
{
97105
BuildTimeout = buildTimeout;
@@ -247,6 +255,7 @@ public void Add(IConfig config)
247255
hardwareCounters.AddRange(config.GetHardwareCounters());
248256
filters.AddRange(config.GetFilters());
249257
Orderer = config.Orderer ?? Orderer;
258+
CategoryDiscoverer = config.CategoryDiscoverer ?? CategoryDiscoverer;
250259
ArtifactsPath = config.ArtifactsPath ?? ArtifactsPath;
251260
CultureInfo = config.CultureInfo ?? CultureInfo;
252261
SummaryStyle = config.SummaryStyle ?? SummaryStyle;

0 commit comments

Comments
 (0)