1
+ using System ;
1
2
using System . Collections . Generic ;
2
3
using System . Collections . Immutable ;
3
4
using System . Diagnostics . CodeAnalysis ;
@@ -19,8 +20,7 @@ public class DefaultOrderer : IOrderer
19
20
private readonly IComparer < string [ ] > categoryComparer = CategoryComparer . Instance ;
20
21
private readonly IComparer < ParameterInstances > paramsComparer = ParameterComparer . Instance ;
21
22
private readonly IComparer < Job > jobComparer = JobComparer . Instance ;
22
- private readonly IComparer < BenchmarkCase > benchmarkComparer ;
23
- private readonly IComparer < IGrouping < string , BenchmarkCase > > logicalGroupComparer ;
23
+ private readonly IComparer < Descriptor > targetComparer ;
24
24
25
25
public SummaryOrderPolicy SummaryOrderPolicy { get ; }
26
26
public MethodOrderPolicy MethodOrderPolicy { get ; }
@@ -31,14 +31,15 @@ public DefaultOrderer(
31
31
{
32
32
SummaryOrderPolicy = summaryOrderPolicy ;
33
33
MethodOrderPolicy = methodOrderPolicy ;
34
- IComparer < Descriptor > targetComparer = new DescriptorComparer ( methodOrderPolicy ) ;
35
- benchmarkComparer = new BenchmarkComparer ( categoryComparer , paramsComparer , jobComparer , targetComparer ) ;
36
- logicalGroupComparer = new LogicalGroupComparer ( benchmarkComparer ) ;
34
+ targetComparer = new DescriptorComparer ( methodOrderPolicy ) ;
37
35
}
38
36
39
37
[ PublicAPI ]
40
- public virtual IEnumerable < BenchmarkCase > GetExecutionOrder ( ImmutableArray < BenchmarkCase > benchmarkCases )
38
+ public virtual IEnumerable < BenchmarkCase > GetExecutionOrder (
39
+ ImmutableArray < BenchmarkCase > benchmarkCases ,
40
+ IEnumerable < BenchmarkLogicalGroupRule > order = null )
41
41
{
42
+ var benchmarkComparer = new BenchmarkComparer ( categoryComparer , paramsComparer , jobComparer , targetComparer , order ) ;
42
43
var list = benchmarkCases . ToList ( ) ;
43
44
list . Sort ( benchmarkComparer ) ;
44
45
return list ;
@@ -47,7 +48,7 @@ public virtual IEnumerable<BenchmarkCase> GetExecutionOrder(ImmutableArray<Bench
47
48
public virtual IEnumerable < BenchmarkCase > GetSummaryOrder ( ImmutableArray < BenchmarkCase > benchmarksCases , Summary summary )
48
49
{
49
50
var benchmarkLogicalGroups = benchmarksCases . GroupBy ( b => GetLogicalGroupKey ( benchmarksCases , b ) ) ;
50
- foreach ( var logicalGroup in GetLogicalGroupOrder ( benchmarkLogicalGroups ) )
51
+ foreach ( var logicalGroup in GetLogicalGroupOrder ( benchmarkLogicalGroups , benchmarksCases . FirstOrDefault ( ) ? . Config . GetLogicalGroupRules ( ) ) )
51
52
foreach ( var benchmark in GetSummaryOrderForGroup ( logicalGroup . ToImmutableArray ( ) , summary ) )
52
53
yield return benchmark ;
53
54
}
@@ -65,7 +66,7 @@ protected virtual IEnumerable<BenchmarkCase> GetSummaryOrderForGroup(ImmutableAr
65
66
case SummaryOrderPolicy . Declared :
66
67
return benchmarksCase ;
67
68
default :
68
- return GetExecutionOrder ( benchmarksCase ) ;
69
+ return GetExecutionOrder ( benchmarksCase , benchmarksCase . FirstOrDefault ( ) ? . Config . GetLogicalGroupRules ( ) ) ;
69
70
}
70
71
}
71
72
@@ -84,41 +85,62 @@ public string GetHighlightGroupKey(BenchmarkCase benchmarkCase)
84
85
85
86
public string GetLogicalGroupKey ( ImmutableArray < BenchmarkCase > allBenchmarksCases , BenchmarkCase benchmarkCase )
86
87
{
87
- var rules = new HashSet < BenchmarkLogicalGroupRule > ( benchmarkCase . Config . GetLogicalGroupRules ( ) ) ;
88
+ var explicitRules = benchmarkCase . Config . GetLogicalGroupRules ( ) . ToList ( ) ;
89
+ var implicitRules = new List < BenchmarkLogicalGroupRule > ( ) ;
88
90
bool hasJobBaselines = allBenchmarksCases . Any ( b => b . Job . Meta . Baseline ) ;
89
91
bool hasDescriptorBaselines = allBenchmarksCases . Any ( b => b . Descriptor . Baseline ) ;
90
92
if ( hasJobBaselines )
91
93
{
92
- rules . Add ( BenchmarkLogicalGroupRule . ByMethod ) ;
93
- rules . Add ( BenchmarkLogicalGroupRule . ByParams ) ;
94
+ implicitRules . Add ( BenchmarkLogicalGroupRule . ByParams ) ;
95
+ implicitRules . Add ( BenchmarkLogicalGroupRule . ByMethod ) ;
94
96
}
95
97
if ( hasDescriptorBaselines )
96
98
{
97
- rules . Add ( BenchmarkLogicalGroupRule . ByJob ) ;
98
- rules . Add ( BenchmarkLogicalGroupRule . ByParams ) ;
99
+ implicitRules . Add ( BenchmarkLogicalGroupRule . ByParams ) ;
100
+ implicitRules . Add ( BenchmarkLogicalGroupRule . ByJob ) ;
99
101
}
100
102
if ( hasJobBaselines && hasDescriptorBaselines )
101
103
{
102
- rules . Remove ( BenchmarkLogicalGroupRule . ByMethod ) ;
103
- rules . Remove ( BenchmarkLogicalGroupRule . ByJob ) ;
104
+ implicitRules . Remove ( BenchmarkLogicalGroupRule . ByMethod ) ;
105
+ implicitRules . Remove ( BenchmarkLogicalGroupRule . ByJob ) ;
104
106
}
105
107
108
+ var rules = new List < BenchmarkLogicalGroupRule > ( explicitRules ) ;
109
+ foreach ( var rule in implicitRules . Where ( rule => ! rules . Contains ( rule ) ) )
110
+ rules . Add ( rule ) ;
111
+
106
112
var keys = new List < string > ( ) ;
107
- if ( rules . Contains ( BenchmarkLogicalGroupRule . ByCategory ) )
108
- keys . Add ( string . Join ( "," , benchmarkCase . Descriptor . Categories ) ) ;
109
- if ( rules . Contains ( BenchmarkLogicalGroupRule . ByMethod ) )
110
- keys . Add ( benchmarkCase . Descriptor . DisplayInfo ) ;
111
- if ( rules . Contains ( BenchmarkLogicalGroupRule . ByJob ) )
112
- keys . Add ( benchmarkCase . Job . DisplayInfo ) ;
113
- if ( rules . Contains ( BenchmarkLogicalGroupRule . ByParams ) )
114
- keys . Add ( benchmarkCase . Parameters . ValueInfo ) ;
113
+ foreach ( var rule in rules )
114
+ {
115
+ switch ( rule )
116
+ {
117
+ case BenchmarkLogicalGroupRule . ByMethod :
118
+ keys . Add ( benchmarkCase . Descriptor . DisplayInfo ) ;
119
+ break ;
120
+ case BenchmarkLogicalGroupRule . ByJob :
121
+ keys . Add ( benchmarkCase . Job . DisplayInfo ) ;
122
+ break ;
123
+ case BenchmarkLogicalGroupRule . ByParams :
124
+ keys . Add ( benchmarkCase . Parameters . ValueInfo ) ;
125
+ break ;
126
+ case BenchmarkLogicalGroupRule . ByCategory :
127
+ keys . Add ( string . Join ( "," , benchmarkCase . Descriptor . Categories ) ) ;
128
+ break ;
129
+ default :
130
+ throw new ArgumentOutOfRangeException ( nameof ( rule ) , rule , $ "Not supported { nameof ( BenchmarkLogicalGroupRule ) } ") ;
131
+ }
132
+ }
115
133
116
134
string logicalGroupKey = string . Join ( "-" , keys . Where ( key => key != string . Empty ) ) ;
117
135
return logicalGroupKey == string . Empty ? "*" : logicalGroupKey ;
118
136
}
119
137
120
- public virtual IEnumerable < IGrouping < string , BenchmarkCase > > GetLogicalGroupOrder ( IEnumerable < IGrouping < string , BenchmarkCase > > logicalGroups )
138
+ public virtual IEnumerable < IGrouping < string , BenchmarkCase > > GetLogicalGroupOrder (
139
+ IEnumerable < IGrouping < string , BenchmarkCase > > logicalGroups ,
140
+ IEnumerable < BenchmarkLogicalGroupRule > order = null )
121
141
{
142
+ var benchmarkComparer = new BenchmarkComparer ( categoryComparer , paramsComparer , jobComparer , targetComparer , order ) ;
143
+ var logicalGroupComparer = new LogicalGroupComparer ( benchmarkComparer ) ;
122
144
var list = logicalGroups . ToList ( ) ;
123
145
list . Sort ( logicalGroupComparer ) ;
124
146
return list ;
@@ -128,36 +150,58 @@ public virtual IEnumerable<IGrouping<string, BenchmarkCase>> GetLogicalGroupOrde
128
150
129
151
private class BenchmarkComparer : IComparer < BenchmarkCase >
130
152
{
153
+ private static readonly BenchmarkLogicalGroupRule [ ] DefaultOrder =
154
+ {
155
+ BenchmarkLogicalGroupRule . ByCategory ,
156
+ BenchmarkLogicalGroupRule . ByParams ,
157
+ BenchmarkLogicalGroupRule . ByJob ,
158
+ BenchmarkLogicalGroupRule . ByMethod
159
+ } ;
160
+
131
161
private readonly IComparer < string [ ] > categoryComparer ;
132
162
private readonly IComparer < ParameterInstances > paramsComparer ;
133
163
private readonly IComparer < Job > jobComparer ;
134
164
private readonly IComparer < Descriptor > targetComparer ;
165
+ private readonly List < BenchmarkLogicalGroupRule > order ;
135
166
136
167
public BenchmarkComparer (
137
168
IComparer < string [ ] > categoryComparer ,
138
169
IComparer < ParameterInstances > paramsComparer ,
139
170
IComparer < Job > jobComparer ,
140
- IComparer < Descriptor > targetComparer )
171
+ IComparer < Descriptor > targetComparer ,
172
+ IEnumerable < BenchmarkLogicalGroupRule > order )
141
173
{
142
174
this . categoryComparer = categoryComparer ;
143
175
this . targetComparer = targetComparer ;
144
176
this . jobComparer = jobComparer ;
145
177
this . paramsComparer = paramsComparer ;
178
+
179
+ this . order = new List < BenchmarkLogicalGroupRule > ( ) ;
180
+ foreach ( var rule in ( order ?? ImmutableArray < BenchmarkLogicalGroupRule > . Empty ) . Concat ( DefaultOrder ) )
181
+ if ( ! this . order . Contains ( rule ) )
182
+ this . order . Add ( rule ) ;
146
183
}
147
184
148
185
public int Compare ( BenchmarkCase x , BenchmarkCase y )
149
186
{
150
187
if ( x == null && y == null ) return 0 ;
151
188
if ( x != null && y == null ) return 1 ;
152
189
if ( x == null ) return - 1 ;
153
- return new [ ]
190
+
191
+ foreach ( var rule in order )
154
192
{
155
- categoryComparer ? . Compare ( x . Descriptor . Categories , y . Descriptor . Categories ) ?? 0 ,
156
- paramsComparer ? . Compare ( x . Parameters , y . Parameters ) ?? 0 ,
157
- jobComparer ? . Compare ( x . Job , y . Job ) ?? 0 ,
158
- targetComparer ? . Compare ( x . Descriptor , y . Descriptor ) ?? 0 ,
159
- string . CompareOrdinal ( x . DisplayInfo , y . DisplayInfo )
160
- } . FirstOrDefault ( c => c != 0 ) ;
193
+ int compare = rule switch
194
+ {
195
+ BenchmarkLogicalGroupRule . ByMethod => targetComparer ? . Compare ( x . Descriptor , y . Descriptor ) ?? 0 ,
196
+ BenchmarkLogicalGroupRule . ByJob => jobComparer ? . Compare ( x . Job , y . Job ) ?? 0 ,
197
+ BenchmarkLogicalGroupRule . ByParams => paramsComparer ? . Compare ( x . Parameters , y . Parameters ) ?? 0 ,
198
+ BenchmarkLogicalGroupRule . ByCategory => categoryComparer ? . Compare ( x . Descriptor . Categories , y . Descriptor . Categories ) ?? 0 ,
199
+ _ => throw new ArgumentOutOfRangeException ( )
200
+ } ;
201
+ if ( compare != 0 )
202
+ return compare ;
203
+ }
204
+ return string . CompareOrdinal ( x . DisplayInfo , y . DisplayInfo ) ;
161
205
}
162
206
}
163
207
@@ -176,4 +220,4 @@ public int Compare(IGrouping<string, BenchmarkCase> x, IGrouping<string, Benchma
176
220
}
177
221
}
178
222
}
179
- }
223
+ }
0 commit comments