spring3.0框架检测方法运行时间测试(转)
主要利用了Spring AOP 技术,对想要统计的方法进行横切处理,方法执行前开始计时,方法执行后停止计时,得到计时方法就是该方法本次消耗时间。
步骤:
- 首先编写自己的Interceptor类来实现MethodInterceptor类,来用于切入方法,运行计时代码
- Spring AOP 的XML配置,配置需要监测的方法和切入方法(自定义的Interceptor)
1、自定义Intercept拦截器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | package com.utis.intercept; import java.util.HashMap; import java.util.Map; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; import org.apache.commons.lang.time.StopWatch; /** * 方法运行时间测试 * @author Saiteam * */ public class MethodTimeActive implements MethodInterceptor { /* * 自定义map集合,key:方法名,value:[0,运行次数,1:总时间] */ public static Map<String, Long[]> methodMap = new HashMap<String, Long[]>(); /* * 拦截要执行的方法 */ public Object invoke(MethodInvocation invocation) throws Throwable { System.out.println( "MethodTimeActive.invoke()" ); //1、创建一个计时器 StopWatch watch = new StopWatch(); //2、计时器开始 watch.start(); //3、执行方法 Object object = invocation.proceed(); //4、计时器停止 watch.stop(); //5、获取方法名称 String methodName = invocation.getMethod().getName(); //6、获取耗时多少 Long time = watch.getTime(); //7、判断方法执行了多少次,耗时多少 if (methodMap.containsKey(methodName)){ Long[] x = methodMap.get(methodName); x[ 0 ]++; x[ 1 ] +=time; } else { methodMap.put(methodName, new Long[]{1L,time}); } return object; } } |
2、配置applicationContext.xml文件,利用AOP横向切面编程
1 2 3 4 5 6 | <aop:config> <aop:pointcut id= "baseServiceMethods" expression= "execution(* com.booksys.service.*.*(..))" /> <aop:advisor advice-ref= "methodTimeActive" pointcut-ref= "baseServiceMethods" /> </aop:config> <bean id= "methodTimeActive" class = "com.utis.intercept.MethodTimeActive" ></bean> |
3、单元测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | package SSITest; import java.util.Map; import java.util.Set; import org.junit.After; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.booksys.service.BookService; import com.utis.intercept.MethodTimeActive; public class MethodInterTest { ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml" ); @Test public void testMethodTime(){ BookService bookService = (BookService) context.getBean( "bookService" ); System.out.println(bookService.findBookById( 1 ).getBookname()); } //----------------重要的是这个------------------------- @After public void testMethodActive(){ Map<String, Long[]> map = MethodTimeActive.methodMap; Set<String> set = map.keySet(); Long[] x = null ; for (String s : set){ x = map.get(s); System.out.println(s+ ":" +x[ 0 ]+ "次," +x[ 1 ]+ "毫秒" ); } } } |
测试结果:
MethodTimeActive.invoke()
11:46:20,912 DEBUG Connection:27 - {conn-100000} Connection
11:46:20,922 DEBUG Connection:27 - {conn-100000} Preparing Statement: select * from book where bookid=?
java基础教程
312
findBookById:1次,312毫秒
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 《HelloGitHub》第 108 期
· Windows桌面应用自动更新解决方案SharpUpdater5发布
· 我的家庭实验室服务器集群硬件清单
· C# 13 中的新增功能实操
· Supergateway:MCP服务器的远程调试与集成工具