-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUnityExtensionBenchmark.cs
172 lines (146 loc) · 4.82 KB
/
UnityExtensionBenchmark.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
using System.Linq;
using System.Threading;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Engines;
using BenchmarkDotNet.Environments;
using BenchmarkDotNet.Jobs;
using Unity;
namespace LazyProxy.Unity.Benchmarks
{
[Config(typeof(BenchmarkConfig))]
public class UnityExtensionBenchmark
{
private class BenchmarkConfig : ManualConfig
{
public BenchmarkConfig()
{
Add(Job.Default.With(RunStrategy.ColdStart)
.With(Runtime.Clr)
.WithTargetCount(1)
.WithInvocationCount(1)
.WithLaunchCount(10)
);
Add(Job.Default.With(RunStrategy.ColdStart)
.With(Runtime.Core)
.WithTargetCount(1)
.WithInvocationCount(1)
.WithLaunchCount(10)
);
Add(DefaultConfig.Instance.GetColumnProviders().ToArray());
}
}
private IUnityContainer _container;
private ITestService _service;
[GlobalSetup(Target = nameof(RegisterType) + "," + nameof(RegisterLazy))]
public void GlobalSetupForRegistrationBenchmarks()
{
_container = new UnityContainer();
}
[Benchmark]
public IUnityContainer RegisterType()
{
return _container.RegisterType<ITestService, TestService>();
}
[Benchmark]
public IUnityContainer RegisterLazy()
{
return _container.RegisterLazy<ITestService, TestService>();
}
[GlobalSetup(Target = nameof(ColdResolveType))]
public void GlobalSetupForColdResolveType()
{
_container = new UnityContainer().RegisterType<ITestService, TestService>();
}
[Benchmark]
public ITestService ColdResolveType()
{
return _container.Resolve<ITestService>();
}
[GlobalSetup(Target = nameof(ColdResolveLazy))]
public void GlobalSetupForColdResolveLazy()
{
_container = new UnityContainer().RegisterLazy<ITestService, TestService>();
}
[Benchmark]
public ITestService ColdResolveLazy()
{
return _container.Resolve<ITestService>();
}
[GlobalSetup(Target = nameof(HotResolveType))]
public void GlobalSetupForHotResolveType()
{
_container = new UnityContainer().RegisterType<ITestService, TestService>();
_container.Resolve<ITestService>();
}
[Benchmark]
public ITestService HotResolveType()
{
return _container.Resolve<ITestService>();
}
[GlobalSetup(Target = nameof(HotResolveLazy))]
public void GlobalSetupForHotResolveLazy()
{
_container = new UnityContainer().RegisterLazy<ITestService, TestService>();
_container.Resolve<ITestService>();
}
[Benchmark]
public ITestService HotResolveLazy()
{
return _container.Resolve<ITestService>();
}
[GlobalSetup(Target = nameof(InvokeMethod))]
public void GlobalSetupForInvokeMethod()
{
_service = new UnityContainer()
.RegisterType<ITestService, TestService>()
.Resolve<ITestService>();
}
[Benchmark]
public int InvokeMethod()
{
return _service.Method();
}
[GlobalSetup(Target = nameof(InvokeLazyMethodFirstTime))]
public void GlobalSetupForInvokeLazyMethodFirstTime()
{
_service = new UnityContainer()
.RegisterLazy<ITestService, TestService>()
.Resolve<ITestService>();
}
[Benchmark]
public int InvokeLazyMethodFirstTime()
{
return _service.Method();
}
[GlobalSetup(Target = nameof(InvokeLazyMethodSecondTime))]
public void GlobalSetupForInvokeLazyMethodSecondTime()
{
_service = new UnityContainer()
.RegisterLazy<ITestService, TestService>()
.Resolve<ITestService>();
_service.Method();
}
[Benchmark]
public int InvokeLazyMethodSecondTime()
{
return _service.Method();
}
}
public interface ITestService
{
int Method();
}
// ReSharper disable once ClassNeverInstantiated.Global
public class TestService : ITestService
{
private readonly int _threadId;
public TestService()
{
// Emulate some hard work (e.g. other services resolving).
Thread.Sleep(10);
_threadId = Thread.CurrentThread.ManagedThreadId;
}
public int Method() => _threadId;
}
}