-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathIEnumerableGetServiceBenchmark.cs
57 lines (45 loc) · 1.97 KB
/
IEnumerableGetServiceBenchmark.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
// 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 System.Collections.Generic;
using BenchmarkDotNet.Attributes;
using MicroBenchmarks;
namespace Microsoft.Extensions.DependencyInjection
{
[BenchmarkCategory(Categories.Libraries)]
public class GetServiceIEnumerable : ServiceProviderEngineBenchmark
{
private IServiceProvider _serviceProvider;
[GlobalSetup(Target = nameof(Transient))]
public void SetupTransient() => Setup(ServiceLifetime.Transient);
[GlobalSetup(Target = nameof(Scoped))]
public void SetupScoped() => Setup(ServiceLifetime.Scoped);
[GlobalSetup(Target = nameof(Singleton))]
public void SetupSingleton() => Setup(ServiceLifetime.Singleton);
[GlobalCleanup]
public void Cleanup() => ((IDisposable)_serviceProvider).Dispose();
private void Setup(ServiceLifetime lifetime)
{
IServiceCollection services = new ServiceCollection();
for (int i = 0; i < 10; i++)
{
services.Add(ServiceDescriptor.Describe(typeof(A), typeof(A), lifetime));
}
services.Add(ServiceDescriptor.Describe(typeof(B), typeof(B), lifetime));
services.Add(ServiceDescriptor.Describe(typeof(C), typeof(C), lifetime));
_serviceProvider = services.BuildServiceProvider(new ServiceProviderOptions()
{
#if INTERNAL_DI
Mode = ServiceProviderMode
#endif
}).CreateScope().ServiceProvider;
}
[Benchmark]
public object Transient() => _serviceProvider.GetService<IEnumerable<A>>();
[Benchmark]
public object Scoped() => _serviceProvider.GetService<IEnumerable<A>>();
[Benchmark]
public object Singleton() => _serviceProvider.GetService<IEnumerable<A>>();
}
}