In one of my recent project, i want to profile method execution time. I have used Spring AOP to profile method execution time.
It’s very easy to implement AOP profiling in grails
1. Suppose we have below mentioned service class, we want to log execution time of method saveDataStudent,saveDataUpdated methods in service class. We need to write aspect for it.
package com.service
import com.test.Student
class StudentService {
Student saveDataStudent(Student student) throws Exception{
return student.save(flush: true,failOnError: true)
}
Student saveDataUpdated(Student student){
return student.save(flush: true,failOnError: true)
}
}
2. Use following aspect code for profiling.
package com.spring3.base.test
import org.aspectj.lang.JoinPoint
import org.aspectj.lang.ProceedingJoinPoint
import org.springframework.stereotype.Component
import org.springframework.util.StopWatch
import org.aspectj.lang.annotation.*
@Aspect
@Component
class LoggerInterceptor {
@Around("execution(* saveData*(..))")
public Object profileSaveMethod(ProceedingJoinPoint call) throws Throwable {
StopWatch clock = new StopWatch("Profiling:::::" + call.signature + ":::::Args::::::" + call.args
);
try {
clock.start(call.toShortString());
return call.proceed();
} finally {
clock.stop();
System.out.println(clock.prettyPrint());
}
}
}
@Around(“execution(* saveData*(..))”) : It represents that this advice will execute for those methods whose name is starting with saveData.
Hope this code will help you
To know more about AOP, you can take the reference from following links:
http://www.intelligrape.com/blog/2012/08/27/integrating-of-spring-aop-with-grails-application/
http://static.springsource.org/spring/docs/2.5.5/reference/aop.html
Thanks & Regards,
Mohit Garg
mohit@intelligrape.com
@gargmohit143
