当前位置:网站首页>Using custom annotations, statistical method execution time
Using custom annotations, statistical method execution time
2022-07-30 06:39:00 【Weizhi】
The project needs to count the execution time of some methods,The easiest way is to record the timestamp before the method executesstartTime,在方法结束前,用时间戳endTime-startTimeIt is time consuming to derive this method.
But in order to avoid codeless intrusion and achieve general purpose,So define an annotation,Which method to count,Just write an annotation on the method,The parameters of the method can be obtained through annotations、方法名、返回值等等信息.
Below is a simple implementation of time statistics:
1.定义一个注解TimeConsume
This annotation has a defaultvalue属性,valueThe value is a method name or a custom description
@Target({
ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface TimeConsume {
String value() default "方法";
}
2.使用AspectDefines the aspect of this annotationTimeConsumeAspect
定义好注解后,The class used for this annotation needs to be monitored,利用Spring框架Aspect实现切面,定义环绕通知,获取到方法的参数、方法名等信息,To facilitate statistical needs.Exception catch for execution method,在finally
The time statistics logic is implemented in the code block,Avoid method exceptions that cannot be counted.代码如下:
@Slf4j
@Component
@Aspect
public class TimeConsumeAspect {
/** * Pointcuts are defined as [email protected](Annotation classpath) */
@Pointcut("@annotation(com.weiller.demo.common.annotation.TimeConsume)")
public void consume(){
}
@Around("consume()")
public <T> T around(ProceedingJoinPoint pjp) throws Throwable {
Long startTime = System.currentTimeMillis();
Object[] args = pjp.getArgs();
T result;
Method methodClass;
try {
result = (T)pjp.proceed(args);//执行方法
}finally {
long endTime = System.currentTimeMillis();
Signature signature = pjp.getSignature();
String methodName = signature.getName();
Class<?> targetClass = pjp.getTarget().getClass();
Class[] parameterTypes = ((MethodSignature) pjp.getSignature()).getParameterTypes();
methodClass = targetClass.getMethod(methodName, parameterTypes);
Annotation[] annotations = methodClass.getAnnotations();
for (Annotation annotation : annotations){
Class<? extends Annotation> aClass = annotation.annotationType();
String simpleName = aClass.getSimpleName();
if("TimeConsume".equals(simpleName)){
TimeConsume timeConsume = (TimeConsume) annotation;
String value = timeConsume.value();
log.info(value+"[{}] 执行耗时:{}ms",methodName,endTime-startTime);
break;
}
}
}
return result;
}
}
3.used in the test target method
@RestController
@RequestMapping("/test")
public class testController {
@TimeConsume("测试")
@GetMapping("info")
public Object testInfo(){
Object parse = JSONObject.parse("{\n" +
"\t\t\"requestId\":\"\",\n" +
"\t\t\"appId\":\"\",\n" +
"\t\t\"nonce\":\"\",\n" +
"\t\t\"timestamp\":12345676543,\n" +
"\t\t\"signature\":\"\",\n" +
"\t\t\"sjgsd\":\"61000\",\n" +
"\t\t\"starTime\":12345676543\n" +
"\t}");
try {
Thread.sleep(new Random().nextInt(100));//Sleep at random time
} catch (InterruptedException e) {
e.printStackTrace();
}
return parse ;
}
}
边栏推荐
猜你喜欢
[Mozhe Academy] Identity Authentication Failure Vulnerability Actual Combat
oracle行转列、列转行总结
Dcat Admin 安装
FastAPI 快速入门
CTF之misc-图片隐写
DVWA installation tutorial (understand what you don't understand · in detail)
3 minutes to tell you how to become a hacker | Zero foundation to hacker introductory guide, you only need to master these five skills
Misc of CTF - other types of steganography
misc-file steganography of CTF
记一次流量分析实战——安恒科技(八月ctf)
随机推荐
umi后台项目导航自定义icon问题
五月份比赛WP
npm install和npm install --save
运算符和交互基础
浏览器缓存
【数仓】数据质量
SSTI靶场
POI工具类
3分钟告诉你如何成为一名黑客|零基础到黑客入门指南,你只需要掌握这五点能力
CTF之misc-其他类型隐写
互联网商城盲盒app为何如此火爆
利用自定义注解,统计方法执行时间
通信中间件 Fast DDS 基础概念简述与通信示例
典型线程问题综合演示
misc-file steganography of CTF
PHP-fpm
promise的基本概念
国内数字藏品交易平台开发市场会开放二级市场吗
C# WPF中监听窗口大小变化事件
Application Practice | Application Practice of Apache Doris in Baidu Intelligent Cloud Billing System