Skip to content

Commit 56ef77f

Browse files
committed
add ByType rule
1 parent d4357ba commit 56ef77f

File tree

4 files changed

+49
-15
lines changed

4 files changed

+49
-15
lines changed

src/BenchmarkDotNet/Configs/BenchmarkLogicalGroupRule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
{
33
public enum BenchmarkLogicalGroupRule
44
{
5-
ByMethod, ByJob, ByParams, ByCategory
5+
ByMethod, ByJob, ByParams, ByCategory, ByType
66
}
77
}

src/BenchmarkDotNet/Order/DefaultOrderer.cs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Diagnostics.CodeAnalysis;
55
using System.Linq;
66
using BenchmarkDotNet.Configs;
7+
using BenchmarkDotNet.Extensions;
78
using BenchmarkDotNet.Jobs;
89
using BenchmarkDotNet.Parameters;
910
using BenchmarkDotNet.Reports;
@@ -20,7 +21,8 @@ public class DefaultOrderer : IOrderer
2021
private readonly IComparer<string[]> categoryComparer = CategoryComparer.Instance;
2122
private readonly IComparer<ParameterInstances> paramsComparer = ParameterComparer.Instance;
2223
private readonly IComparer<Job> jobComparer = JobComparer.Instance;
23-
private readonly IComparer<Descriptor> targetComparer;
24+
private readonly IComparer<Descriptor> typeComparer = TypeComparer.Instance;
25+
private readonly IComparer<Descriptor> methodComparer;
2426

