多个IoC容器适配器设计及性能测试和容器选择

来源:互联网 发布:mysql查询性能分析器 编辑:程序博客网 时间:2024/06/05 18:07
http://blog.csdn.net/liuweitoo/article/details/8290966
iocIoC容器选择性能测试

目录(?)[-]

  1. 采用的IoC容器和版本
  2. 基础类库服务类库和组件类库及相关的辅助类库
  3. Autofac容器适配器
  4. CastleWindsor容器适配器
  5. SpringNET的容器适配器
  6. 测试结果
    1. 单例模式
    2. 瞬时多例模式
  7. 测试结论
    1. 1 不管是单例还是多例初始化的时间都差不多Autofac略快
    2. 2 不管是单例还是多例非初始化取数据时取数据量大时花费的时间也大
    3. 3 单例时性能比较Autofac略慢但差异不大
    4. 4 多例时性能比较Autofac速度最快也最稳定差异较大
  8. IoC容器选择结论Autofac第一名
    1. A 单例时Autofac会略慢但相差不大
    2. B 多例时Autofac速度明显较快Spring性能下降太厉害
    3. C 整体而言Autofac的优劣较明显
    4. D 性能对比Autofac Windsor Spring
  9. Demo下载

1. 采用的IoC容器和版本

Autofac.2.6.3.862

Castle.Windsor.3.1.0

Spring.Core.2.0.0

 

2. 基础类库:服务类库和组件类库及相关的辅助类库

辅助类库:Demo.Common.dll

服务接口类库:Demo.Lib.dll

Oracle组件类库:Demo.Lib.Oracle.dll

Sql组件类库:Demo.Lib.Sql.dll

 

3. Autofac容器适配器

[csharp] view plaincopy
  1. using Autofac;  
  2.   
  3. using System;  
  4.   
  5. namespace IoC.Core.Adapter  
  6. {  
  7.     public sealed class AutofacContainerAdapter : IContainerAdapter  
  8.     {  
  9.         public AutofacContainerAdapter()  
  10.         {  
  11.             Prepare();  
  12.         }  
  13.         private IContainer container;  
  14.   
  15.   
  16.         public void Prepare()  
  17.         {  
  18.             var autofacContainerBuilder = new ContainerBuilder();  
  19.               
  20.             //autofacContainerBuilder.RegisterType<Singleton>()  
  21.             //        .As<ISingleton>()  
  22.             //        .SingleInstance();  
  23.   
  24.             //autofacContainerBuilder.RegisterType<Transient>()  
  25.             //        .As<ITransient>();  
  26.   
  27.             //autofacContainerBuilder.RegisterType<Combined>()  
  28.             //        .As<ICombined>();  
  29.   
  30.             this.container = autofacContainerBuilder.Build();  
  31.         }  
  32.   
  33.         public T Resolve<T>()  
  34.         {  
  35.             return this.container.Resolve<T>();  
  36.         }  
  37.   
  38.   
  39.         public T Resolve<T>(string ServiceName)  
  40.         {  
  41.             return this.container.ResolveNamed<T>(ServiceName);  
  42.         }  
  43.   
  44.   
  45.         public void Dispose()  
  46.         {  
  47.             // Allow the container and everything it references to be disposed.  
  48.             this.container = null;  
  49.         }  
  50.   
  51.         public void Register(Type TService, Type TImplementation, string ServiceName, LifeCycleType LifeCycle = LifeCycleType.Transient)  
  52.         {  
  53.             if (!string.IsNullOrEmpty(ServiceName) && !container.IsRegisteredWithName(ServiceName, TService))  
  54.             {  
  55.                 var autofacContainerBuilder = new ContainerBuilder();  
  56.   
  57.                 var item = autofacContainerBuilder.RegisterType(TImplementation);  
  58.   
  59.                 if (!string.IsNullOrEmpty(ServiceName))  
  60.                 {  
  61.                     item = item.Named(ServiceName, TService);  
  62.                 }  
  63.                 else  
  64.                 {  
  65.                     item = item.As(TService);  
  66.                 }  
  67.   
  68.   
  69.   
  70.                 switch (LifeCycle)  
  71.                 {  
  72.                     case LifeCycleType.Singleton:  
  73.                         item = item.SingleInstance();  
  74.                         break;  
  75.                     case LifeCycleType.Transient:  
  76.                         item = item.InstancePerDependency();  
  77.                         break;  
  78.                     case LifeCycleType.Request:  
  79.                         item = item.InstancePerLifetimeScope();  
  80.                         break;  
  81.                 }  
  82.   
  83.   
  84.                   
  85.   
  86.                 //if (!string.IsNullOrEmpty(ServiceName))  
  87.                 //{  
  88.                 //    autofacContainerBuilder.RegisterType(TImplementation).Named(ServiceName, TService);  
  89.                 //}  
  90.                 //else  
  91.                 //{  
  92.                 //    autofacContainerBuilder.RegisterType(TImplementation).As(TService);  
  93.                 //}  
  94.   
  95.                 //container = autofacContainerBuilder.Build();  
  96.                 autofacContainerBuilder.Update(container);  
  97.             }  
  98.         }  
  99.   
  100.   
  101.         public void Register(string TServiceFull, string TImplementationFull, string ServiceName, LifeCycleType LifeCycle = LifeCycleType.Transient)  
  102.         {  
  103.             Type TService = Type.GetType(TServiceFull);  
  104.             Type TImplementation = Type.GetType(TImplementationFull);  
  105.   
  106.             if (!string.IsNullOrEmpty(ServiceName) && !container.IsRegisteredWithName(ServiceName, TService))  
  107.             {  
  108.                 Register(TService, TImplementation, ServiceName, LifeCycle);  
  109.             }  
  110.   
  111.         }  
  112.   
  113.   
  114.         public void Register(Type TService, Type TImplementation, LifeCycleType LifeCycle)  
  115.         {  
  116.             string ServiceName = TImplementation.GetType().Name;  
  117.             Register(TService, TImplementation, ServiceName, LifeCycle);  
  118.         }  
  119.   
  120.         public void Register(string TServiceFull, string TImplementationFull, LifeCycleType LifeCycle)  
  121.         {  
  122.             if (!string.IsNullOrEmpty(TImplementationFull))  
  123.             {  
  124.                 string ServiceName = TImplementationFull;  
  125.                 if (TImplementationFull.IndexOf(",") > 0)  
  126.                 {  
  127.                     ServiceName = TImplementationFull.Substring(0, TImplementationFull.IndexOf(","));  
  128.                 }  
  129.                 Register(TServiceFull, TImplementationFull, ServiceName, LifeCycle);  
  130.             }  
  131.         }  
  132.   
  133.   
  134.   
  135.   
  136.     }  
  137. }  


 

