IoC Container Benchmark - Unity, Windsor, StructureMap and Spring.NET

简介:

There are a number of inversion of control containers out there so I thought it would be an interesting experiment to do a simple benchmark. There are different ways that one can instantiate a type in .NET, for example via the new operator, Activator, GetUninitializedObject and Dynamic Method. The performance difference between these methods are in some cases quite high, maybe the same is true for these IoC containers? Granted IoC containers do more than just create objects so other factors will probably play a big role in the results.

So here are the contestants:

I have been using Castle Windsor since 2005 and I think it is the best of the bunch, so I guess I am unconsciously biased toward Windsor.  However I will try to make this benchmark as objective as I can.

The scenario for this test:

  • Have each IoC container resolve a UserController 1000 000 times
  • The UserController will have two constructor dependencies
  • Run the test with transient (new instance for each resolve) and singleton components

The UserController looks like this:

public class UserController

{

private IUserRepository repository;

private IAuthentificationService authService;

public UserController(IUserRepository repository, IAuthentificationService authService)

{

this.repository = repository;

this.authService = authService;

}

}

I have also a general container interface that the benchmark engine will use. Each container will implement this interface.

public interface IContainer

{

string Name { get; }

T Resolve<T>();

void SetupForTransientTest();

void SetupForSingletonTest();

}

All tests used the latest released version of each library. Before you interpret these charts please observe that the measurement is for one million component resolves which means the actual time difference between each container is actually very small.

Here are the results when all components were setup as singletons:

clip_image002

Here are the results when all components were setup as transient:

clip_image004

So what does these charts tell us? Lets take the biggest difference in the transient case, Spring.NET took 44.149 seconds and Unity took 8.164 seconds, what is the actual difference when resolving a single instance?

Spring.NET : 44.149 / 1000000 = 0.000044149 seconds

Unity : 8.164 / 1000000 = 0.000008164 seconds

So the actual difference is only about 36 microseconds. Another way to put these values into perspective is to compare against the new operator. I created a NewOperatorContainer with a resolve method that looks like this:

public T Resolve<T>()

{

object o = new UserController(new LdapUserRepository(), new DefaultAuthentificationService());

return (T) o;

}

OK, comparing the above with an inversion of control container is like comparing apples to oranges, an IoC handles so much more than just object creation. Also an IoC cannot use the new operator directly but must use one of the other methods. My guess is that all IoC containers in this test uses an approach which involve IL Generation which if cashed comes close to using the new operator directly. Anyway I think it will show just how small the difference between the real IoC containers are. In order to visualize this I needed to invert the values so that high means fast and low means slow.

image

http://lh3.ggpht.com/torkel.odegaard/SALo6OJA3yI/AAAAAAAAAWg/Nhrvuby1VUE/s1600-h/IoCInversed[5].png

Update: The above chart can be very misleading. The x-axis is not seconds but 1/s. I hope it shows that the difference between the containers are very small compared to instantiating the objects manually.

OK, can we draw any conclusion from the test? Well I think we can say that performance should not be an issue when choosing one of these IoC containers. The difference is too small. When you choose which container to use you should consider other aspects, like how invasive the container is to they way you want to work.

For the complete code: IoCBenchmark.zip

Posted by Torkel Ödegaard at 6:46 AM

Labels: BenchmarksC#CastleIoC

13 comments:

Anonymous said...

IMHO your last graph is very misleading. why do you use the inverse values? use the usual values and point out how tiny the "new" value is - or if you feel like the inverse is beneficial at least label your axis correctly. 
otherwise nice article

April 14, 2008 1:20 PM clip_image006

Torkel Ödegaard said...

Yes, you are right. I also feel that the last graph is too misleading. I will correct it when I get home from work.

April 14, 2008 1:25 PM clip_image006[1]

Joshua said...

I would interested to see how Ninject compares.

April 14, 2008 6:57 PM clip_image006[2]

Torkel Ödegaard said...

I guess it should have been in the test having a slogan like "Lightning-fast dependency injection for .NET" 
Hopefully I will have time to do it later in the week, thanks for the tip!

April 14, 2008 7:40 PM clip_image006[3]

Bil Simser said...

This is interesting but IMHO a bit of a waste of time. I mean, what application would *ever* need to try to create a million objects all at once? Even in a tight loop you *might* create a few thousand objects. I just can't think of any use case where this situation would ever manifest itself so to me the numbers are pretty meaningless. Interesting to look at, but not of any value to judge something by. If the differences were significant with say a few hundred objects then maybe this exercise would be worth something.

April 16, 2008 3:53 PM clip_image006[4]

Torkel Ödegaard said...

Yes, I agree that this is not a realistic scenario. Doing one million resolves is not something that do in one request. That was just to accumulate the performance difference to see if there were any substantial difference. 
I don't think it was a waste of time, because it could have potentially been a big meaningful difference between them. Now it turned out that there weren't but that didn't make the test meaningless, because now you know :)

April 16, 2008 4:25 PM clip_image006[5]

Nate Kohari said...

Nice post. I'd also be interested to know how Ninject does in comparison. :) 
Bil's right, though, that it wouldn't typically make a difference, because most applications don't need to create 1,000,000 instances. However, faster IoC also means that you can use it in places that you wouldn't otherwise be able to -- for example, devices that support the compact framework. 
Still a worthwhile study, and it's interesting to see the results!

April 16, 2008 9:43 PM clip_image006[6]

Ruurd Boeke said...

