当前位置:   article > 正文

网上找了一段利用aop计算耗时日志的_aop 效率

aop 效率
  1. import org.aspectj.lang.ProceedingJoinPoint;
  2. import org.aspectj.lang.annotation.Around;
  3. import org.aspectj.lang.annotation.Aspect;
  4. import org.springframework.stereotype.Component;
  5. import com.alibaba.common.lang.diagnostic.Profiler;
  6. @Aspect
  7. @Component
  8. public class ProfilerAspectJ {
  9. private static final String DOT_SPLIT = ".";
  10. //执行时间超过3s 的,输出出来
  11. private static final String THREADHOLD = "THREADHOLD";
  12. private static final long DEFAULTTHREADHOLD = 3000L;
  13. @Around("within(com.aliyun.activity.biz.*.impl..*)")
  14. public Object around(ProceedingJoinPoint jp) throws java.lang.Throwable {
  15. String className = jp.getTarget().getClass().getName();
  16. String method = jp.getSignature().getName();
  17. final String key = className + DOT_SPLIT + method;
  18. System.out.println("className: "+className);
  19. try {
  20. // 开始 Profiler
  21. Profiler.start(key);
  22. Profiler.enter(key);
  23. // 调用被代理类的方法
  24. return jp.proceed();
  25. } finally {
  26. Profiler.release();
  27. // 释放 start 的栈
  28. Profiler.release();
  29. try {
  30. long threadHold = DEFAULTTHREADHOLD;
  31. final long duration = Profiler.getDuration();
  32. if (duration > threadHold) {
  33. final String detail = Profiler.dump("Detail: ", " ");
  34. StringBuffer msg = new StringBuffer();
  35. msg.append("调用服务:").append(className).append("的方法").append(method);
  36. if (null != jp.getArgs()) {
  37. msg.append(" 参数列表 [");
  38. for (int i = 0; i < jp.getArgs().length; i++) {
  39. Object obj = jp.getArgs()[i];
  40. msg.append(obj);
  41. if (i < jp.getArgs().length - 1) {
  42. msg.append(",");
  43. }
  44. }
  45. msg.append("]");
  46. }
  47. msg.append("耗时:").append(duration).append("ms,超过预期\n").append(detail).append("\n");
  48. }
  49. } finally {
  50. Profiler.reset();
  51. }
  52. }
  53. }
  54. }

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/823454
推荐阅读
相关标签
  

闽ICP备14008679号