我简单介绍过Autofac的使用,有人希望能有个性能上的测试,考虑到有那么多的IOC框架,而主流的有:Castle Windsor、微软企业库中的Unity、Spring.NET、StructureMap、Ninject等等。本篇文章主要针对这些IOC框架编写测试程序。
我们提供的服务有:网站设计、做网站、微信公众号开发、网站优化、网站认证、义马ssl等。为上千家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的义马网站制作公司
Autofac下载地址:http://code.google.com/p/autofac/
Castle Windsor下载地址:http://sourceforge.net/projects/castleproject/files/Windsor/2.5/Castle.Windsor.2.5.3.zip/download
Unity下载地址:http://entlib.codeplex.com/
Spring.NET下载地址:http://www.springframework.net/
StructureMap下载地址:http://sourceforge.net/projects/structuremap/files/
Ninject下载地址:http://ninject.org/download
其中,测试程序均采用***的类库。
基础工作
1、程序还是引用上一篇的示例作为测试背景。
2、编写一个性能计数器,这里我采用老赵写的一个CodeTimer的类,具体介绍见:http://www.cnblogs.com/JeffreyZhao/archive/2009/03/10/codetimer.html
使用方式类似于:
- int iteration = 100 * 1000;string s = "";
- CodeTimer.Time("String Concat", iteration, () => { s += "a"; });
- StringBuilder sb = new StringBuilder();
- CodeTimer.Time("StringBuilder", iteration, () => { sb.Append("a"); });
3、编写一个IRunner运行接口:
- public interface IRunner
- {
- void Start(RunType runType);
- }
以及RunnerBase抽象基础运行类:
- public abstract class RunnerBase
- {
- private int _iteration = Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["Iteration"] ?? "10000");
- internal int Iteration
- {
- get { return _iteration; }
- }
- internal void Time(Action action)
- {
- CodeTimer.Time(Name, Iteration, action);
- }
- protected abstract string Name { get; }
- }
这里_iteration表示测试运行次数,通过配置文件来设置值。Time方法通过计数器对action方法进行Iteration次迭代。
编写一个RunManager的运行管理器:
- public class RunManager
- {
- public static void Start(IRunner runner)
- {
- Start(runner, RunType.Transient);
- }
- public static void Start(IRunner runner, RunType runType)
- {
- runner.Start(runType);
- }
- }
在测试中,我采用两种方式的性能比较,一个是单例状态,一个是非单例状态:
- ///
- /// 运行状态
- ///
- public enum RunType
- {
- ///
- /// 单例
- ///
- Singleton,
- ///
- /// 瞬时
- ///
- Transient
- }
好了,现在我的程序只要继承RunnerBase以及IRunnre接口,就可以实现各个IOC框架的初始化装配的工作了。基础工作已经做好。
#p#
各个IOC框架测试程序
1、Autofac:
- public class AutofacRunner : RunnerBase, IRunner
- {
- protected override string Name
- {
- get { return "Autofac"; }
- }
- public void Start(RunType runType)
- {
- var builder = new ContainerBuilder();
- //if (runType == RunType.Singleton)
- // builder.RegisterType
().SingleInstance(); - //else
- // builder.RegisterType
(); - //builder.RegisterType
().As (); - ////builder.RegisterModule(new ConfigurationSettingsReader("autofac"));
- builder.RegisterType
().As (); - if (runType == RunType.Singleton)
- builder.Register(c => new DatabaseManager(c.Resolve
())).SingleInstance(); - else
- builder.Register(c => new DatabaseManager(c.Resolve
())); - var container = builder.Build();
- Time(() =>
- {
- var manager = container.Resolve
(); - manager.Search("SELECT * FROM USER");
- });
- container.Dispose();
- }
- }
2、Castle Windsor:
- public class WindsorRunner : RunnerBase, IRunner
- {
- protected override string Name
- {
- get { return "Castle Windsor"; }
- }
- public void Start(RunType runType)
- {
- var container = new WindsorContainer();
- if(runType == RunType.Singleton)
- container.Register(Component.For(typeof(DatabaseManager)).LifeStyle.Singleton);
- else
- container.Register(Component.For(typeof(DatabaseManager)).LifeStyle.Transient);
- container.Register(Component.For(typeof(IDatabase)).ImplementedBy(typeof(SqlDatabase)));
- Time(() =>
- {
- var manager = container.Resolve
(); - manager.Search("SELECT * FROM USER");
- });
- }
- }
3、Unity:
- public class UnityRunner : RunnerBase, IRunner
- {
- protected override string Name
- {
- get { return "Unity"; }
- }
- public void Start(RunType runType)
- {
- var container = new UnityContainer();
- if(runType == RunType.Singleton)
- container.RegisterType
(new ContainerControlledLifetimeManager()); - else
- container.RegisterType
(new TransientLifetimeManager()); - container.RegisterType
(); - Time(() =>
- {
- var manager = container.Resolve
(); - manager.Search("SELECT * FROM USER");
- });
- }
- }
4、Spring.NET:
- public class SpringRunner : RunnerBase, IRunner
- {
- protected override string Name
- {
- get { return "Spring.NET"; }
- }
- public void Start(RunType runType)
- {
- string databaseManagerName;
- if (runType == RunType.Singleton)
- databaseManagerName = "DatabaseManager_Singleton";
- else
- databaseManagerName = "DatabaseManager_Transient";
- Time(() =>
- {
- IApplicationContext context = ContextRegistry.GetContext();
- var manager = (DatabaseManager)context.GetObject(databaseManagerName);
- manager.Search("SELECT * FROM USER");
- });
- }
- }
5、StructureMap:
- public class StructureMapRunner : RunnerBase, IRunner
- {
- protected override string Name
- {
- get { return "StructureMap"; }
- }
- public void Start(RunType runType)
- {
- ObjectFactory.Initialize(container =>
- {
- if (runType == RunType.Singleton)
- container.ForRequestedType
().Singleton(); - else
- container.ForRequestedType
(); - container.ForRequestedType
().TheDefaultIsConcreteType (); - });
- Time(() =>
- {
- var manager = ObjectFactory.GetInstance
(); - manager.Search("SELECT * FROM USER");
- });
- }
- }
6、Ninject:
- public class NinjectRunner : RunnerBase, IRunner
- {
- protected override string Name
- {
- get { return "Ninject"; }
- }
- public void Start(RunType runType)
- {
- IKernel kernel = new StandardKernel(new MyNinjectModule(runType));
- Time(() =>
- {
- var manager = kernel.Get
(); - manager.Search("SELECT * FROM USER");
- });
- }
- }
客户端测试程序
- static void Main(string[] args)
- {
- CodeTimer.Initialize();
- Console.WriteLine("IOC - Singleton");
- // Autofac Singleton
- RunManager.Start(new AutofacRunner(), RunType.Singleton);
- // Castle Windsor
- RunManager.Start(new WindsorRunner(), RunType.Singleton);
- // Unity
- RunManager.Start(new UnityRunner(), RunType.Singleton);
- // Spring.NET
- RunManager.Start(new SpringRunner(), RunType.Singleton);
- // StructureMap
- RunManager.Start(new StructureMapRunner(), RunType.Singleton);
- // Ninject
- RunManager.Start(new NinjectRunner(), RunType.Singleton);
- Console.WriteLine("===================================");
- Console.WriteLine("IOC - Transient");
- // Autofac Singleton
- RunManager.Start(new AutofacRunner(), RunType.Transient);
- // Castle Windsor
- RunManager.Start(new WindsorRunner(), RunType.Transient);
- // Unity
- RunManager.Start(new UnityRunner(), RunType.Transient);
- // Spring.NET
- RunManager.Start(new SpringRunner(), RunType.Transient);
- // StructureMap
- RunManager.Start(new StructureMapRunner(), RunType.Transient);
- // Ninject
- RunManager.Start(new NinjectRunner(), RunType.Transient);
- Console.ReadKey();
- }
通过修改App.config的Iteration配置值,来设置迭代次数。
运行结果
1、Iteration=1000:
分析:在千数量级时,Autofac,CastleWindsor、StructureMap基本差不多,效率上比其他的要高。
2、Iteration=10000:
分析:在万数量级时,Autofac,CastleWindsor,StructureMap基本效率还是差不多,其中StructureMap效率稍稍有些下降;Spring.NET以及Ninject的性能比较低。
3、Iteration=100000:
分析:在十万数量级时,CastleWindsor的效率开始下降,而在Transient方面,StructureMap和Autofac基本差不多。
4、Iteration=1000000:
分析:在百万数量级时,Autofac和StructureMap两者还是保持比较高的效率,并且在Transient方面,StructureMap已经超过了Autofac。
总结:从测试中,可以看出Autofac和StructureMap在性能上面还是体现出比较大的优势,Ninject可以说性能上较低。而Spring.NET不仅仅专注于IOC方面,它还专注于其他方方面面的功能,所以在IOC方面的性能不是太高。另外,微软的Unity中规中矩,性能较为稳定,也是一个不错的选择。另外,可能测试程序会有所偏差,希望大家也能够指出问题!
测试程序源代码:IOCPerformanceTest.rar
原文:http://www.cnblogs.com/liping13599168/archive/2011/07/17/2108734.html
当前名称:各大主流.Net的IOC框架性能测试比较
文章位置:http://www.mswzjz.cn/qtweb/news7/505707.html
攀枝花网站建设、攀枝花网站运维推广公司-贝锐智能,是专注品牌与效果的网络营销公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 贝锐智能