4. Castle.Windsor容器适配器

[csharp] view plaincopy
  1. using Castle.MicroKernel.Registration;  
  2. using Castle.Windsor;  
  3.   
  4. using System;  
  5. using System.Collections.Generic;  
  6.   
  7. namespace IoC.Core.Adapter  
  8. {  
  9.     public sealed class WindsorContainerAdapter : IContainerAdapter  
  10.     {  
  11.         public WindsorContainerAdapter()  
  12.         {  
  13.             Prepare();  
  14.         }  
  15.         private WindsorContainer container;  
  16.   
  17.         public void Prepare()  
  18.         {  
  19.             this.container = new WindsorContainer();  
  20.   
  21.             //this.container.Register(Component.For<ISingleton>().ImplementedBy<Singleton>());  
  22.             //this.container.Register(Component.For<ITransient>().ImplementedBy<Transient>().LifeStyle.Transient);  
  23.             //this.container.Register(Component.For<ICombined>().ImplementedBy<Combined>().LifeStyle.Transient);  
  24.         }  
  25.   
  26.         public T Resolve<T>()  
  27.         {  
  28.             return this.container.Resolve<T>();  
  29.         }  
  30.   
  31.         public T Resolve<T>(string ServiceName)  
  32.         {  
  33.   
  34.             return this.container.Resolve<T>(ServiceName);  
  35.         }  
  36.   
  37.   
  38.         public void Dispose()  
  39.         {  
  40.             // Allow the container and everything it references to be disposed.  
  41.             this.container = null;  
  42.         }  
  43.   
  44.         private IDictionary<stringstring> NameDict = new Dictionary<stringstring>();  
  45.   
  46.         public void Register(Type TService, Type TImplementation, string ServiceName, LifeCycleType LifeCycle = LifeCycleType.Transient)  
  47.         {  
  48.             if (!string.IsNullOrEmpty(ServiceName) && !NameDict.ContainsKey(ServiceName))  
  49.             {  
  50.                 var item = Component.For(TService).ImplementedBy(TImplementation);  
  51.   
  52.                 switch (LifeCycle)  
  53.                 {  
  54.                     case LifeCycleType.Singleton:  
  55.                         item = item.LifestyleSingleton();  
  56.                         break;  
  57.                     case LifeCycleType.Transient:  
  58.                         item = item.LifestyleTransient();  
  59.                         break;  
  60.                     case LifeCycleType.Request:  
  61.                         item = item.LifestylePerWebRequest();  
  62.                         break;  
  63.                 }  
  64.   
  65.                 if (!string.IsNullOrEmpty(ServiceName))  
  66.                 {  
  67.                     item = item.Named(ServiceName);  
  68.                     NameDict[ServiceName] = null;  
  69.                 }  
  70.   
  71.                 container.Register(item);  
  72.   
  73.             }  
  74.   
  75.         }  
  76.   
  77.         public void Register(string TServiceFull, string TImplementationFull, string ServiceName, LifeCycleType LifeCycle = LifeCycleType.Transient)  
  78.         {  
  79.             if (!string.IsNullOrEmpty(ServiceName) && !NameDict.ContainsKey(ServiceName))  
  80.             {  
  81.   
  82.                 Type TService = Type.GetType(TServiceFull);  
  83.                 Type TImplementation = Type.GetType(TImplementationFull);  
  84.   
  85.                 Register(TService, TImplementation, ServiceName, LifeCycle);  
  86.   
  87.   
  88.             }  
  89.   
  90.         }  
  91.   
  92.   
  93.   
  94.         public void Register(Type TService, Type TImplementation, LifeCycleType LifeCycle)  
  95.         {  
  96.             string ServiceName = TImplementation.GetType().Name;  
  97.             Register(TService, TImplementation, ServiceName, LifeCycle);  
  98.         }  
  99.   
  100.         public void Register(string TServiceFull, string TImplementationFull, LifeCycleType LifeCycle)  
  101.         {  
  102.             if (!string.IsNullOrEmpty(TImplementationFull))  
  103.             {  
  104.                 string ServiceName = TImplementationFull;  
  105.                 if (TImplementationFull.IndexOf(",") > 0)  
  106.                 {  
  107.                     ServiceName = TImplementationFull.Substring(0, TImplementationFull.IndexOf(","));  
  108.                 }  
  109.                 Register(TServiceFull, TImplementationFull, ServiceName, LifeCycle);  
  110.             }  
  111.         }  
  112.   
  113.     }  
  114. }  


 

 

