Performance profiling with AspectJ AOP and Spring
Q. Can you profile the following sample test class with aspectj AOP and Spring?
package com.myapp; import java.util.concurrent.TimeUnit; public class TestClass { public void testMethod1() { System.out.println("executing testMethod1"); try { // just delay TimeUnit.MILLISECONDS.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } }
A. Here are the steps.
Step 1: Required libraries via maven pom.xml.
<properties> <spring.version>3.0.5.RELEASE</spring.version> </properties> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <!-- Spring AOP + AspectJ --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.6.11</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.11</version> </dependency>
Step 2: Aspect using aspectj.
package com.myapp; import java.util.Arrays; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.Signature; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; @Aspect @Component public class PerformanceProfilingAspect { @Around("execution( * com.myapp.*.* (..) )") public Object invoke(final ProceedingJoinPoint pjp) throws Throwable { Signature signature = pjp.getSignature(); Object[] args = pjp.getArgs(); String argList = Arrays.toString(args); System.out.println("Start executing: "+ signature.getDeclaringTypeName() + "." + signature.getName() + "(" + argList + ")"); long s = System.nanoTime(); //invoke the actual method Object proceed = pjp.proceed(args); long e = System.nanoTime(); System.out.println("Completed after: " + signature.getDeclaringTypeName() + "." + signature.getName() + "(" + argList + ") ended after " + ((double) (e - s)/1000000) + "ms"); return proceed; } }
Step 3: Spring config file test-spring-aop.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:batch="http://www.springframework.org/schema/batch" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <aop:aspectj-autoproxy /> <context:component-scan base-package="com.myapp"/> <bean id="performanceProfilingAspect" class="com.myapp.PerformanceProfilingAspect" /> <bean id="test" class="com.myapp.TestClass" /> </beans>
Step 4: the main class
package com.myapp; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class AspectJAopProfilingTest { public static void main(String[] args) { ApplicationContext appContext = new ClassPathXmlApplicationContext( "classpath:/test-spring-aop.xml"); TestClass test= (TestClass) appContext.getBean("test"); test.testMethod1(); } }
Step 5: The output
Start executing: com.myapp.TestClass.testMethod1([]) executing testMethod1 Completed after: com.myapp.TestClass.testMethod1([]) ended after 121.053234ms
Labels: AOP, aspectj, Performance, Spring
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home