@@ -45,6 +45,9 @@ public InProcessTest(ITestOutputHelper output) : base(output)
45
45
[ Fact ]
46
46
public void BenchmarkActionTaskSupported ( ) => TestInvoke ( x => x . InvokeOnceTaskAsync ( ) , UnrollFactor , null ) ;
47
47
48
+ [ Fact ]
49
+ public void BenchmarkActionValueTaskSupported ( ) => TestInvoke ( x => x . InvokeOnceValueTaskAsync ( ) , UnrollFactor , null ) ;
50
+
48
51
[ Fact ]
49
52
public void BenchmarkActionRefTypeSupported ( ) => TestInvoke ( x => x . InvokeOnceRefType ( ) , UnrollFactor , StringResult ) ;
50
53
@@ -57,6 +60,18 @@ public InProcessTest(ITestOutputHelper output) : base(output)
57
60
[ Fact ]
58
61
public void BenchmarkActionValueTaskOfTSupported ( ) => TestInvoke ( x => x . InvokeOnceValueTaskOfT ( ) , UnrollFactor , DecimalResult ) ;
59
62
63
+ [ Fact ]
64
+ public void BenchmarkActionGlobalSetupTaskSupported ( ) => TestInvokeSetupCleanupTask ( x => BenchmarkSetupCleanupTask . GlobalSetup ( ) , UnrollFactor ) ;
65
+
66
+ [ Fact ]
67
+ public void BenchmarkActionGlobalCleanupTaskSupported ( ) => TestInvokeSetupCleanupTask ( x => x . GlobalCleanup ( ) , UnrollFactor ) ;
68
+
69
+ [ Fact ]
70
+ public void BenchmarkActionGlobalSetupValueTaskSupported ( ) => TestInvokeSetupCleanupValueTask ( x => BenchmarkSetupCleanupValueTask . GlobalSetup ( ) , UnrollFactor ) ;
71
+
72
+ [ Fact ]
73
+ public void BenchmarkActionGlobalCleanupValueTaskSupported ( ) => TestInvokeSetupCleanupValueTask ( x => x . GlobalCleanup ( ) , UnrollFactor ) ;
74
+
60
75
[ Fact ]
61
76
public void BenchmarkDifferentPlatformReturnsValidationError ( )
62
77
{
@@ -83,30 +98,30 @@ private void TestInvoke(Expression<Action<BenchmarkAllCases>> methodCall, int un
83
98
84
99
// Run mode
85
100
var action = BenchmarkActionFactory . CreateWorkload ( descriptor , new BenchmarkAllCases ( ) , unrollFactor ) ;
86
- TestInvoke ( action , unrollFactor , false , null ) ;
101
+ TestInvoke ( action , unrollFactor , false , null , ref BenchmarkAllCases . Counter ) ;
87
102
88
103
// Idle mode
89
104
action = BenchmarkActionFactory . CreateOverhead ( descriptor , new BenchmarkAllCases ( ) , unrollFactor ) ;
90
- TestInvoke ( action , unrollFactor , true , null ) ;
105
+ TestInvoke ( action , unrollFactor , true , null , ref BenchmarkAllCases . Counter ) ;
91
106
92
107
// GlobalSetup/GlobalCleanup
93
108
action = BenchmarkActionFactory . CreateGlobalSetup ( descriptor , new BenchmarkAllCases ( ) ) ;
94
- TestInvoke ( action , 1 , false , null ) ;
109
+ TestInvoke ( action , 1 , false , null , ref BenchmarkAllCases . Counter ) ;
95
110
action = BenchmarkActionFactory . CreateGlobalCleanup ( descriptor , new BenchmarkAllCases ( ) ) ;
96
- TestInvoke ( action , 1 , false , null ) ;
111
+ TestInvoke ( action , 1 , false , null , ref BenchmarkAllCases . Counter ) ;
97
112
98
113
// GlobalSetup/GlobalCleanup (empty)
99
114
descriptor = new Descriptor ( typeof ( BenchmarkAllCases ) , targetMethod ) ;
100
115
action = BenchmarkActionFactory . CreateGlobalSetup ( descriptor , new BenchmarkAllCases ( ) ) ;
101
- TestInvoke ( action , unrollFactor , true , null ) ;
116
+ TestInvoke ( action , unrollFactor , true , null , ref BenchmarkAllCases . Counter ) ;
102
117
action = BenchmarkActionFactory . CreateGlobalCleanup ( descriptor , new BenchmarkAllCases ( ) ) ;
103
- TestInvoke ( action , unrollFactor , true , null ) ;
118
+ TestInvoke ( action , unrollFactor , true , null , ref BenchmarkAllCases . Counter ) ;
104
119
105
120
// Dummy (just in case something may broke)
106
121
action = BenchmarkActionFactory . CreateDummy ( ) ;
107
- TestInvoke ( action , unrollFactor , true , null ) ;
122
+ TestInvoke ( action , unrollFactor , true , null , ref BenchmarkAllCases . Counter ) ;
108
123
action = BenchmarkActionFactory . CreateDummy ( ) ;
109
- TestInvoke ( action , unrollFactor , true , null ) ;
124
+ TestInvoke ( action , unrollFactor , true , null , ref BenchmarkAllCases . Counter ) ;
110
125
}
111
126
112
127
[ AssertionMethod ]
@@ -117,7 +132,7 @@ private void TestInvoke<T>(Expression<Func<BenchmarkAllCases, T>> methodCall, in
117
132
118
133
// Run mode
119
134
var action = BenchmarkActionFactory . CreateWorkload ( descriptor , new BenchmarkAllCases ( ) , unrollFactor ) ;
120
- TestInvoke ( action , unrollFactor , false , expectedResult ) ;
135
+ TestInvoke ( action , unrollFactor , false , expectedResult , ref BenchmarkAllCases . Counter ) ;
121
136
122
137
// Idle mode
123
138
@@ -126,15 +141,83 @@ private void TestInvoke<T>(Expression<Func<BenchmarkAllCases, T>> methodCall, in
126
141
object idleExpected ;
127
142
if ( isValueTask )
128
143
idleExpected = GetDefault ( typeof ( T ) . GetGenericArguments ( ) [ 0 ] ) ;
144
+ else if ( expectedResult == null || typeof ( T ) == typeof ( Task ) || typeof ( T ) == typeof ( ValueTask ) )
145
+ idleExpected = null ;
129
146
else if ( typeof ( T ) . GetTypeInfo ( ) . IsValueType )
130
147
idleExpected = 0 ;
131
- else if ( expectedResult == null || typeof ( T ) == typeof ( Task ) )
132
- idleExpected = null ;
133
148
else
134
149
idleExpected = GetDefault ( expectedResult . GetType ( ) ) ;
135
150
136
151
action = BenchmarkActionFactory . CreateOverhead ( descriptor , new BenchmarkAllCases ( ) , unrollFactor ) ;
137
- TestInvoke ( action , unrollFactor , true , idleExpected ) ;
152
+ TestInvoke ( action , unrollFactor , true , idleExpected , ref BenchmarkAllCases . Counter ) ;
153
+ }
154
+
155
+ [ AssertionMethod ]
156
+ private void TestInvokeSetupCleanupTask ( Expression < Func < BenchmarkSetupCleanupTask , Task > > methodCall , int unrollFactor )
157
+ {
158
+ var targetMethod = ( ( MethodCallExpression ) methodCall . Body ) . Method ;
159
+ var descriptor = new Descriptor ( typeof ( BenchmarkSetupCleanupTask ) , targetMethod , targetMethod , targetMethod , targetMethod , targetMethod ) ;
160
+
161
+ // Run mode
162
+ var action = BenchmarkActionFactory . CreateWorkload ( descriptor , new BenchmarkSetupCleanupTask ( ) , unrollFactor ) ;
163
+ TestInvoke ( action , unrollFactor , false , null , ref BenchmarkSetupCleanupTask . Counter ) ;
164
+
165
+ // Idle mode
166
+ action = BenchmarkActionFactory . CreateOverhead ( descriptor , new BenchmarkSetupCleanupTask ( ) , unrollFactor ) ;
167
+ TestInvoke ( action , unrollFactor , true , null , ref BenchmarkSetupCleanupTask . Counter ) ;
168
+
169
+ // GlobalSetup/GlobalCleanup
170
+ action = BenchmarkActionFactory . CreateGlobalSetup ( descriptor , new BenchmarkSetupCleanupTask ( ) ) ;
171
+ TestInvoke ( action , 1 , false , null , ref BenchmarkSetupCleanupTask . Counter ) ;
172
+ action = BenchmarkActionFactory . CreateGlobalCleanup ( descriptor , new BenchmarkSetupCleanupTask ( ) ) ;
173
+ TestInvoke ( action , 1 , false , null , ref BenchmarkSetupCleanupTask . Counter ) ;
174
+
175
+ // GlobalSetup/GlobalCleanup (empty)
176
+ descriptor = new Descriptor ( typeof ( BenchmarkSetupCleanupTask ) , targetMethod ) ;
177
+ action = BenchmarkActionFactory . CreateGlobalSetup ( descriptor , new BenchmarkSetupCleanupTask ( ) ) ;
178
+ TestInvoke ( action , unrollFactor , true , null , ref BenchmarkSetupCleanupTask . Counter ) ;
179
+ action = BenchmarkActionFactory . CreateGlobalCleanup ( descriptor , new BenchmarkSetupCleanupTask ( ) ) ;
180
+ TestInvoke ( action , unrollFactor , true , null , ref BenchmarkSetupCleanupTask . Counter ) ;
181
+
182
+ // Dummy (just in case something may broke)
183
+ action = BenchmarkActionFactory . CreateDummy ( ) ;
184
+ TestInvoke ( action , unrollFactor , true , null , ref BenchmarkSetupCleanupTask . Counter ) ;
185
+ action = BenchmarkActionFactory . CreateDummy ( ) ;
186
+ TestInvoke ( action , unrollFactor , true , null , ref BenchmarkSetupCleanupTask . Counter ) ;
187
+ }
188
+
189
+ [ AssertionMethod ]
190
+ private void TestInvokeSetupCleanupValueTask ( Expression < Func < BenchmarkSetupCleanupValueTask , ValueTask > > methodCall , int unrollFactor )
191
+ {
192
+ var targetMethod = ( ( MethodCallExpression ) methodCall . Body ) . Method ;
193
+ var descriptor = new Descriptor ( typeof ( BenchmarkSetupCleanupValueTask ) , targetMethod , targetMethod , targetMethod , targetMethod , targetMethod ) ;
194
+
195
+ // Run mode
196
+ var action = BenchmarkActionFactory . CreateWorkload ( descriptor , new BenchmarkSetupCleanupValueTask ( ) , unrollFactor ) ;
197
+ TestInvoke ( action , unrollFactor , false , null , ref BenchmarkSetupCleanupValueTask . Counter ) ;
198
+
199
+ // Idle mode
200
+ action = BenchmarkActionFactory . CreateOverhead ( descriptor , new BenchmarkSetupCleanupValueTask ( ) , unrollFactor ) ;
201
+ TestInvoke ( action , unrollFactor , true , null , ref BenchmarkSetupCleanupValueTask . Counter ) ;
202
+
203
+ // GlobalSetup/GlobalCleanup
204
+ action = BenchmarkActionFactory . CreateGlobalSetup ( descriptor , new BenchmarkSetupCleanupValueTask ( ) ) ;
205
+ TestInvoke ( action , 1 , false , null , ref BenchmarkSetupCleanupValueTask . Counter ) ;
206
+ action = BenchmarkActionFactory . CreateGlobalCleanup ( descriptor , new BenchmarkSetupCleanupValueTask ( ) ) ;
207
+ TestInvoke ( action , 1 , false , null , ref BenchmarkSetupCleanupValueTask . Counter ) ;
208
+
209
+ // GlobalSetup/GlobalCleanup (empty)
210
+ descriptor = new Descriptor ( typeof ( BenchmarkSetupCleanupValueTask ) , targetMethod ) ;
211
+ action = BenchmarkActionFactory . CreateGlobalSetup ( descriptor , new BenchmarkSetupCleanupValueTask ( ) ) ;
212
+ TestInvoke ( action , unrollFactor , true , null , ref BenchmarkSetupCleanupValueTask . Counter ) ;
213
+ action = BenchmarkActionFactory . CreateGlobalCleanup ( descriptor , new BenchmarkSetupCleanupValueTask ( ) ) ;
214
+ TestInvoke ( action , unrollFactor , true , null , ref BenchmarkSetupCleanupValueTask . Counter ) ;
215
+
216
+ // Dummy (just in case something may broke)
217
+ action = BenchmarkActionFactory . CreateDummy ( ) ;
218
+ TestInvoke ( action , unrollFactor , true , null , ref BenchmarkSetupCleanupValueTask . Counter ) ;
219
+ action = BenchmarkActionFactory . CreateDummy ( ) ;
220
+ TestInvoke ( action , unrollFactor , true , null , ref BenchmarkSetupCleanupValueTask . Counter ) ;
138
221
}
139
222
140
223
private static object GetDefault ( Type type )
@@ -147,36 +230,36 @@ private static object GetDefault(Type type)
147
230
}
148
231
149
232
[ AssertionMethod ]
150
- private void TestInvoke ( BenchmarkAction benchmarkAction , int unrollFactor , bool isIdle , object expectedResult )
233
+ private void TestInvoke ( BenchmarkAction benchmarkAction , int unrollFactor , bool isIdle , object expectedResult , ref int counter )
151
234
{
152
235
try
153
236
{
154
- BenchmarkAllCases . Counter = 0 ;
237
+ counter = 0 ;
155
238
156
239
if ( isIdle )
157
240
{
158
241
benchmarkAction . InvokeSingle ( ) ;
159
- Assert . Equal ( 0 , BenchmarkAllCases . Counter ) ;
242
+ Assert . Equal ( 0 , counter ) ;
160
243
benchmarkAction . InvokeMultiple ( 0 ) ;
161
- Assert . Equal ( 0 , BenchmarkAllCases . Counter ) ;
244
+ Assert . Equal ( 0 , counter ) ;
162
245
benchmarkAction . InvokeMultiple ( 11 ) ;
163
- Assert . Equal ( 0 , BenchmarkAllCases . Counter ) ;
246
+ Assert . Equal ( 0 , counter ) ;
164
247
}
165
248
else
166
249
{
167
250
benchmarkAction . InvokeSingle ( ) ;
168
- Assert . Equal ( 1 , BenchmarkAllCases . Counter ) ;
251
+ Assert . Equal ( 1 , counter ) ;
169
252
benchmarkAction . InvokeMultiple ( 0 ) ;
170
- Assert . Equal ( 1 , BenchmarkAllCases . Counter ) ;
253
+ Assert . Equal ( 1 , counter ) ;
171
254
benchmarkAction . InvokeMultiple ( 11 ) ;
172
- Assert . Equal ( BenchmarkAllCases . Counter , 1 + unrollFactor * 11 ) ;
255
+ Assert . Equal ( 1 + unrollFactor * 11 , counter ) ;
173
256
}
174
257
175
258
Assert . Equal ( benchmarkAction . LastRunResult , expectedResult ) ;
176
259
}
177
260
finally
178
261
{
179
- BenchmarkAllCases . Counter = 0 ;
262
+ counter = 0 ;
180
263
}
181
264
}
182
265
@@ -244,6 +327,13 @@ public async Task InvokeOnceTaskAsync()
244
327
Interlocked . Increment ( ref Counter ) ;
245
328
}
246
329
330
+ [ Benchmark ]
331
+ public async ValueTask InvokeOnceValueTaskAsync ( )
332
+ {
333
+ await Task . Yield ( ) ;
334
+ Interlocked . Increment ( ref Counter ) ;
335
+ }
336
+
247
337
[ Benchmark ]
248
338
public string InvokeOnceRefType ( )
249
339
{
@@ -273,5 +363,57 @@ public ValueTask<decimal> InvokeOnceValueTaskOfT()
273
363
return new ValueTask < decimal > ( DecimalResult ) ;
274
364
}
275
365
}
366
+
367
+ [ UsedImplicitly ( ImplicitUseTargetFlags . WithMembers ) ]
368
+ public class BenchmarkSetupCleanupTask
369
+ {
370
+ public static int Counter ;
371
+
372
+ [ GlobalSetup ]
373
+ public static async Task GlobalSetup ( )
374
+ {
375
+ await Task . Yield ( ) ;
376
+ Interlocked . Increment ( ref Counter ) ;
377
+ }
378
+
379
+ [ GlobalCleanup ]
380
+ public async Task GlobalCleanup ( )
381
+ {
382
+ await Task . Yield ( ) ;
383
+ Interlocked . Increment ( ref Counter ) ;
384
+ }
385
+
386
+ [ Benchmark ]
387
+ public void InvokeOnceVoid ( )
388
+ {
389
+ Interlocked . Increment ( ref Counter ) ;
390
+ }
391
+ }
392
+
393
+ [ UsedImplicitly ( ImplicitUseTargetFlags . WithMembers ) ]
394
+ public class BenchmarkSetupCleanupValueTask
395
+ {
396
+ public static int Counter ;
397
+
398
+ [ GlobalSetup ]
399
+ public static async ValueTask GlobalSetup ( )
400
+ {
401
+ await Task . Yield ( ) ;
402
+ Interlocked . Increment ( ref Counter ) ;
403
+ }
404
+
405
+ [ GlobalCleanup ]
406
+ public async ValueTask GlobalCleanup ( )
407
+ {
408
+ await Task . Yield ( ) ;
409
+ Interlocked . Increment ( ref Counter ) ;
410
+ }
411
+
412
+ [ Benchmark ]
413
+ public void InvokeOnceVoid ( )
414
+ {
415
+ Interlocked . Increment ( ref Counter ) ;
416
+ }
417
+ }
276
418
}
277
419
}
0 commit comments