The major mainstream. NET IOC Framework performance test comparison

Source: Internet
Author: User

In the previous article, I briefly introduced the use of the next AUTOFAC, some people want to have a personality to test, considering that there are so many IOC framework, and the mainstream is: Castle Windsor, Microsoft Enterprise Library Unity, spring.net, StructureMap, Ninject and so on. This article is intended to write test procedures for these IOC frameworks.

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

Among them, the test program uses the newest class library.

Basic work

1, the program or a reference to the previous example as a test background.

2, write a performance counter, here I use Lao Zhao wrote a Codetimer class, specific introduction see: Http://www.cnblogs.com/JeffreyZhao/archive/2009/03/10/codetimer.html

Used in a way similar to:

int iteration = * 1000;string s = "";
Codetimer.time ("String Concat", iteration, () = {s + = "a";});
StringBuilder sb = new StringBuilder ();
Codetimer.time ("StringBuilder", iteration, () = {sb. Append ("a"); });

3. Write a irunner running interface:

public interface Irunner
{
void Start (RunType runtype);
}

and Runnerbase abstract base run class:

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;}
}

Here _iteration indicates the number of test runs and sets the value through the configuration file. The time method iteration the action method through a counter.

Write a Runmanager run Manager:

public class Runmanager
{
public static void Start (Irunner runner)
{
Start (runner, runtype.transient);
}

public static void Start (Irunner runner, RunType RunType)
{
Runner. Start (RunType);
}
}


In the test, I used two methods of performance comparison, one is a singleton state, one is a non-singleton state:

<summary>
Running state
</summary>
public enum RunType
{
<summary>
Single case
</summary>
Singleton,

<summary>
Instantaneous
</summary>
Transient
}

OK, now my program can implement the initialization assembly of each IOC framework as long as it inherits the Runnerbase and Irunnre interfaces. The basic work has been done.

Each IOC framework test program

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");
});
}
}

Client test Program

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 ();
}

Set the number of iterations by modifying the iteration configuration value of app. CONFIG.

<appSettings>
<add key= "Iteration" value= "100000"/>
</appSettings>

Run results

1, iteration=1000:

Analysis: in the thousand orders of magnitude, autofac,castlewindsor, StructureMap basically almost, more efficient than the other higher.

2, iteration=10000:

Analysis: in the million order of magnitude, autofac,castlewindsor,structuremap basic efficiency is similar, where structuremap efficiency slightly decreased Spring.net and Ninject performance is relatively low.

3, iteration=100000:

Analysis: in the 100,000 order of magnitude, the efficiency of castlewindsor began to decline, and in transient aspect, structuremap and AUTOFAC basically similar.

4, iteration=1000000:

Analysis: in the million orders of magnitude, AUTOFAC and StructureMap are still relatively high efficiency, and in transient aspect, StructureMap has exceeded AUTOFAC.

Summary: from the test, you can see AUTOFAC and StructureMap in the performance above or reflect a relatively large advantage, Ninject can say performance is low. And Spring.net not only focuses on the IOC, it also focuses on other aspects of functionality, so performance in the IOC is not too high. In addition, Microsoft's unity, performance is more stable, but also a good choice. In addition, the test program may be biased, I hope you can also point out the problem!

Test program source code: Iocperformancetest.rar

Original: http://www.cnblogs.com/liping13599168/archive/2011/07/17/2108734.html

The major mainstream. NET IOC Framework performance test comparison

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.