使用AOP统计方法的执行时间

本文介绍了在性能调优中,如何利用AOP(面向切面编程)来统计方法的执行时间,避免传统方式带来的代码耦合。通过Spring AOP配合AspectJ和相关库,创建了一个DaoTimeInterceptor,实现了对指定包下方法的执行时间监控,当超过一定阈值时,打印执行详情。
摘要由CSDN通过智能技术生成
1.  需求 背景
性能调优阶段,需要找出执行时间比较长的方法,针对这些方法进行调优。
 
2.  可行方案
一种是传统的在每个方法前后获取System.currentMis(),然后得到方法的执行时间。
这种方式的缺点是方法多会写很多耦合代码,而且不可重用,测试完需要删掉。
 
另一种使用AOP监控方法的前后点,监控方法的执行时间,比较优雅且无侵入。
可行的方案之一是使用 Around Advice, 环绕通知可在方法执行前后做一些操作。
 
AOP,面向切面编程,Aspect Oriented Programming, 可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术
 
3. AOP
jar librarys requires:
<dependency>
     <groupId>org.aspectj</groupId>
     <artifactId>aspectjweaver</artifactId>
    <version>1.5.4</version>
</dependency>
<dependency>
    <groupId>cglib</groupId>
     <artifactId>cglib-nodep</artifactId>
      <version>2.2</version>
</dependency>
<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>4.3.6</version>
</dependency>
 
Spring配置:
<aop:aspectj-autoproxy proxy-target-class="true"/>
  
AOP实现类:
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

/**
 * Created by jzq1999 on 2017/4/19.
 * 通过aop拦截后执行具体操作
 */
@Aspect
@Component
public class DaoTimeInterceptor {

    private final static String POINT = "execution (* com.tzx.*.po.springjdbc.dao.imp.*.*(..))";

    @Pointcut(POINT)
    public void recordLog(){}

    @Around("recordLog()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable{
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        Object obj = pjp.proceed(pjp.getArgs());
        stopWatch.stop();

        long cost = stopWatch.getTotalTimeMillis();
        if(cost > 10) {
            MethodSignature signature = (MethodSignature) pjp.getSignature();
            String methodName = signature.getDeclaringTypeName() + "." + signature.getName();

            System.out.println("----------- 执行" + methodName + "方法, 用时: " + cost + "ms -----------");
        }

        return obj;
    }

}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值