在上一篇中,我简单介绍了下Autofac的使用,有人希望能有个性能上的测试,考虑到有那么多的IOC框架,而主流的有:Castle Windsor、微软企业库中的Unity、Spring.NET、StructureMap、Ninject等等。本篇文章主要针对这些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
使用方式类似于:
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,
///
<summary>
///
瞬时
///
</summary>
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
{
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
<
DatabaseManager
>
();
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
<
DatabaseManager
>
(
new
ContainerControlledLifetimeManager());
else
container.RegisterType
<
DatabaseManager
>
(
new
TransientLifetimeManager());
container.RegisterType
<
IDatabase, SqlDatabase
>
();
Time(()
=>
{
var manager
=
container.Resolve
<
DatabaseManager
>
();
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
<
DatabaseManager
>
().Singleton();
else
container.ForRequestedType
<
DatabaseManager
>
();
container.ForRequestedType
<
IDatabase
>
().TheDefaultIsConcreteType
<
SqlDatabase
>
();
});
Time(()
=>
{
var manager
=
ObjectFactory.GetInstance
<
DatabaseManager
>
();
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
<
DatabaseManager
>
();
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配置值,来设置迭代次数。
<
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中规中矩,性能较为稳定,也是一个不错的选择。另外,可能测试程序会有所偏差,希望大家也能够指出问题!
测试程序源代码:IOCPerformanceTest.rar
Autofac是一款IOC框架,比较于其他的IOC框架,如Spring.NET,Unity,Castle等等所包含的,它很轻量级性能上也是很高的。于是,今天抽空研究了下它。下载地址:http://code.google.com/p/autofac/downloads/list
1)解压它的压缩包,主要看到Autofac.dll,Autofac.Configuration.dll,这也是本篇文章重点使用的Autofac的类库。
2)创建一个控制台工程,并且引用以上的DLL文件。创建一个数据库操作接口IDatabase.cs:
///
<summary>
///
Database operate interface
///
</summary>
public
interface
IDatabase
{
string
Name {
get
; }
void
Select(
string
commandText);
void
Insert(
string
commandText);
void
Update(
string
commandText);
void
Delete(
string
commandText);
}
这里包含CRUD四种操作的方法。
3)创建两种数据库的操作类,SqlDatabase.cs以及OracleDatabase.cs:
public
class
SqlDatabase : IDatabase
{
public
string
Name
{
get
{
return
"
sqlserver
"
; }
}
public
void
Select(
string
commandText)
{
Console.WriteLine(
string
.Format(
"
'{0}' is a query sql in {1}!
"
, commandText, Name));
}
public
void
Insert(
string
commandText)
{
Console.WriteLine(
string
.Format(
"
'{0}' is a insert sql in {1}!
"
, commandText, Name));
}
public
void
Update(
string
commandText)
{
Console.WriteLine(
string
.Format(
"
'{0}' is a update sql in {1}!
"
, commandText, Name));
}
public
void
Delete(
string
commandText)
{
Console.WriteLine(
string
.Format(
"
'{0}' is a delete sql in {1}!
"
, commandText, Name));
}
}
以及
public
class
OracleDatabase : IDatabase
{
public
string
Name
{
get
{
return
"
oracle
"
; }
}
public
void
Select(
string
commandText)
{
Console.WriteLine(
string
.Format(
"
'{0}' is a query sql in {1}!
"
, commandText, Name));
}
public
void
Insert(
string
commandText)
{
Console.WriteLine(
string
.Format(
"
'{0}' is a insert sql in {1}!
"
, commandText, Name));
}
public
void
Update(
string
commandText)
{
Console.WriteLine(
string
.Format(
"
'{0}' is a update sql in {1}!
"
, commandText, Name));
}
public
void
Delete(
string
commandText)
{
Console.WriteLine(
string
.Format(
"
'{0}' is a delete sql in {1}!
"
, commandText, Name));
}
}
4)接着创建一个数据库管理器DatabaseManager.cs:
public
class
DatabaseManager
{
IDatabase _database;
public
DatabaseManager(IDatabase database)
{
_database
=
database;
}
public
void
Search(
string
commandText)
{
_database.Select(commandText);
}
public
void
Add(
string
commandText)
{
_database.Insert(commandText);
}
public
void
Save(
string
commandText)
{
_database.Update(commandText);
}
public
void
Remove(
string
commandText)
{
_database.Delete(commandText);
}
}
5)在控制台中,编写以下测试程序:
var builder
=
new
ContainerBuilder();
builder.RegisterType
<
DatabaseManager
>
();
builder.RegisterType
<
SqlDatabase
>
().As
<
IDatabase
>
();
using
(var container
=
builder.Build())
{
var manager
=
container.Resolve
<
DatabaseManager
>
();
manager.Search(
"
SELECT * FORM USER
"
);
}
运行结果:
分析:
这里通过ContainerBuilder方法RegisterType对DatabaseManager进行注册,当注册的类型在相应得到的容器中可以Resolve你的DatabaseManager实例。
builder.RegisterType<SqlDatabase>().As<IDatabase>();通过AS可以让DatabaseManager类中通过构造函数依赖注入类型相应的接口。
Build()方法生成一个对应的Container实例,这样,就可以通过Resolve解析到注册的类型实例。
同样地,如果你修改数据库类型注册为:
builder.RegisterType<OracleDatabase>().As<IDatabase>();
运行结果:
6)显然以上的程序中,SqlDatabase或者OracleDatabase已经暴露于客户程序中了,现在我想将该类型选择通过文件配置进行读取。Autofac自带了一个Autofac.Configuration.dll 非常方便地对类型进行配置,避免了程序的重新编译。
修改App.config:
<
configuration
>
<
configSections
>
<
section
name
="autofac"
type
="Autofac.Configuration.SectionHandler, Autofac.Configuration"
/>
</
configSections
>
<
autofac
defaultAssembly
="AutofacDemo"
>
<
components
>
<
component
type
="AutofacDemo.SqlDatabase, AutofacDemo"
service
="AutofacDemo.IDatabase"
/>
</
components
>
</
autofac
>
</
configuration
>
通过Autofac.Configuration.SectionHandler配置节点对组件进行处理。
对应的客户端程序改为:
var builder
=
new
ContainerBuilder();
builder.RegisterType
<
DatabaseManager
>
();
builder.RegisterModule(
new
ConfigurationSettingsReader(
"
autofac
"
));
using
(var container
=
builder.Build())
{
var manager
=
container.Resolve
<
DatabaseManager
>
();
manager.Search(
"
SELECT * FORM USER
"
);
}
运行结果:
7)另外还有一种方式,通过Register方法进行注册:
var builder
=
new
ContainerBuilder();
//
builder.RegisterType<DatabaseManager>();
builder.RegisterModule(
new
ConfigurationSettingsReader(
"
autofac
"
));
builder.Register(c
=>
new
DatabaseManager(c.Resolve
<
IDatabase
>
()));
using
(var container
=
builder.Build())
{
var manager
=
container.Resolve
<
DatabaseManager
>
();
manager.Search(
"
SELECT * FORM USER
"
);
}
得到结果也是一样的。
8)现在我想通过一个用户类来控制操作权限,比如增删改的权限,创建一个用户类:
///
<summary>
///
Id Identity Interface
///
</summary>
public
interface
Identity
{
int
Id {
get
;
set
; }
}
public
class
User : Identity
{
public
int
Id {
get
;
set
; }
public
string
Name {
get
;
set
; }
}
修改DatabaseManager.cs代码:
public
class
DatabaseManager
{
IDatabase _database;
User _user;
public
DatabaseManager(IDatabase database) :
this
(database,
null
)
{
}
public
DatabaseManager(IDatabase database, User user)
{
_database
=
database;
_user
=
user;
}
///
<summary>
///
Check Authority
///
</summary>
///
<returns></returns>
public
bool
IsAuthority()
{
bool
result
=
_user
!=
null
&&
_user.Id
==
1
&&
_user.Name
==
"
leepy
"
?
true
:
false
;
if
(
!
result)
Console.WriteLine(
"
Not authority!
"
);
return
result;
}
public
void
Search(
string
commandText)
{
_database.Select(commandText);
}
public
void
Add(
string
commandText)
{
if
(IsAuthority())
_database.Insert(commandText);
}
public
void
Save(
string
commandText)
{
if
(IsAuthority())
_database.Update(commandText);
}
public
void
Remove(
string
commandText)
{
if
(IsAuthority())
_database.Delete(commandText);
}
}
在构造函数中增加了一个参数User,而Add,Save,Remove增加了权限判断。
修改客户端程序:
User user
=
new
User { Id
=
1
, Name
=
"
leepy
"
};
var builder
=
new
ContainerBuilder();
builder.RegisterModule(
new
ConfigurationSettingsReader(
"
autofac
"
));
builder.RegisterInstance(user).As
<
User
>
();
builder.Register(c
=>
new
DatabaseManager(c.Resolve
<
IDatabase
>
(), c.Resolve
<
User
>
()));
using
(var container
=
builder.Build())
{
var manager
=
container.Resolve
<
DatabaseManager
>
();
manager.Add(
"
INSERT INTO USER ...
"
);
}
运行结果:
分析:
builder.RegisterInstance(user).As<User>();注册User实例。
builder.Register(c => new DatabaseManager(c.Resolve<IDatabase>(), c.Resolve<User>()));通过Lampda表达式注册DatabaseManager实例。
如果这里我修改User的属性值:
User user = new User { Id = 2, Name = "zhangsan" };
运行结果:
说明该用户无权限操作。
源代码下载:AutofacDemo.rar