5. Spring.NET的容器适配器

[csharp] view plaincopy
  1. using Spring.Core;  
  2. using Spring.Context;  
  3. using Spring.Context.Support;  
  4. using Spring.Objects.Factory.Support;  
  5.   
  6. using System;  
  7.   
  8. namespace IoC.Core.Adapter  
  9. {  
  10.     public sealed class SpringContainerAdapter : IContainerAdapter  
  11.     {  
  12.         public SpringContainerAdapter()  
  13.         {  
  14.             Prepare();  
  15.         }  
  16.   
  17.         private GenericApplicationContext container;  
  18.   
  19.         public void Prepare()  
  20.         {  
  21.             //this.container = Spring.Context.Support.ContextRegistry.GetContext();  
  22.             container = new GenericApplicationContext();  
  23.         }  
  24.   
  25.         public T Resolve<T>()  
  26.         {  
  27.             return (T)container.GetObject(typeof(T).FullName);  
  28.         }  
  29.   
  30.         public T Resolve<T>(string ServiceName)  
  31.         {  
  32.             return (T)container.GetObject<T>(ServiceName);  
  33.         }  
  34.   
  35.   
  36.   
  37.         public void Dispose()  
  38.         {  
  39.             // Allow the container and everything it references to be disposed.  
  40.             this.container = null;  
  41.         }  
  42.   
  43.   
  44.         public void Register(Type TService, Type TImplementation, string ServiceName, LifeCycleType LifeCycle = LifeCycleType.Transient)  
  45.         {  
  46.   
  47.             if (!string.IsNullOrEmpty(ServiceName) && !container.IsObjectNameInUse(ServiceName))  
  48.             {  
  49.                 string vName = TImplementation.GetType().ToString();  
  50.                 string TImplementationFull = vName + ", " + TImplementation.Assembly.ToString();  
  51.                 IObjectDefinitionFactory factory = new DefaultObjectDefinitionFactory();  
  52.                 AbstractObjectDefinition defi = factory.CreateObjectDefinition(TImplementationFull, null, AppDomain.CurrentDomain);  
  53.   
  54.                 string SpringObjeLifeCycle = "singleton";//"prototype" "request";  
  55.                 switch (LifeCycle)  
  56.                 {  
  57.                     case LifeCycleType.Singleton:  
  58.                         SpringObjeLifeCycle = "singleton";  
  59.                         break;  
  60.                     case LifeCycleType.Transient:  
  61.                         SpringObjeLifeCycle = "prototype";  
  62.                         break;  
  63.                     case LifeCycleType.Request:  
  64.                         SpringObjeLifeCycle = "request";  
  65.                         break;  
  66.                 }  
  67.                 defi.Scope = SpringObjeLifeCycle;  
  68.   
  69.                 //建立容器    
  70.                 //GenericApplicationContext tmpContainer = container as GenericApplicationContext;  
  71.                 container.RegisterObjectDefinition(ServiceName, defi);  
  72.   
  73.             }  
  74.   
  75.         }  
  76.   
  77.         public void Register(string TServiceFull, string TImplementationFull, string ServiceName, LifeCycleType LifeCycle = LifeCycleType.Transient)  
  78.         {  
  79.             if (!string.IsNullOrEmpty(ServiceName) && !container.IsObjectNameInUse(ServiceName))  
  80.             {  
  81.   
  82.                 IObjectDefinitionFactory factory = new DefaultObjectDefinitionFactory();  
  83.                 AbstractObjectDefinition defi = factory.CreateObjectDefinition(TImplementationFull, null, AppDomain.CurrentDomain);  
  84.   
  85.                 string SpringObjeLifeCycle = "singleton";//"prototype" "request";  
  86.                 switch (LifeCycle)  
  87.                 {  
  88.                     case LifeCycleType.Singleton:  
  89.                         SpringObjeLifeCycle = "singleton";  
  90.                         break;  
  91.                     case LifeCycleType.Transient:  
  92.                         SpringObjeLifeCycle = "prototype";  
  93.                         break;  
  94.                     case LifeCycleType.Request:  
  95.                         SpringObjeLifeCycle = "request";  
  96.                         break;  
  97.                 }  
  98.                 defi.Scope = SpringObjeLifeCycle;  
  99.   
  100.                 string vFullName = Type.GetType(TImplementationFull).FullName;  
  101.                 //建立容器    
  102.                 //GenericApplicationContext tmpContainer = container as GenericApplicationContext;  
  103.                 container.RegisterObjectDefinition(vFullName, defi);  
  104.             }  
  105.   
  106.         }  
  107.   
  108.   
  109.         public void Register(Type TService, Type TImplementation, LifeCycleType LifeCycle)  
  110.         {  
  111.             string ServiceName = TImplementation.GetType().Name;  
  112.             Register(TService, TImplementation, ServiceName, LifeCycle);  
  113.         }  
  114.   
  115.         public void Register(string TServiceFull, string TImplementationFull, LifeCycleType LifeCycle)  
  116.         {  
  117.             if (!string.IsNullOrEmpty(TImplementationFull))  
  118.             {  
  119.                 string ServiceName = TImplementationFull;  
  120.                 if (TImplementationFull.IndexOf(",") > 0)  
  121.                 {  
  122.                     ServiceName = TImplementationFull.Substring(0, TImplementationFull.IndexOf(","));  
  123.                 }  
  124.                 Register(TServiceFull, TImplementationFull, ServiceName, LifeCycle);  
  125.             }  
  126.         }  
  127.     }  
  128. }  


 

 

6. 测试结果

6.1. 单例模式

 

 

6.2. 瞬时(多例)模式

 

7. 测试结论

(1) 不管是单例还是多例,初始化的时间都差不多,Autofac略快

 Autofac略快
 Spring和Windsor略慢,但两者相差不大


(2) 不管是单例还是多例,非初始化取数据时,取数据量大时,花费的时间也大


(3) 单例时,性能比较,Autofac略慢,但差异不大


 Spring和Windsor花费时间差不多
 Autofac比较慢


(4) 多例时,性能比较,Autofac速度最快,也最稳定,差异较大


 Autofac速度最快,相当稳定
 Windsor速度居中:是Autofac的1~2倍
 Spring速度最慢: 是Autofac的5~6倍左右

 

8. IoC容器选择结论(Autofac第一名)


 A. 单例时,Autofac会略慢,但相差不大


 B. 多例时,Autofac速度明显较快,Spring性能下降太厉害


 C. 整体而言,Autofac的优劣较明显


 D. 性能对比,Autofac > Windsor > Spring

 

9. Demo下载

点此下载

0 0
原创粉丝点击