{"id":8635,"date":"2012-09-25T20:37:09","date_gmt":"2012-09-25T15:07:09","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=8635"},"modified":"2012-09-25T20:57:20","modified_gmt":"2012-09-25T15:27:20","slug":"how-to-implemnet-aop-profiling-in-grails-application","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/how-to-implemnet-aop-profiling-in-grails-application\/","title":{"rendered":"How to implement AOP Profiling in Grails application"},"content":{"rendered":"<p>In one of my recent project, i want to profile method execution time. I have used Spring AOP to profile method execution time.<\/p>\n<p style=\"padding-bottom: 10px\">\n<p>It&#8217;s very easy to implement AOP profiling in grails<\/p>\n<p style=\"padding-bottom: 10px\">\n<p>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.<\/p>\n<p>[groovy]<br \/>\npackage com.service<\/p>\n<p>import com.test.Student<br \/>\nclass StudentService {<br \/>\n    Student saveDataStudent(Student student) throws Exception{<br \/>\n        return student.save(flush: true,failOnError: true)<br \/>\n    }<\/p>\n<p>    Student saveDataUpdated(Student student){<br \/>\n        return student.save(flush: true,failOnError: true)<br \/>\n    }<br \/>\n}<br \/>\n[\/groovy]<\/p>\n<p style=\"padding-bottom: 10px\">\n<p>2. Use following aspect code for profiling.<\/p>\n<p>[groovy]<br \/>\npackage com.spring3.base.test<\/p>\n<p>import org.aspectj.lang.JoinPoint<br \/>\nimport org.aspectj.lang.ProceedingJoinPoint<br \/>\nimport org.springframework.stereotype.Component<br \/>\nimport org.springframework.util.StopWatch<br \/>\nimport org.aspectj.lang.annotation.*<\/p>\n<p>@Aspect<br \/>\n@Component<br \/>\nclass LoggerInterceptor {<\/p>\n<p>     @Around(&quot;execution(* saveData*(..))&quot;)<br \/>\n    public Object profileSaveMethod(ProceedingJoinPoint call) throws Throwable {<br \/>\n        StopWatch clock = new StopWatch(&quot;Profiling:::::&quot; + call.signature + &quot;:::::Args::::::&quot; + call.args<br \/>\n        );<br \/>\n        try {<br \/>\n            clock.start(call.toShortString());<br \/>\n            return call.proceed();<br \/>\n        } finally {<br \/>\n            clock.stop();<br \/>\n            System.out.println(clock.prettyPrint());<br \/>\n        }<br \/>\n    }<\/p>\n<p>}<br \/>\n[\/groovy]<\/p>\n<p style=\"padding-bottom: 10px\">\n<p>@Around(&#8220;execution(* saveData*(..))&#8221;) : It represents that this advice will execute for those methods whose name is starting with saveData.<\/p>\n<p style=\"padding-bottom: 10px\">\n<p>Hope this code  will help  you \ud83d\ude42<\/p>\n<p style=\"padding-bottom: 10px\">\n<p>To know more about AOP, you can take the reference from following links:<\/p>\n<p><a href=\"http:\/\/www.tothenew.com\/blog\/integrating-of-spring-aop-with-grails-application\/\" target=\"_blank\">http:\/\/www.tothenew.com\/blog\/integrating-of-spring-aop-with-grails-application\/<\/a><br \/>\n<a href=\"http:\/\/static.springsource.org\/spring\/docs\/2.5.5\/reference\/aop.html\" target=\"_blank\">http:\/\/static.springsource.org\/spring\/docs\/2.5.5\/reference\/aop.html<\/a><\/p>\n<p style=\"padding-bottom: 10px\">\n","protected":false},"excerpt":{"rendered":"<p>In one of my recent project, i want to profile method execution time. I have used Spring AOP to profile method execution time. It&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":47,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":20},"categories":[7],"tags":[888,4840,9,4844,1073,4841],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8635"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/47"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=8635"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8635\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=8635"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=8635"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=8635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}