A couple of years ago (~August 2012), we had to choose an IoC container for a new .NET project. Given the many mature options with largely similar feature sets, we based the decision on their performance. The existing benchmarks published on the web left us dissatisfied due to overly simplistic test data. Ideally, the test data should have a large dependency graph to thoroughly exercise the container's ability to resolve.
Using T4 templates, it's simple to create C# classes and interfaces that fit that structure. Using a generic interface to configure the container, it is then easy to provide an implementation for each candidate.
The configuration of generated test data is:
- 8 levels of depth for the dependency tree
- 100 types per level
- Between 0 and 8 dependencies for each type (excluding level 0)
- 1 in 5 types are registered as singleton (20%)
These parameters can be tweaked (TestTypeGeneratorSetup.ttinclude) to simulate alternative scenarios.
It's an exaggerated example but serves the purpose of stress testing. In fact, some of the containers fail to complete the test.
|Container||2012 Version||2012 Time Elapsed (Sec)||2014 Version||Time Elapsed (Sec)|
Dynamo & Hiro failed to complete the test.
From this, it appears that StructureMap is the fastest container to register and resolve all 800 types.
Updating to the latest version (as of Oct 2014) shows that Unity is the biggest winner in terms of speed gain. The rest are largely unchanged.
So if you’re looking for outright performance, give StuctureMap a look.
Since version 3.0.0 it also comes as a Portable Class Library (PCL) so is easy to recommend.
Check out / fork the code on github:
All credits for this blog post and associated source code go to Paul Spiteri.
Co-founder and Chief Technology Officer,
Adaptive Financial Consulting