As most of you know, my favorite is SimpleInjector. I use it in all my projects. Since Sitecore 8.2 was released yesterday (August 30th 2016), I decided to do a performance benchmark against Microsoft Extensions DependencyInjection which is used as part of Sitecore to provide DependencyInjection.
Kam has written a blog post: Dependency Injection in Sitecore 8.2 which talks about the Sitecore integration.
I ran these tests on a virtual with Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz with 8GB memory.
My source is available on GitHub: IoC Performance
In order to do this I have taken two approaches. First, I used the repo from Daniel Palme and his blog post: IoC Container Benchmark – Performance comparison and I updated the NuGet packages. Here is a blurb from Daniels post about the setup:
The test setup
Several benchmarks are executed to test the performance of the containers in different scenarios. Every container is initialized with a couple of interfaces and their corresponding implementations. The types are registered with different lifetimes to support the various benchmarks.
Every interface is resolved 500.000 times during the benchmark and the time is measured in milliseconds. Each test is executed single threaded and multi threaded.
The benchmarks:
- Singleton: Objects with is singleton lifetime are resolved
- Transient: Objects with is transient lifetime are resolved
- Combined: Objects with two dependencies (singleton and transient lifetime) are resolved
- Complex: Objects with several nested dependencies are resolved
- Property: Objects which require property injection are resolved
- Generics: Objects with a generic dependency are resolved
- IEnumerable: Several objects that implement the same interface are resolved
- Conditional: Objects with a conditional dependency are resolved
- Child Container: Objects are resolved trough a child container
- Interception With Proxy: Objects with a dynamically generated wrapper are resolved
- Prepare And Register: Initializes container and registers some basic elements (executed 3.000 times)
- Prepare And Register And Simple Resolve: Initializes container and registers some basic elements. The resolves two objects (executed 3.000 times)
First value: Time of single-threaded execution in [ms]
Second value: Time of multi-threaded execution in [ms]
That produced the following results:
Version | Singleton | Transient | Combined | Complex | Property | Generics | IEnumerable | Conditional | Child Container | Interception With Proxy | Prepare And Register | Prepare And Register And Simple Resolve | ||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
No |
56 89 |
72
104 |
105
124 |
134
134 |
148
146 |
96
110 |
305
231 |
98
118 |
606
391 |
72
105 |
2 | 3 | ||
AutoFac | 4.1.0.0 | 602
508 |
1240
785 |
3760
2108 |
10242
6385 |
18682
10466 |
2632
1592 |
10363
5634 |
37968
20398 |
|||||
Microsoft Extensions DependencyInjection |
1.0.0.20622 | 156
220 |
140
144 |
320
318 |
729
830 |
198
186 |
738
448 |
18 | 25 | |||||
Ninject | 3.2.2.0 | 3932
2588 |
13965
11564 |
37197
25070 |
103800
64927 |
93449
69829 |
37011
26061 |
91829
68581 |
34162
23586 |
62954750*
75133444* |
102117
99437 |
129042 | 115231 | |
SimpleInjector | 3.2.2.0 | 66
101 |
87
120 |
151
159 |
188
176 |
270
218 |
100
131 |
854 520 |
121
144 |
28926
30097 |
419 | 3359 |
Second, I took the repos from:
IOCBattle by Martin From
IOCBattle by Nathanael Mann
To get the following results:
Version | Singleton Reg. (ms) | Singleton Resolve (1,000,000) (ms) | Transient Reg. (ms) | Transient Resolve (1,000,000) (ms) | |
---|---|---|---|---|---|
new () | 0.0726 | 57.6387 | 0.0683 | 200.6526 | |
AutoFac Lambda | 4.1.0.0 | 54.8336 | 453.2864 | 2.1957 | 6569.4292 |
AutoFac | 4.1.0.0 | 4.8632 | 464.7712 | 0.7575 | 16183.6045 |
Microsoft Extensions DependencyInjection |
1.0.0.20622 | 0.5311 | 66.0343 | 0.4936 | 198.7997 |
Ninject | 3.2.2.0 | 34.5873 | 3343.113 | 1.6425 | 186098.403 |
SimpleInjector | 3.2.2.0 | 31.0477 | 104.6839 | 2.5184 | 225.4272 |
If you have any questions or concerns, please get in touch with me. (@akshaysura13 on twitter or on Slack).
Links:
IoC Container Benchmark – Performance comparison
IoC Battle in 2015 results: Using Ninject – think again!
*Due to the updated AutoFac version I was getting some issues with Daniels code. I will resolve it at a later date and retest it.
Go again, but this time add in DryIoC, which somehow is actually faster than not using an IoC at all !?
I would find this article a lot more useful if you tried to draw some conclusions from your setup, possibly discussing pros and cons, or limitations with your setup.
I’m also sad to see that StructureMap is not even part of the comparison anywhere.
So far as I can tell at least, Microsoft.Extensions.DependencyInjection seems like a very strong alternative, especially since it’s made and maintained by Microsoft. Given that you can live with netstandard2.0 or are already working with .net core 2.0.
Thanks for the good summary!