-
Notifications
You must be signed in to change notification settings - Fork 272
/
Copy pathActivatorUtilitiesBenchmark.cs
428 lines (356 loc) · 15.4 KB
/
ActivatorUtilitiesBenchmark.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using BenchmarkDotNet.Attributes;
using MicroBenchmarks;
namespace Microsoft.Extensions.DependencyInjection
{
[BenchmarkCategory(Categories.Libraries)]
public class ActivatorUtilitiesBenchmark
{
private ServiceProvider _serviceProvider;
private Type[] _types0;
private Type[] _types2;
private Type[] _types2_OutOfOrder;
private Type[] _types3;
private ObjectFactory _factory0;
private ObjectFactory _factory2;
private ObjectFactory _factory2_OutOfOrder;
private ObjectFactory _factory3;
private object[] _factoryArguments0;
private object[] _factoryArguments1;
private object[] _factoryArguments2;
private object[] _factoryArguments2_OutOfOrder;
private object[] _factoryArguments3;
private object[] _factoryArguments5;
[GlobalSetup]
public void SetUp()
{
ServiceCollection collection = new();
collection.AddSingleton<DependencyA>();
collection.AddSingleton<DependencyB>();
collection.AddSingleton<DependencyC>();
collection.AddSingleton<DependencyD>();
collection.AddSingleton<DependencyE>();
_types0 = new Type[] { };
_types2 = new Type[] { typeof(DependencyA), typeof(DependencyB) };
_types2_OutOfOrder = new Type[] { typeof(DependencyB), typeof(DependencyC) };
_types3 = new Type[] { typeof(DependencyA), typeof(DependencyB), typeof(DependencyC) };
_factory0 = ActivatorUtilities.CreateFactory(typeof(TypeWithVaryingIncomingParametersToBeActivated), _types0);
_factory2 = ActivatorUtilities.CreateFactory(typeof(TypeWithVaryingIncomingParametersToBeActivated), _types2);
_factory2_OutOfOrder = ActivatorUtilities.CreateFactory(typeof(TypeWithVaryingIncomingParametersToBeActivated), _types2_OutOfOrder);
_factory3 = ActivatorUtilities.CreateFactory(typeof(TypeWithVaryingIncomingParametersToBeActivated), _types3);
_factoryArguments0 = Array.Empty<object>();
_factoryArguments1 = new object[] { new DependencyA() };
_factoryArguments2 = new object[] { new DependencyA(), new DependencyB() };
_factoryArguments2_OutOfOrder = new object[] { new DependencyB(), new DependencyC() };
_factoryArguments3 = new object[] { new DependencyA(), new DependencyB(), new DependencyC() };
_factoryArguments5 = new object[] { new DependencyA(), new DependencyB(), new DependencyC(), new DependencyD(), new DependencyE() };
_serviceProvider = collection.BuildServiceProvider();
}
[GlobalCleanup]
public void Cleanup() => _serviceProvider.Dispose();
[Benchmark]
[BenchmarkCategory(Categories.NoAOT)]
public TypeWith1ParameterToBeActivated GetService_1Injected()
{
return _serviceProvider.GetService<TypeWith1ParameterToBeActivated>();
}
[Benchmark]
[BenchmarkCategory(Categories.NoAOT)]
public TypeWith3ParametersToBeActivated GetService_3Injected()
{
return _serviceProvider.GetService<TypeWith3ParametersToBeActivated>();
}
[Benchmark]
[BenchmarkCategory(Categories.NoAOT)]
[MemoryRandomization]
public TypeWith5ParametersToBeActivated GetService_5Injected()
{
return _serviceProvider.GetService<TypeWith5ParametersToBeActivated>();
}
[Benchmark]
[BenchmarkCategory(Categories.NoAOT)]
public TypeWith1ParameterToBeActivated CreateInstance_1() => ActivatorUtilities.CreateInstance<TypeWith1ParameterToBeActivated>(_serviceProvider, _factoryArguments1);
[Benchmark]
[BenchmarkCategory(Categories.NoAOT)]
public TypeWith3ParametersToBeActivated CreateInstance_3() => ActivatorUtilities.CreateInstance<TypeWith3ParametersToBeActivated>(_serviceProvider, _factoryArguments3);
[Benchmark]
[BenchmarkCategory(Categories.NoAOT)]
public TypeWith5ParametersToBeActivated CreateInstance_5() => ActivatorUtilities.CreateInstance<TypeWith5ParametersToBeActivated>(_serviceProvider, _factoryArguments5);
[Benchmark]
[BenchmarkCategory(Categories.NoAOT)]
public TypeWith1ParameterToBeActivated_WithAttrFirst CreateInstance_1_WithAttrFirst() => ActivatorUtilities.CreateInstance<TypeWith1ParameterToBeActivated_WithAttrFirst>(_serviceProvider, _factoryArguments1);
[Benchmark]
[BenchmarkCategory(Categories.NoAOT)]
public TypeWith3ParametersToBeActivated_WithAttrFirst CreateInstance_3_WithAttrFirst() => ActivatorUtilities.CreateInstance<TypeWith3ParametersToBeActivated_WithAttrFirst>(_serviceProvider, _factoryArguments3);
[Benchmark]
[BenchmarkCategory(Categories.NoAOT)]
public TypeWith5ParametersToBeActivated_WithAttrFirst CreateInstance_5_WithAttrFirst() => ActivatorUtilities.CreateInstance<TypeWith5ParametersToBeActivated_WithAttrFirst>(_serviceProvider, _factoryArguments5);
[Benchmark]
[BenchmarkCategory(Categories.NoAOT)]
public TypeWith1ParameterToBeActivated_WithAttrLast CreateInstance_1_WithAttrLast() => ActivatorUtilities.CreateInstance<TypeWith1ParameterToBeActivated_WithAttrLast>(_serviceProvider, _factoryArguments1);
[Benchmark]
[BenchmarkCategory(Categories.NoAOT)]
public TypeWith3ParametersToBeActivated_WithAttrLast CreateInstance_3_WithAttrLast() => ActivatorUtilities.CreateInstance<TypeWith3ParametersToBeActivated_WithAttrLast>(_serviceProvider, _factoryArguments3);
[Benchmark]
[BenchmarkCategory(Categories.NoAOT)]
public TypeWith5ParametersToBeActivated_WithAttrLast CreateInstance_5_WithAttrLast() => ActivatorUtilities.CreateInstance<TypeWith5ParametersToBeActivated_WithAttrLast>(_serviceProvider, _factoryArguments5);
[Benchmark]
[BenchmarkCategory(Categories.NoAOT)]
public TypeWithVaryingIncomingParametersToBeActivated Factory_1Injected_2Explicit()
{
return (TypeWithVaryingIncomingParametersToBeActivated)_factory2(_serviceProvider, _factoryArguments2);
}
[Benchmark]
[BenchmarkCategory(Categories.NoAOT)]
public TypeWithVaryingIncomingParametersToBeActivated Factory_1Injected_2Explicit_OutOfOrder()
{
return (TypeWithVaryingIncomingParametersToBeActivated)_factory2_OutOfOrder(_serviceProvider, _factoryArguments2_OutOfOrder);
}
[Benchmark]
[BenchmarkCategory(Categories.NoAOT)]
public TypeWithVaryingIncomingParametersToBeActivated Factory_3Explicit()
{
return (TypeWithVaryingIncomingParametersToBeActivated)_factory3(_serviceProvider, _factoryArguments3);
}
[Benchmark]
[BenchmarkCategory(Categories.NoAOT)]
public TypeWithVaryingIncomingParametersToBeActivated Factory_3Injected()
{
return (TypeWithVaryingIncomingParametersToBeActivated)_factory0(_serviceProvider, _factoryArguments0);
}
public class TypeWith1ParameterToBeActivated
{
public DependencyA _a;
public TypeWith1ParameterToBeActivated()
{
throw new NotImplementedException();
}
public TypeWith1ParameterToBeActivated(DependencyA a)
{
_a = a;
}
}
public class TypeWith1ParameterToBeActivated_WithAttrFirst
{
public DependencyA _a;
public TypeWith1ParameterToBeActivated_WithAttrFirst()
{
throw new NotImplementedException();
}
[ActivatorUtilitiesConstructor]
public TypeWith1ParameterToBeActivated_WithAttrFirst(DependencyA a)
{
_a = a;
}
}
public class TypeWith1ParameterToBeActivated_WithAttrLast
{
public DependencyA _a;
public TypeWith1ParameterToBeActivated_WithAttrLast()
{
throw new NotImplementedException();
}
[ActivatorUtilitiesConstructor]
public TypeWith1ParameterToBeActivated_WithAttrLast(DependencyA a)
{
_a = a;
}
}
public class TypeWith3ParametersToBeActivated
{
public DependencyA _a;
public DependencyB _b;
public DependencyC _c;
public TypeWith3ParametersToBeActivated()
{
throw new NotImplementedException();
}
public TypeWith3ParametersToBeActivated(int i)
{
throw new NotImplementedException();
}
public TypeWith3ParametersToBeActivated(int i, int i2)
{
throw new NotImplementedException();
}
public TypeWith3ParametersToBeActivated(DependencyA a, DependencyB b, DependencyC c)
{
_a = a;
_b = b;
_c = c;
}
}
public class TypeWith3ParametersToBeActivated_WithAttrFirst
{
public DependencyA _a;
public DependencyB _b;
public DependencyC _c;
[ActivatorUtilitiesConstructor]
public TypeWith3ParametersToBeActivated_WithAttrFirst(DependencyA a, DependencyB b, DependencyC c)
{
_a = a;
_b = b;
_c = c;
}
public TypeWith3ParametersToBeActivated_WithAttrFirst()
{
throw new NotImplementedException();
}
public TypeWith3ParametersToBeActivated_WithAttrFirst(int i)
{
throw new NotImplementedException();
}
public TypeWith3ParametersToBeActivated_WithAttrFirst(int i, int i2)
{
throw new NotImplementedException();
}
}
public class TypeWith3ParametersToBeActivated_WithAttrLast
{
public DependencyA _a;
public DependencyB _b;
public DependencyC _c;
public TypeWith3ParametersToBeActivated_WithAttrLast()
{
throw new NotImplementedException();
}
public TypeWith3ParametersToBeActivated_WithAttrLast(int i)
{
throw new NotImplementedException();
}
public TypeWith3ParametersToBeActivated_WithAttrLast(int i, int i2)
{
throw new NotImplementedException();
}
[ActivatorUtilitiesConstructor]
public TypeWith3ParametersToBeActivated_WithAttrLast(DependencyA a, DependencyB b, DependencyC c)
{
_a = a;
_b = b;
_c = c;
}
}
public class TypeWith5ParametersToBeActivated
{
public DependencyA _a;
public DependencyB _b;
public DependencyC _c;
public DependencyD _d;
public DependencyE _e;
public TypeWith5ParametersToBeActivated()
{
throw new NotImplementedException();
}
public TypeWith5ParametersToBeActivated(int i)
{
throw new NotImplementedException();
}
public TypeWith5ParametersToBeActivated(int i, int i2)
{
throw new NotImplementedException();
}
public TypeWith5ParametersToBeActivated(int i, int i2, int i3)
{
throw new NotImplementedException();
}
public TypeWith5ParametersToBeActivated(int i, int i2, int i3, int i4)
{
throw new NotImplementedException();
}
public TypeWith5ParametersToBeActivated(DependencyA a, DependencyB b, DependencyC c, DependencyD d, DependencyE e)
{
_a = a;
_b = b;
_c = c;
_d = d;
_e = e;
}
}
public class TypeWith5ParametersToBeActivated_WithAttrFirst
{
public DependencyA _a;
public DependencyB _b;
public DependencyC _c;
public DependencyD _d;
public DependencyE _e;
[ActivatorUtilitiesConstructor]
public TypeWith5ParametersToBeActivated_WithAttrFirst(DependencyA a, DependencyB b, DependencyC c, DependencyD d, DependencyE e)
{
_a = a;
_b = b;
_c = c;
_d = d;
_e = e;
}
public TypeWith5ParametersToBeActivated_WithAttrFirst()
{
throw new NotImplementedException();
}
public TypeWith5ParametersToBeActivated_WithAttrFirst(int i)
{
throw new NotImplementedException();
}
public TypeWith5ParametersToBeActivated_WithAttrFirst(int i, int i2)
{
throw new NotImplementedException();
}
public TypeWith5ParametersToBeActivated_WithAttrFirst(int i, int i2, int i3)
{
throw new NotImplementedException();
}
public TypeWith5ParametersToBeActivated_WithAttrFirst(int i, int i2, int i3, int i4)
{
throw new NotImplementedException();
}
}
public class TypeWith5ParametersToBeActivated_WithAttrLast
{
public DependencyA _a;
public DependencyB _b;
public DependencyC _c;
public DependencyD _d;
public DependencyE _e;
public TypeWith5ParametersToBeActivated_WithAttrLast()
{
throw new NotImplementedException();
}
public TypeWith5ParametersToBeActivated_WithAttrLast(int i)
{
throw new NotImplementedException();
}
public TypeWith5ParametersToBeActivated_WithAttrLast(int i, int i2)
{
throw new NotImplementedException();
}
public TypeWith5ParametersToBeActivated_WithAttrLast(int i, int i2, int i3)
{
throw new NotImplementedException();
}
public TypeWith5ParametersToBeActivated_WithAttrLast(int i, int i2, int i3, int i4)
{
throw new NotImplementedException();
}
[ActivatorUtilitiesConstructor]
public TypeWith5ParametersToBeActivated_WithAttrLast(DependencyA a, DependencyB b, DependencyC c, DependencyD d, DependencyE e)
{
_a = a;
_b = b;
_c = c;
_d = d;
_e = e;
}
}
public class TypeWithVaryingIncomingParametersToBeActivated
{
public TypeWithVaryingIncomingParametersToBeActivated(DependencyA a, DependencyB b, DependencyC c) { }
}
public class DependencyA { }
public class DependencyB { }
public class DependencyC { }
public class DependencyD { }
public class DependencyE { }
}
}