废话少说,直接上代码!!!
一、利用aop实现接口耗时统计
import com.xxxx.cloud.business.dataVisualize.query.SludgeCompositeQuery;
import lombok.extern.slf4j.Slf4j;
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.springframework.stereotype.Component;
/**
* @className: TimeAspect
* @author: xxxxxxxxxx
* @Date: 2022/01/15
**/
@Aspect
@Slf4j
@Component
public class TimeAspect {
//使用正则表达式的方式给SludgeCompositeService这个接口中的所有实现的方法调用加上耗时统计
@Pointcut("execution(* com.xxxx.cloud.business.dataVisualize.service.SludgeCompositeService.*(*))")
public void sludgeCompositeServicePointCut() {
}
@Around("sludgeCompositeServicePointCut()")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
long start = System.currentTimeMillis();
String methodName = joinPoint.getSignature().getName();
try {
return joinPoint.proceed();
} finally {
long finish = System.currentTimeMillis();
log.info("===========调用" + methodName + "方法总耗时 :======={} 毫秒 。", (finish - start));
}
}
//给具体的方法加上耗时统计(并且带上方法上的具体参数sludgeCompositeQuery)
@Pointcut(value = "execution(* com.xxxx.cloud.business.dataVisualize.service.SludgeCompositeService.getRingChartData(com" +
".xxxx.cloud.business.dataVisualize.query.SludgeCompositeQuery)) && args(sludgeCompositeQuery)", argNames =
"sludgeCompositeQuery")
public void getRingChartDataPointCut(SludgeCompositeQuery sludgeCompositeQuery) {
}
@Around("getRingChartDataPointCut(sludgeCompositeQuery)")
public Object getRingChartData(ProceedingJoinPoint joinPoint, SludgeCompositeQuery sludgeCompositeQuery) throws Throwable {
long start = System.currentTimeMillis();
String methodName = joinPoint.getSignature().getName();
try {
return joinPoint.proceed();
} finally {
long finish = System.currentTimeMillis();
log.info("===========调用" + methodName + "方法总耗时 :======={} 毫秒 。", (finish - start));
}
}
//给具体的方法加上耗时统计(方法上的具体参数用正则表达式表示)(*表示任何参数)
@Pointcut(value = "execution(* com.xxxx.cloud.business.dataVisualize.service.SludgeCompositeService.getCurveChartData(*))")
public void getCurveChartDataPointCut() {
}
@Around("getCurveChartDataPointCut()")
public Object getCurveChartData(ProceedingJoinPoint joinPoint) throws Throwable {
long start = System.currentTimeMillis();
String methodName = joinPoint.getSignature().getName();
try {
return joinPoint.proceed();
} finally {
long finish = System.currentTimeMillis();
log.info("===========调用" + methodName + "方法总耗时 :======={} 毫秒 。", (finish - start));
}
}
}
内心OS:虽然这种方式解决了代码污染的问题,但是每次计算一个接口的耗时统计还是要写很多代码,颜(逼)值(格)这么高的我,怎么能容忍这么low的事情发生呢!!!
不多BB,还是直接上代码!!!
二、注解的方式实现接口耗时统计
1、建立注解TimeStatistics
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TimeStatistics {
}
2、建立切面类
@Aspect
@Slf4j
@Component
public class TimeAspect {
@Around("@annotation(TimeStatistics)")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
long start = System.currentTimeMillis();
String methodName = joinPoint.getSignature().getName();
try {
return joinPoint.proceed();
} finally {
long finish = System.currentTimeMillis();
log.info("===========调用" + methodName + "方法总耗时 :======={} 毫秒 。", (finish - start));
}
}
}
3、在需要耗时统计的接口上或方法上添加@TimeStatistics即可
@Override
@TimeStatistics
public RingChartData getRingChartData(SludgeCompositeQuery sludgeCompositeQuery) {
RingChartData ringChartData = new RingChartData();
List<WasteTypeDataInfo> wasteTypeDataInfoList = this.getWasteTypeDataInfo(sludgeCompositeQuery);
List<CarTypeDataInfo> carTypeDataInfoList = this.getCarTypeDataInfo(sludgeCompositeQuery);
List<DealArtDataInfo> dealArtDataInfoList = this.getDealArtDataInfo(sludgeCompositeQuery);
RingChartData ringChartDataProcess = this.processRingChartData(wasteTypeDataInfoList,carTypeDataInfoList,dealArtDataInfoList);
BeanUtils.copyProperties(ringChartDataProcess, ringChartData);
ringChartData.setWasteTypeDataInfoList(wasteTypeDataInfoList);
ringChartData.setCarTypeDataInfoList(carTypeDataInfoList);
ringChartData.setDealArtDataInfoList(dealArtDataInfoList);
return ringChartData;
}