其中,測試程式均采用最新的類庫。
基礎工作
使用方式類似于:
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);
}
在測試中,我采用兩種方式的性能比較,一個是單例狀态,一個是非單例狀态:
/// <summary>
/// 運作狀态
/// </summary>
public enum RunType
/// <summary>
/// 單例
/// </summary>
Singleton,
/// 瞬時
Transient
好了,現在我的程式隻要繼承RunnerBase以及IRunnre接口,就可以實作各個IOC架構的初始化裝配的工作了。基礎工作已經做好。
各個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<DatabaseManager>().SingleInstance();
//else
// builder.RegisterType<DatabaseManager>();
//builder.RegisterType<SqlDatabase>().As<IDatabase>();
////builder.RegisterModule(new ConfigurationSettingsReader("autofac"));
builder.RegisterType<SqlDatabase>().As<IDatabase>();
if (runType == RunType.Singleton)
builder.Register(c => new DatabaseManager(c.Resolve<IDatabase>())).SingleInstance();
else
builder.Register(c => new DatabaseManager(c.Resolve<IDatabase>()));
var container = builder.Build();
Time(() =>
{
var manager = container.Resolve<DatabaseManager>();
manager.Search("SELECT * FROM USER");
});
container.Dispose();
2、Castle Windsor:
public class WindsorRunner : RunnerBase, IRunner
get { return "Castle Windsor"; }
var container = new WindsorContainer();
if(runType == RunType.Singleton)
container.Register(Component.For(typeof(DatabaseManager)).LifeStyle.Singleton);
container.Register(Component.For(typeof(DatabaseManager)).LifeStyle.Transient);
container.Register(Component.For(typeof(IDatabase)).ImplementedBy(typeof(SqlDatabase)));
});
3、Unity:
public class UnityRunner : RunnerBase, IRunner
get { return "Unity"; }
var container = new UnityContainer();
container.RegisterType<DatabaseManager>(new ContainerControlledLifetimeManager());
container.RegisterType<DatabaseManager>(new TransientLifetimeManager());
container.RegisterType<IDatabase, SqlDatabase>();
{
var manager = container.Resolve<DatabaseManager>();
manager.Search("SELECT * FROM USER");
});
4、Spring.NET:
public class SpringRunner : RunnerBase, IRunner
get { return "Spring.NET"; }
string databaseManagerName;
databaseManagerName = "DatabaseManager_Singleton";
databaseManagerName = "DatabaseManager_Transient";
IApplicationContext context = ContextRegistry.GetContext();
var manager = (DatabaseManager)context.GetObject(databaseManagerName);
5、StructureMap:
public class StructureMapRunner : RunnerBase, IRunner
get { return "StructureMap"; }
ObjectFactory.Initialize(container =>
if (runType == RunType.Singleton)
container.ForRequestedType<DatabaseManager>().Singleton();
else
container.ForRequestedType<DatabaseManager>();
container.ForRequestedType<IDatabase>().TheDefaultIsConcreteType<SqlDatabase>();
var manager = ObjectFactory.GetInstance<DatabaseManager>();
6、Ninject:
public class NinjectRunner : RunnerBase, IRunner
get { return "Ninject"; }
IKernel kernel = new StandardKernel(new MyNinjectModule(runType));
var manager = kernel.Get<DatabaseManager>();
用戶端測試程式
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");
RunManager.Start(new AutofacRunner(), RunType.Transient);
RunManager.Start(new WindsorRunner(), RunType.Transient);
RunManager.Start(new UnityRunner(), RunType.Transient);
RunManager.Start(new SpringRunner(), RunType.Transient);
RunManager.Start(new StructureMapRunner(), RunType.Transient);
RunManager.Start(new NinjectRunner(), RunType.Transient);
Console.ReadKey();
通過修改App.config的Iteration配置值,來設定疊代次數。
<appSettings>
<add key="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中規中矩,性能較為穩定,也是一個不錯的選擇。另外,可能測試程式會有所偏差,希望大家也能夠指出問題!
原文:http://www.cnblogs.com/liping13599168/archive/2011/07/17/2108734.html