Announcement Announcement Module
Collapse
No announcement yet.
Performance Differences + Around advice Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    First remove the System.out.println - they ruin performance because of IO operations.

    Comment


    • #3
      How long does it take *without* the advice?

      Comment


      • #4
        The difference of times

        With advice it is 125ms and with out 11438ms

        Why this much huge difference?

        Ramesh

        Comment


        • #5
          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?

          Comment


          • #6
            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

            Comment


            • #7
              Hi,

              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

              Comment

              Working...
              X