|
我们想监控SQL执行的效率,打出执行时间。一般Spring项目都是用AOP拦截,然后计算方法开始结束时间,再减一下,就得到执行花的时间了。那Grails里更简单,不用自己写代码,其实用的是Spring的PerformanceMonitorInterceptor。
修改grails-app\conf\spring\resources.groovy
beans = {
xmlns aop:"http://www.springframework.org/schema/aop"
aop {
config("proxy-target-class":true) {
pointcut(id:"interceptorPointcut", expression:"execution(* org.apache.ibatis.session.SqlSession.*(..))")
advisor( 'pointcut-ref': "interceptorPointcut", 'advice-ref':"preformanceMonitoringInterceptorAdvice")
}
}
preformanceMonitoringInterceptorAdvice(org.springframework.aop.interceptor.PerformanceMonitorInterceptor, true){
loggerName = "grailsee.performanceMonitor"
}
}
然后修改grails-app\conf\Config.groovy,加入以下代码
debug 'java.sql.Connection',
'java.sql.Statement',
'java.sql.PreparedStatement'
//'org.mybatis',
//'org.apache.ibatis',
//'org.codehaus.groovy.grails.plugins.ibatis'
trace 'grailsee.performanceMonitor'
OK,然后执行SQL时就自动拦截计算执行时间了,看一下控制台上,有如下信息:
grailsee.performanceMonitor StopWatch 'org.apache.ibatis.session.SqlSession.selectOne': running time (millis) = 8482
执行此SQL花了8秒,搞定!
可以参考http://www.objectpartners.com/2010/10/19/grails-plumbing-spring-aop-interceptors/
如果要监控真实SQL方便调试的话,可以参考另一篇文章
在Grails下查看真实的SQL
(不过我测试下来没有成功。Grails2.1.0) |
|
|