【轉載】各大主流.Net的IOC框架效能測試比較
各大主流.Net的IOC框架效能測試比較
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
使用方式類似於:
intiteration=100*1000;strings="";CodeTimer.Time("StringConcat",iteration,()=>{s+="a";});
StringBuildersb=newStringBuilder();
CodeTimer.Time("StringBuilder",iteration,()=>{sb.Append("a");});
3、編寫一個IRunner執行介面:
{
voidStart(RunTyperunType);
}
以及RunnerBase抽象基礎執行類:
publicabstractclassRunnerBase{
privateint_iteration=Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["Iteration"]??"10000");
internalintIteration
{
get{return_iteration;}
}
internalvoidTime(Actionaction)
{
CodeTimer.Time(Name,Iteration,action);
}
protectedabstractstringName{get;}
}
這裡_iteration表示測試執行次數,通過配置檔案來設定值。Time方法通過計數器對action方法進行Iteration次迭代。
編寫一個RunManager的執行管理器:
publicclassRunManager{
publicstaticvoidStart(IRunnerrunner)
{
Start(runner,RunType.Transient);
}
publicstaticvoidStart(IRunnerrunner,RunTyperunType)
{
runner.Start(runType);
}
}
在測試中,我採用兩種方式的效能比較,一個是單例狀態,一個是非單例狀態:
///執行狀態
///</summary>
publicenumRunType
{
///<summary>
///單例
///</summary>
Singleton,
///<summary>
///瞬時
///</summary>
Transient
}
好了,現在我的程式只要繼承RunnerBase以及IRunnre介面,就可以實現各個IOC框架的初始化裝配的工作了。基礎工作已經做好。
各個IOC框架測試程式
1、Autofac:
publicclassAutofacRunner:RunnerBase,IRunner{
protectedoverridestringName
{
get{return"Autofac";}
}
publicvoidStart(RunTyperunType)
{
varbuilder=newContainerBuilder();
//if(runType==RunType.Singleton)
//builder.RegisterType<DatabaseManager>().SingleInstance();
//else
//builder.RegisterType<DatabaseManager>();
//builder.RegisterType<SqlDatabase>().As<IDatabase>();
////builder.RegisterModule(newConfigurationSettingsReader("autofac"));
builder.RegisterType<SqlDatabase>().As<IDatabase>();
if(runType==RunType.Singleton)
builder.Register(c=>newDatabaseManager(c.Resolve<IDatabase>())).SingleInstance();
else
builder.Register(c=>newDatabaseManager(c.Resolve<IDatabase>()));
varcontainer=builder.Build();
Time(()=>
{
varmanager=container.Resolve<DatabaseManager>();
manager.Search("SELECT*FROMUSER");
});
container.Dispose();
}
}
2、Castle Windsor:
publicclassWindsorRunner:RunnerBase,IRunner{
protectedoverridestringName
{
get{return"CastleWindsor";}
}
publicvoidStart(RunTyperunType)
{
varcontainer=newWindsorContainer();
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(()=>
{
varmanager=container.Resolve<DatabaseManager>();
manager.Search("SELECT*FROMUSER");
});
}
}
3、Unity:
publicclassUnityRunner:RunnerBase,IRunner{
protectedoverridestringName
{
get{return"Unity";}
}
publicvoidStart(RunTyperunType)
{
varcontainer=newUnityContainer();
if(runType==RunType.Singleton)
container.RegisterType<DatabaseManager>(newContainerControlledLifetimeManager());
else
container.RegisterType<DatabaseManager>(newTransientLifetimeManager());
container.RegisterType<IDatabase,SqlDatabase>();
Time(()=>
{
varmanager=container.Resolve<DatabaseManager>();
manager.Search("SELECT*FROMUSER");
});
}
}
4、Spring.NET:
publicclassSpringRunner:RunnerBase,IRunner{
protectedoverridestringName
{
get{return"Spring.NET";}
}
publicvoidStart(RunTyperunType)
{
stringdatabaseManagerName;
if(runType==RunType.Singleton)
databaseManagerName="DatabaseManager_Singleton";
else
databaseManagerName="DatabaseManager_Transient";
Time(()=>
{
IApplicationContextcontext=ContextRegistry.GetContext();
varmanager=(DatabaseManager)context.GetObject(databaseManagerName);
manager.Search("SELECT*FROMUSER");
});
}
}
5、StructureMap:
publicclassStructureMapRunner:RunnerBase,IRunner{
protectedoverridestringName
{
get{return"StructureMap";}
}
publicvoidStart(RunTyperunType)
{
ObjectFactory.Initialize(container=>
{
if(runType==RunType.Singleton)
container.ForRequestedType<DatabaseManager>().Singleton();
else
container.ForRequestedType<DatabaseManager>();
container.ForRequestedType<IDatabase>().TheDefaultIsConcreteType<SqlDatabase>();
});
Time(()=>
{
varmanager=ObjectFactory.GetInstance<DatabaseManager>();
manager.Search("SELECT*FROMUSER");
});
}
}
6、Ninject:
publicclassNinjectRunner:RunnerBase,IRunner{
protectedoverridestringName
{
get{return"Ninject";}
}
publicvoidStart(RunTyperunType)
{
IKernelkernel=newStandardKernel(newMyNinjectModule(runType));
Time(()=>
{
varmanager=kernel.Get<DatabaseManager>();
manager.Search("SELECT*FROMUSER");
});
}
}
客戶端測試程式
staticvoidMain(string[]args){
CodeTimer.Initialize();
Console.WriteLine("IOC-Singleton");
//AutofacSingleton
RunManager.Start(newAutofacRunner(),RunType.Singleton);
//CastleWindsor
RunManager.Start(newWindsorRunner(),RunType.Singleton);
//Unity
RunManager.Start(newUnityRunner(),RunType.Singleton);
//Spring.NET
RunManager.Start(newSpringRunner(),RunType.Singleton);
//StructureMap
RunManager.Start(newStructureMapRunner(),RunType.Singleton);
//Ninject
RunManager.Start(newNinjectRunner(),RunType.Singleton);
Console.WriteLine("===================================");
Console.WriteLine("IOC-Transient");
//AutofacSingleton
RunManager.Start(newAutofacRunner(),RunType.Transient);
//CastleWindsor
RunManager.Start(newWindsorRunner(),RunType.Transient);
//Unity
RunManager.Start(newUnityRunner(),RunType.Transient);
//Spring.NET
RunManager.Start(newSpringRunner(),RunType.Transient);
//StructureMap
RunManager.Start(newStructureMapRunner(),RunType.Transient);
//Ninject
RunManager.Start(newNinjectRunner(),RunType.Transient);
Console.ReadKey();
}
通過修改App.config的Iteration配置值,來設定迭代次數。
<appSettings><addkey="Iteration"value="100000"/>
</appSettings>
執行結果
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中規中矩,效能較為穩定,也是一個不錯的選擇。另外,可能測試程式會有所偏差,希望大家也能夠指出問題!
本文來自部落格園,作者:Love In Winter,轉載請註明原文連結:https://www.cnblogs.com/LifeDecidesHappiness/p/15648840.html
歡迎掃碼打賞