Results 1 to 7 of 7

Thread: Performance Differences + Around advice

  1. #1

    Default Performance Differences + Around advice

    I am trying an example to understand around advice. I have written to classes Test and Main there is a lot of variation in execution times. Can any one tell me if I am missing anything. Why the performance differences happening.

    Code:
    // the business interface
    package com.jteam.springdemos.aspects.around;
    
    public interface Worker {
        void doSomeWork(int numOfTimes);
    }
    The implementation class
    Code:
    package com.jteam.springdemos.aspects.around;
    
    public class WorkerBean implements Worker {
    
        public void doSomeWork(int numOfTimes) {
            for &#40;int i = 0; i < numOfTimes; i++&#41; &#123;
                System.out.print&#40;""&#41;;
            &#125;
        &#125;
    &#125;
    The around advice
    Code:
    package com.jteam.springdemos.aspects.around;
    
    import java.lang.reflect.Method;
    
    import org.aopalliance.intercept.MethodInterceptor;
    import org.aopalliance.intercept.MethodInvocation;
    import org.springframework.util.StopWatch;
    
    public class AroundAdvice implements MethodInterceptor &#123;
    
        public Object invoke&#40;MethodInvocation invocation&#41; throws Throwable &#123;
    
            StopWatch sw = new StopWatch&#40;&#41;;
            sw.start&#40;invocation.getMethod&#40;&#41;.getName&#40;&#41;&#41;;
            // invoking...
            Object returnValue = invocation.proceed&#40;&#41;;
    
            // stopping watch after invocation
            sw.stop&#40;&#41;;
    
    
            Method m = invocation.getMethod&#40;&#41;;
            Object target = invocation.getThis&#40;&#41;;
            Object&#91;&#93; args = invocation.getArguments&#40;&#41;;
            System.out.println&#40;"Executed method&#58; " + m.getName&#40;&#41;&#41;;
            System.out.println&#40;"On object of type&#58; " + target.getClass&#40;&#41;.getName&#40;&#41;&#41;;
            System.out.println&#40;"With arguments&#58;"&#41;;
            for &#40;Object arg &#58; args&#41; &#123;
                System.out.println&#40;"---->" + arg&#41;;
            &#125;
            System.out.println&#40;&#41;;
            System.out.println&#40;"TOOK&#58; " + sw.getTotalTimeMillis&#40;&#41; + "ms!"&#41;;
    
            return returnValue;
        &#125;
    &#125;
    The beand definitions
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC  "-//SPRING//DTD BEAN//EN" "http&#58;//www.springframework.org/dtd/spring-beans.dtd">
    <beans>
    
        <!-- Bean configuration -->
        <bean id="businesslogicbean" class="org.springframework.aop.framework.ProxyFactoryBean">
             <property name="proxyTargetClass"><value>true</value></property>
            <property name="singleton">
                <value>false</value>
            </property>
            <property name="exposeProxy">
                <value>true</value>
            </property>
            <property name="proxyInterfaces">
                <value>com.jteam.springdemos.aspects.around.Worker</value>
            </property>
            <property name="target">
                <ref local="beanTarget"/>
            </property>
            <property name="interceptorNames">
                <list>
                    <value>theAroundAdvisor</value>
                </list>
            </property>
        </bean>
        <!-- Bean Classes -->
        <bean id="beanTarget" class="com.jteam.springdemos.aspects.around.WorkerBean"/>
    
        <!-- Advisor pointcut definition for around advice -->
        <bean id="theAroundAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
            <property name="advice">
                <ref local="theAroundAdvice"/>
            </property>
            <property name="pattern">
                <value>.*</value>
            </property>
        </bean>
    
        <!-- Advice classes -->
        <bean id="theAroundAdvice" class="com.jteam.springdemos.aspects.around.AroundAdvice"/>
    </beans>
    I have written to test classes as below:
    Code:
    package com.jteam.springdemos.aspects.around;
    
    import org.springframework.aop.framework.ProxyFactory;
    
    public class Test &#123;
        public static void main&#40;String&#91;&#93; args&#41; &#123;
            WorkerBean bean = getWorkerBean&#40;&#41;;
            bean.doSomeWork&#40;100000000&#41;;
        &#125;
    
        private static WorkerBean getWorkerBean&#40;&#41; &#123;
            WorkerBean target = new WorkerBean&#40;&#41;;
            ProxyFactory pf = new ProxyFactory&#40;&#41;;
            pf.setTarget&#40;target&#41;;
            pf.addAdvice&#40;new AroundAdvice&#40;&#41;&#41;;
            return &#40;WorkerBean&#41; pf.getProxy&#40;&#41;;
        &#125;
    &#125;
    and
    Code:
    package com.jteam.springdemos.aspects.around;
    
    import org.springframework.core.io.Resource;
    import org.springframework.core.io.ClassPathResource;
    import org.springframework.beans.factory.support.AbstractBeanFactory;
    import org.springframework.beans.factory.xml.XmlBeanFactory;
    
    public class Main &#123;
        public static void main&#40;String&#91;&#93; args&#41; &#123;
    
            // Read the configuration file
            Resource res = new ClassPathResource&#40;"com/jteam/springdemos/aspects/around/beandef.xml"&#41;;
            AbstractBeanFactory ft = new XmlBeanFactory&#40;res&#41;;
    
            //Instantiate an object
            Worker testObject = &#40;Worker&#41; ft.getBean&#40;"businesslogicbean"&#41;;
    
            // Execute the public method of the bean
            testObject.doSomeWork&#40;1000000&#41;;
        &#125;
    &#125;
    What I see is the Test class is giving 11438ms and Main class is giving just 125ms.

    Test class log:

    Sep 6, 2005 8:00:28 PM org.springframework.aop.framework.DefaultAopProxyF actory <clinit>
    INFO: CGLIB2 available: proxyTargetClass feature enabled
    Sep 6, 2005 8:00:28 PM org.springframework.core.CollectionFactory <clinit>
    INFO: JDK 1.4+ collections available
    Executed method: doSomeWork
    On object of type: com.jteam.springdemos.aspects.around.WorkerBean
    With arguments:
    ---->100000000

    TOOK: 11438ms!

    and Main class log

    org.springframework.beans.factory.xml.XmlBeanDefin itionReader loadBeanDefinitions
    INFO: Loading XML bean definitions from class path resource [com/jteam/springdemos/aspects/around/beandef.xml]
    Sep 6, 2005 7:58:49 PM org.springframework.beans.factory.support.Abstract BeanFactory getBean
    INFO: Creating shared instance of singleton bean 'businesslogicbean'
    Sep 6, 2005 7:58:49 PM org.springframework.aop.framework.DefaultAopProxyF actory <clinit>
    INFO: CGLIB2 available: proxyTargetClass feature enabled
    Sep 6, 2005 7:58:49 PM org.springframework.core.CollectionFactory <clinit>
    INFO: JDK 1.4+ collections available
    Sep 6, 2005 7:58:49 PM org.springframework.beans.factory.support.Abstract BeanFactory getBean
    INFO: Creating shared instance of singleton bean 'beanTarget'
    Sep 6, 2005 7:58:50 PM org.springframework.beans.factory.support.Abstract BeanFactory getBean
    INFO: Creating shared instance of singleton bean 'theAroundAdvisor'
    Sep 6, 2005 7:58:50 PM org.springframework.beans.factory.support.Abstract BeanFactory getBean
    INFO: Creating shared instance of singleton bean 'theAroundAdvice'
    Executed method: doSomeWork
    On object of type: com.jteam.springdemos.aspects.around.WorkerBean
    With arguments:
    ---->1000000

    TOOK: 125ms!

  2. #2
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    First remove the System.out.println - they ruin performance because of IO operations.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  3. #3
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    How long does it take *without* the advice?

  4. #4

    Default The difference of times

    With advice it is 125ms and with out 11438ms

    Why this much huge difference?

    Ramesh

  5. #5
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    With advice it is 125ms and with out 11438ms
    To rephrase what yatesco said, how much does it take if you don't enable the advice? This is important to see if the delay is actually inside the advice or in your code. Have you also removed the System.out?
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  6. #6

    Default

    Hello Costin,

    Sorry the test value I entered is not same. For one test 100000000 and another test 1000000. Sorry for taking up your time.

    Thanks & Regards
    Ramesh

  7. #7
    Join Date
    Sep 2005
    Location
    Vienna
    Posts
    44

    Default

    Hi,

    Quote Originally Posted by RameshMandaleeka
    Sorry the test value I entered is not same. For one test 100000000 and another test 1000000. Sorry for taking up your time.
    Now you know why they always say: define constants instead of using hard coded numbers (or strings etc.)

    & :wink:

    Erik

Similar Threads

  1. After advice design question
    By drc in forum AOP
    Replies: 2
    Last Post: Sep 29th, 2005, 11:14 PM
  2. May I advice a Servlet?
    By dobri in forum AOP
    Replies: 2
    Last Post: Apr 20th, 2005, 08:20 AM
  3. Replies: 5
    Last Post: Mar 28th, 2005, 08:41 AM
  4. Replies: 9
    Last Post: Dec 6th, 2004, 04:05 PM
  5. Replies: 2
    Last Post: Aug 16th, 2004, 12:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •