Unity 2.1 Automatic Factories VERY SLOW performace?

Jul 18, 2011 at 3:10 PM

Hi Guys, I was excited to see this new feature adopted by Unity 2.x, so I tried to see how this new version compares in performance with the previous one (v1.2).

Found this interesting benchmark (http://www.codinginstinct.com/2008/05/ioc-container-benchmark-rerevisited.html) and I modified it to use the latest version of Unity. Running the tests AS IS, the new verison of Unity is slightly slower than v1.2, but nothing too serious (8.23 seconds for v1.2 vs 16.42 seconds for v2.1). I know, its almost double but still less than some of the competitors, but still a pitty to have less performance than the previous version.

So, I modified the test to use the new Automatic Factory (injecting a Func<ImyDependency> instead of just the interface as before) and the performance dropped dramatically: now the same test takes 122 seconds!!!!!

What's happening guys? This mechanism is supposed to allow faster creation of the instances or not? The actual creation of the injected dependencies is delegated for later resolution, so what's happening?  I'm doing something wrong?

Thanks in advance,

Andrés.

Jul 19, 2011 at 8:04 AM

Hi,

I wanted to take a look at the benchmark, but the link you provided says the page was not found. Anyway, regarding with the performance, Chris Tavares mentioned on this thread that generally, the first request for an instance is slower but the succeeding request should be much faster. It would be better if you can provide us the benchmark so we can have an idea.

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

 

 

 

Sep 13, 2011 at 8:41 PM

Hi,

the aforementioned benchmark can be found here: http://www.codinginstinct.com/2008/04/ioc-container-benchmark-unity-windsor.html. This is the base benchmark, the aforementioned includes some other IoC containers.

The source code is at the bottom of that page (here it is IoCBenchmark.zip).

I'm currently using version 1.2. So I was curious about performance in new release. I was awaiting better performance or at least the same performance as 1.2 version.

So I've done my own comparison of those two versions.

I've just tried to run source code with updated Unity reference to 1.2 release and with 2.1 release consequently.

And I have to say I'm disappointed. The new 2.1 release of Unity is more 2-3 times slower than the 1.2 version!!!

Please see the code and make your own conclusions.
But it is expected this should be fixed asap. This release isn't usable for upgrade.

Miroslav Galajda

Sep 14, 2011 at 10:14 AM

Hi,

I was able to see the benchmark code and able to verify that it actually recreates the container for every registration. Note that it adds additional overhead on the overall performance. Kindly refer to these posts regarding Unity Performance:

http://unity.codeplex.com/discussions/37992
http://unity.codeplex.com/discussions/231444
http://unity.codeplex.com/discussions/37992

Hope this helps.

 

Noel Angelo Bolasoc
Avanade Software
Avanade, Inc.
Contact Us

Sep 14, 2011 at 10:59 AM

Hi, I don't understand you.

The container is created once per benchmark. So there is no additional overhead as you are writing.

This is the snippet of benchmark method:

private void RunBenchmark(Action setupAction, string mode)
{
	var regTimer = new HiPerfTimer();
	var resolveTimer = new HiPerfTimer();

	// 1. setup of the container, eg. create it and register types in it
	regTimer.Start();
	setupAction();
	regTimer.Stop();

	// 2. benchmark the Resolve function 1000000x
	resolveTimer.Start();

	for (int i = 0; i < 1000000; i++)
	{
		var controller = container.Resolve<UserController>();
	}

	resolveTimer.Stop();

	Console.WriteLine("{0}: - {1} - Registartion time: \t{2}s", container.Name, mode, regTimer.Duration);
	Console.WriteLine("{0}: - {1} - Component resolve time: \t{2}s", container.Name, mode, resolveTimer.Duration);
}

As you can see, there are two phases. And the second one is interesting. This is slower 2-3 times when the same code runs against previous versions of Unity.

Sep 15, 2011 at 7:10 AM

Hi,

Apologies for the earlier post but I tested the wrong benchmark project. Moving on, I believe that the benchmark can further be optimized by placing the the instantiation of container on the constructor instead on the registration methods. This would reduce the resolution time (of transient registrations) by around 50%. I was also able to compare the performance of 1.2 and 2.1 and verified that latter is slower. I guess this is because of some added features since 1.2 which may have hit the overall performance. I'll let know Microsoft regarding this and let's wait for their feedback. Meanwhile, you can log this on our issue tracker.

 

Noel Angelo Bolasoc
Avanade Software
Avanade, Inc.
Contact Us

 

 

Sep 15, 2011 at 2:25 PM
Edited Sep 15, 2011 at 2:25 PM

Hi,

I've created an issue at http://unity.codeplex.com/workitem/11273.

Miroslav Galajda