PDA

View Full Version : Performance Issues on Stress Test Regarding ProxyFactory and NonSingleton Objects



jamendoza
05-31-2006, 09:33 PM
Im having performance issues when creating ProxyObjects with advices, on performances tests, where i create the same object 1000 times, performance worsens every time the test is run.

I Know that when not using singleton (i need new instances each time because of object's state) objects it may affect perfomance, but not exponentially as what is happening.

For example the first time it takes 2 seconds, after that, each new run takes over 2-3 Seconds more than the last test.

Example:
Run / Time
1st / 2 secs
2nd / 5 secs
3rd / 7 secs
4th / 10 secs
5th / 13 secs

I tried using Spring Programatically and using configuration, the results are the same.

jamendoza
05-31-2006, 09:36 PM
This is an example of what i did programatically:


//this is not working code, it just an example
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.Target = targetObject
proxyFactory.AddAdvice(advice1);
proxyFactory.AddAdvice(advice2);
proxyObject = proxyFactory.GetProxy();

jamendoza
05-31-2006, 09:37 PM
This is what i did on configuration



<!-- this is not working code, it just an example -->
<object id="advice1" type="....." singleton="false"/>
<object id="advice1" type="....." singleton="false"/>
<object id="targetObject" type="....." singleton="false"/>

<object id="proxyObject" type="Spring.Aop.Framework.ProxyFactoryObject">
<property name="isSingleton" value="false"/>
<property name="targetName" value="targetObject" />
<property name="interceptorNames">
<list>
<value>advice1</value>
<value>advice2</value>
</list>
</property>
</object>

Bruno Baia
06-01-2006, 07:24 PM
Hi,

I tried to reproduce it, but everything seems oki here.
I used the AopQuickStart sample, changed singletons to prototypes and my testing code looks like this :


// Create AOP proxy using Spring.NET IoC container.
IApplicationContext ctx = ContextRegistry.GetContext();

for (int i = 0; i < 100; i++)
{
Stopwatch sw = Stopwatch.StartNew();
ICommand command = (ICommand)ctx["myServiceObject"];
command.DoExecute();
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
}


what kind of advice are you using ?
Try to use some basic advices to know if the problem is due to your advices or not.


-Bruno

jamendoza
06-02-2006, 12:09 AM
Hi, i did an example solution to prove what im talking about,
the link to the solution is at the end of this post, unfortunately, due to size limits, i had to exclude spring's dlls so, just open the solution and re-add the references.

I did 3 runs

1.- Open app, and execute programatic creation 5 times, close app
http://forum.springframework.net/attachment.php?attachmentid=1&stc=1&d=1149204437

2.- Open app, and execute creation with applicationcontext 5 times, close app
http://forum.springframework.net/attachment.php?attachmentid=2&stc=1&d=1149204466

3.- 1.- Open app, and execute 3 programatic and 2 with applicationcontext, close app
http://forum.springframework.net/attachment.php?attachmentid=3&stc=1&d=1149204470

The targetObject is a simple class with 2 fields (1 is string, the other is an int)

Here is an extraction from the code that creates programatically:


for(int i = 0; i<1000; i++)
{
targetObject = new TargetObject();
sampleAfterAdvice = new SampleAfterAdvice();
sampleBeforeAdvice = new SampleBeforeAdvice();
proxyFactory = new ProxyFactory(targetObject);
proxyFactory.AddAdvice(sampleAfterAdvice);
proxyFactory.AddAdvice(sampleBeforeAdvice);
targetObject = (ITargetObject)proxyFactory.GetProxy();
}


Here is an extraction from the code that uses applicationcontext:


for(int i = 0; i<1000; i++)
{
targetObject = (ITargetObject) applicationContext.GetObject("ProxyObject");
}


Here is the object configuration


<objects xmlns="http://www.springframework.net">
<object id="SampleAfterAdvice"
type="SpringSample.SampleAfterAdvice, SpringSample" singleton="false"/>
<object id="SampleBeforeAdvice"
type="SpringSample.SampleBeforeAdvice, SpringSample" singleton="false"/>
<object id="TargetObject"
type="SpringSample.TargetObject, SpringSample" singleton="false"/>

<object id="ProxyObject" type="Spring.Aop.Framework.ProxyFactoryObject">
<property name="isSingleton" value="false"/>
<property name="targetName" value="TargetObject" />
<property name="interceptorNames">
<list>
<value>SampleAfterAdvice</value>
<value>SampleBeforeAdvice</value>
</list>
</property>
</object>
</objects>


And here are the main methods of the Before and After Advices



public void AfterReturning
(object returnValue, MethodInfo method, object[] args, object target)
{
//I commented this line so it wont interfere with this test,
//but it can be uncommented to show the advices actually works
//Console.WriteLine("I'm the after advice");
}



public void Before
(MethodInfo method, object[] args, object target)
{
//I commented this line so it wont interfere with this test,
//but it can be uncommented to show the advices actually works
//Console.WriteLine("I'm the before advice");
}


Download the Solution (zip file) (http://forum.springframework.net/attachment.php?attachmentid=6&stc=1&d=1149206730)

Bruno Baia
06-02-2006, 09:27 AM
Hi,

Problem here is that the Proxy is created each time and maybe you don't need it.
Why not using a PrototypeTargetSource in your ProxyFactory if your target is a prototype (By default, it uses a SingletonTargetSource) :


<object id="ProxyObject" type="Spring.Aop.Framework.ProxyFactoryObject">
<property name="targetSource">
<object type="Spring.Aop.Target.PrototypeTargetSource, Spring.Aop">
<property name="TargetObjectName" value="TargetObject" />
</object>
</property>
<property name="interceptorNames">
<list>
<value>SampleAfterAdvice</value>
<value>SampleBeforeAdvice</value>
</list>
</property>
</object>


Or do you really need a new proxy for each call ?

Take a look to the documentation :



IsSingleton whether or not the factory should return a single proxy object, no matter how often the GetObject() method is called. Several IFactoryObject implementations offer such a method. Default value is true. If you would like to be able to apply advice on a per-proxy object basis, use a IsSingleton value of false and a IsFrozen value of false. If you want to use stateful advice--for example, for stateful mixins--use prototype advices along with a IsSingleton value of false.

ProxyFactoryObject Properties (http://www.springframework.net/doc/reference/html/aop.html#aop-pfb-2)

Prototype target sources (http://www.springframework.net/doc/reference/html/aop.html#aop-ts-prototype)


Cheers,
Bruno Baia

jamendoza
06-02-2006, 02:24 PM
Thanks a lot Bruno!!, that solved my configuration test, but, what about if i want to do it programatically?? How can i accomplish the same result without the overhead ??