2527
public SummaryOrderPolicy SummaryOrderPolicy { get; }
2628
public MethodOrderPolicy MethodOrderPolicy { get; }
@@ -31,15 +33,15 @@ public DefaultOrderer(
3133
{
3234
SummaryOrderPolicy = summaryOrderPolicy;
3335
MethodOrderPolicy = methodOrderPolicy;
34-
targetComparer = new DescriptorComparer(methodOrderPolicy);
36+
methodComparer = new MethodComparer(methodOrderPolicy);
3537
}
3638

3739
[PublicAPI]
3840
public virtual IEnumerable<BenchmarkCase> GetExecutionOrder(
3941
ImmutableArray<BenchmarkCase> benchmarkCases,
4042
IEnumerable<BenchmarkLogicalGroupRule> order = null)
4143
{
42-
var benchmarkComparer = new BenchmarkComparer(categoryComparer, paramsComparer, jobComparer, targetComparer, order);
44+
var benchmarkComparer = new BenchmarkComparer(categoryComparer, paramsComparer, jobComparer, typeComparer, methodComparer, order);
4345
var list = benchmarkCases.ToList();
4446
list.Sort(benchmarkComparer);
4547
return list;
@@ -87,8 +89,15 @@ public string GetLogicalGroupKey(ImmutableArray<BenchmarkCase> allBenchmarksCase
8789
{
8890
var explicitRules = benchmarkCase.Config.GetLogicalGroupRules().ToList();
8991
var implicitRules = new List<BenchmarkLogicalGroupRule>();
92+
93+
bool hasMultipleTypes = allBenchmarksCases.DistinctBy(b => b.Descriptor.Type).Count() > 1;
9094
bool hasJobBaselines = allBenchmarksCases.Any(b => b.Job.Meta.Baseline);
9195
bool hasDescriptorBaselines = allBenchmarksCases.Any(b => b.Descriptor.Baseline);
96+
97+
if (hasMultipleTypes)
98+
{
99+
implicitRules.Add(BenchmarkLogicalGroupRule.ByType);
100+
}
92101
if (hasJobBaselines)
93102
{
94103
implicitRules.Add(BenchmarkLogicalGroupRule.ByParams);
@@ -114,8 +123,11 @@ public string GetLogicalGroupKey(ImmutableArray<BenchmarkCase> allBenchmarksCase
114123
{
115124
switch (rule)
116125
{
126+
case BenchmarkLogicalGroupRule.ByType:
127+
keys.Add(benchmarkCase.Descriptor.TypeInfo);
128+
break;
117129
case BenchmarkLogicalGroupRule.ByMethod:
118-
keys.Add(benchmarkCase.Descriptor.DisplayInfo);
130+
keys.Add(benchmarkCase.Descriptor.WorkloadMethodDisplayInfo);
119131
break;
120132
case BenchmarkLogicalGroupRule.ByJob:
121133
keys.Add(benchmarkCase.Job.DisplayInfo);
@@ -139,7 +151,7 @@ public virtual IEnumerable<IGrouping<string, BenchmarkCase>> GetLogicalGroupOrde
139151
IEnumerable<IGrouping<string, BenchmarkCase>> logicalGroups,
140152
IEnumerable<BenchmarkLogicalGroupRule> order = null)
141153
{
142-
var benchmarkComparer = new BenchmarkComparer(categoryComparer, paramsComparer, jobComparer, targetComparer, order);
154+
var benchmarkComparer = new BenchmarkComparer(categoryComparer, paramsComparer, jobComparer, typeComparer, methodComparer, order);
143155
var logicalGroupComparer = new LogicalGroupComparer(benchmarkComparer);
144156
var list = logicalGroups.ToList();
145157
list.Sort(logicalGroupComparer);
@@ -153,6 +165,7 @@ private class BenchmarkComparer : IComparer<BenchmarkCase>
153165
private static readonly BenchmarkLogicalGroupRule[] DefaultOrder =
154166
{
155167
BenchmarkLogicalGroupRule.ByCategory,
168+
BenchmarkLogicalGroupRule.ByType,
156169
BenchmarkLogicalGroupRule.ByParams,
157170
BenchmarkLogicalGroupRule.ByJob,
158171
BenchmarkLogicalGroupRule.ByMethod
@@ -161,18 +174,21 @@ private class BenchmarkComparer : IComparer<BenchmarkCase>
161174
private readonly IComparer<string[]> categoryComparer;
162175
private readonly IComparer<ParameterInstances> paramsComparer;
163176
private readonly IComparer<Job> jobComparer;
164-
private readonly IComparer<Descriptor> targetComparer;
177+
private readonly IComparer<Descriptor> typeComparer;
178+
private readonly IComparer<Descriptor> methodComparer;
165179
private readonly List<BenchmarkLogicalGroupRule> order;
166180

167181
public BenchmarkComparer(
168182
IComparer<string[]> categoryComparer,
169183
IComparer<ParameterInstances> paramsComparer,
170184
IComparer<Job> jobComparer,
171-
IComparer<Descriptor> targetComparer,
185+
IComparer<Descriptor> typeComparer,
186+
IComparer<Descriptor> methodComparer,
172187
IEnumerable<BenchmarkLogicalGroupRule> order)
173188
{
174189
this.categoryComparer = categoryComparer;
175-
this.targetComparer = targetComparer;
190+
this.typeComparer = typeComparer;
191+
this.methodComparer = methodComparer;
176192
this.jobComparer = jobComparer;
177193
this.paramsComparer = paramsComparer;
178194

@@ -192,7 +208,8 @@ public int Compare(BenchmarkCase x, BenchmarkCase y)
192208
{
193209
int compare = rule switch
194210
{
195-
BenchmarkLogicalGroupRule.ByMethod => targetComparer?.Compare(x.Descriptor, y.Descriptor) ?? 0,
211+
BenchmarkLogicalGroupRule.ByType => typeComparer?.Compare(x.Descriptor, y.Descriptor) ?? 0,
212+
BenchmarkLogicalGroupRule.ByMethod => methodComparer?.Compare(x.Descriptor, y.Descriptor) ?? 0,
196213
BenchmarkLogicalGroupRule.ByJob => jobComparer?.Compare(x.Job, y.Job) ?? 0,
197214
BenchmarkLogicalGroupRule.ByParams => paramsComparer?.Compare(x.Parameters, y.Parameters) ?? 0,
198215
BenchmarkLogicalGroupRule.ByCategory => categoryComparer?.Compare(x.Descriptor.Categories, y.Descriptor.Categories) ?? 0,

src/BenchmarkDotNet/Running/DescriptorComparer.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55

66
namespace BenchmarkDotNet.Running
77
{
8-
internal class DescriptorComparer : IComparer<Descriptor>
8+
internal class MethodComparer : IComparer<Descriptor>
99
{
10-
[PublicAPI] public static readonly IComparer<Descriptor> Alphabetical = new DescriptorComparer(MethodOrderPolicy.Alphabetical);
11-
[PublicAPI] public static readonly IComparer<Descriptor> Declared = new DescriptorComparer(MethodOrderPolicy.Declared);
10+
[PublicAPI] public static readonly IComparer<Descriptor> Alphabetical = new MethodComparer(MethodOrderPolicy.Alphabetical);
11+
[PublicAPI] public static readonly IComparer<Descriptor> Declared = new MethodComparer(MethodOrderPolicy.Declared);
1212

1313
private readonly MethodOrderPolicy methodOrderPolicy;
1414

15-
public DescriptorComparer(MethodOrderPolicy methodOrderPolicy)
15+
public MethodComparer(MethodOrderPolicy methodOrderPolicy)
1616
{
1717
this.methodOrderPolicy = methodOrderPolicy;
1818
}
@@ -25,7 +25,7 @@ public int Compare(Descriptor x, Descriptor y)
2525
switch (methodOrderPolicy)
2626
{
2727
case MethodOrderPolicy.Alphabetical:
28-
return string.CompareOrdinal(x.DisplayInfo, y.DisplayInfo);
28+
return string.CompareOrdinal(x.WorkloadMethodDisplayInfo, y.WorkloadMethodDisplayInfo);
2929
case MethodOrderPolicy.Declared:
3030
return x.MethodIndex - y.MethodIndex;
3131
default:
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System.Collections.Generic;
2+
3+
namespace BenchmarkDotNet.Running
4+
{
5+
internal class TypeComparer : IComparer<Descriptor>
6+
{
7+
public static readonly IComparer<Descriptor> Instance = new TypeComparer();
8+
9+
public int Compare(Descriptor x, Descriptor y)
10+
{
11+
if (x == null && y == null) return 0;
12+
if (x != null && y == null) return 1;
13+
if (x == null) return -1;
14+
return string.CompareOrdinal(x.TypeInfo, y.TypeInfo);
15+
}
16+
}
17+
}

0 commit comments

Comments
 (0)