I'm sorry, but if you are going to do a test and find a huge relative difference, you should not disregard it by looking at the absolute difference. What's the point in doing the test than? 
I think it's pretty clear that no container takes a very long time, so what were you hoping to find? 
(no disrespect meant, I clicked on the post because I was curious as well ;-) )

April 16, 2008 10:09 PM clip_image006[7]

Torkel Ödegaard said...

Well the point was to check if there was any relevant difference, not to check which was the fastest. 
And I think you can actually disregard a relative difference if that difference is still not relevant when you look at the absolute performance. 
My conclusion to the test was that the relative difference was too small to make any relevant difference in real applications.

April 16, 2008 10:41 PM clip_image006[8]

Anonymous said...

Nice benchmark. A functional comparison for the used Ioc Containers would be interesting. Where do the performance differences come from?

April 17, 2008 9:20 AM clip_image006[9]

Nick said...

I think the comment about functional differences is important - what are you getting for you time? 
In a real app with more components, the factors affecting container performance will shift from the expense of creating instances, to the expense associated with algorithms that are affected by the number of instances (e.g. which instance from many should be returned?) and algorithms that take a time proportional to the size of the dependency graph (e.g. circular dependency checking.) 
You can never tell where that performance bottleneck is going to be until you measure it ;)

April 20, 2008 8:09 AM clip_image006[10]

sharkboy said...

I think what this page shows is that using IOC containers will not make your app significantly slower. What makes apps slow are poor design, bad databases, and network latency. 
As to which one to use, you have to try and figure out which one has a future. 
I would rule out Spring.Net because it is a Java port and will always be several steps behind in supporting the latest .Net framework features. 
StructureMap is cool but since only one guy is supporting it I would expect the project to die once that dude gets tired of dealing with it. 
Unity may be the safe bet being that it has "Official" support from Microsoft. The MS guys where starting to promote this at TechEd this year. On the other hand, don't expect any innovation from Microsoft. 
If you want a supported component, go with Unity. If you want innovation, go Windsor.

June 16, 2008 6:24 AM clip_image006[11]

Luke said...

You clearly don't know Jeremy Miller very well if you think that's what'll happen with StructureMap.

July 6, 2008 5:31 PM


本文转自海天一鸥博客园博客,原文链接:http://www.cnblogs.com/sgsoft/archive/2011/07/28/2119967.html,如需转载请自行联系原作者

相关文章
|
8月前
|
XML Java 测试技术
《深入理解Spring》:IoC容器核心原理与实战
Spring IoC通过控制反转与依赖注入实现对象间的解耦,由容器统一管理Bean的生命周期与依赖关系。支持XML、注解和Java配置三种方式,结合作用域、条件化配置与循环依赖处理等机制,提升应用的可维护性与可测试性,是现代Java开发的核心基石。
|
XML Java 测试技术
Spring IOC—基于注解配置和管理Bean 万字详解(通俗易懂)
Spring 第三节 IOC——基于注解配置和管理Bean 万字详解!
1016 26
|
12月前
|
XML 人工智能 Java
Spring IOC 到底是什么?
IOC(控制反转)是一种设计思想,主要用于解耦代码,简化依赖管理。其核心是将对象的创建和管理交给容器处理,而非由程序直接硬编码实现。通过IOC,开发者无需手动new对象,而是由框架负责实例化、装配和管理依赖对象。常见应用如Spring框架中的BeanFactory和ApplicationContext,它们实现了依赖注入和动态管理功能,提升了代码的灵活性与可维护性。
279 1
|
XML Java 数据格式
Spring IoC容器的设计与实现
Spring 是一个功能强大且模块化的 Java 开发框架,其核心架构围绕 IoC 容器、AOP、数据访问与集成、Web 层支持等展开。其中,`BeanFactory` 和 `ApplicationContext` 是 Spring 容器的核心组件,分别定位为基础容器和高级容器,前者提供轻量级的 Bean 管理,后者扩展了事件发布、国际化等功能。
349 18
|
XML Java 数据格式
【SpringFramework】Spring IoC-基于XML的实现
本文主要讲解SpringFramework中IoC和DI相关概念,及基于XML的实现方式。
350 69
|
XML Java 数据格式
京东一面:spring ioc容器本质是什么? ioc容器启动的步骤有哪些?
京东一面:spring ioc容器本质是什么? ioc容器启动的步骤有哪些?
|
Java 容器 Spring
什么是Spring IOC 和DI ?
IOC : 控制翻转 , 它把传统上由程序代码直接操控的对象的调用权交给容 器,通过容器来实现对象组件的装配和管理。所谓的“控制反转”概念就是对组件对象控制权的转 移,从程序代码本身转移到了外部容器。 DI : 依赖注入,在我们创建对象的过程中,把对象依赖的属性注入到我们的类中。
|
设计模式 XML Java
【23种设计模式·全精解析 | 自定义Spring框架篇】Spring核心源码分析+自定义Spring的IOC功能,依赖注入功能
本文详细介绍了Spring框架的核心功能,并通过手写自定义Spring框架的方式,深入理解了Spring的IOC(控制反转)和DI(依赖注入)功能,并且学会实际运用设计模式到真实开发中。
【23种设计模式·全精解析 | 自定义Spring框架篇】Spring核心源码分析+自定义Spring的IOC功能,依赖注入功能
|
Java Spring 容器
【SpringFramework】Spring IoC-基于注解的实现
本文主要记录基于Spring注解实现IoC容器和DI相关知识。
278 21
|
存储 Java 应用服务中间件
【Spring】IoC和DI,控制反转,Bean对象的获取方式
IoC,DI,控制反转容器,Bean的基本常识,类注解@Controller,获取Bean对象的常用三种方式
594 12