当前位置:网站首页>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,在finallyThe 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 ;
}
}
边栏推荐
- torch distributed training
- JDBC一文搞懂
- 关于浅拷贝和深拷贝,草稿闲了写
- Application Practice | Application Practice of Apache Doris in Baidu Intelligent Cloud Billing System
- 事件高级:事件的绑定及取消、DOM事件流、事件委托
- C#中default关键字用法简介
- Misc of CTF-Memory Analysis (Volatility)
- 【文献阅读】Age Progress/Regression by Conditional Adversarial Autoencoder 基于条件对抗自编码器(CAAE)的老化/去龄化方案
- [PASECA2019]honey_shop
- CTF之misc-文件隐写
猜你喜欢
随机推荐
C#中使用OleDb操作access数据库
标准输入输出流(System.in,System.out)
记一次流量分析实战——安恒科技(八月ctf)
Application Practice | Application Practice of Apache Doris in Baidu Intelligent Cloud Billing System
torch分布式训练
盲注、报错注入、宽字节注入、堆叠注入学习笔记
2022CISCNmisc
运算符和交互基础
【OS】操作系统高频面试题英文版(1)
[Mozhe Academy] Identity Authentication Failure Vulnerability Actual Combat
Sql操作
C#中对委托的理解和使用
网上说的挖矿究竟是什么? 挖矿系统开发详解介绍
CTF之misc-文件隐写
C# WPF中监听窗口大小变化事件
C# WPF下限制TextBox只输入数字、小数点、删除等键
强国杯初赛WP
C#下利用开源NPlot绘制股票十字交叉线
CTFSHOW command execution [web29-web124] unfinished to be continued
最新Redistemplate配置及使用,附带操作工具类,测试类



![[MATLAB]图像处理——交通标志的识别](/img/45/1a5797a17ebf6db965a64c85e0f037.png)



