spring3.0框架检测方法运行时间测试(转)
主要利用了Spring AOP 技术,对想要统计的方法进行横切处理,方法执行前开始计时,方法执行后停止计时,得到计时方法就是该方法本次消耗时间。
步骤:
- 首先编写自己的Interceptor类来实现MethodInterceptor类,来用于切入方法,运行计时代码
- Spring AOP 的XML配置,配置需要监测的方法和切入方法(自定义的Interceptor)
1、自定义Intercept拦截器
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横向切面编程
<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、单元测试
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毫秒
spring3.0框架检测方法运行时间测试(转)的更多相关文章
- java监测方法运行时间/效率方法
前言: 这周在写一个小项目,虽然小但是是纯调外部接口的,调完了接口还不停的循环接口返回的数据(已转换JSONArray),然后再判断值,再做不同处理,关键是数据量还比较大,这刚做完还没开始上线,测试也 ...
- Spring3.0 与 MyBatis框架 整合小实例
本文将在Eclipse开发环境下,采用Spring MVC + Spring + MyBatis + Maven + Log4J 框架搭建一个Java web 项目. 1. 环境准备: 1.1 创建数 ...
- 开发基础框架:mybatis-3.2.8 +hibernate4.0+spring3.0+struts2.3
一:项目下载地址(点击 Source code(zip)) https://github.com/fzxblgong/frame_2014-12-15/releases 版本:v1.2大小:20M 二 ...
- SSH框架(四) struts2+spring3.0的登陆示例
(一)关键理念及需要注意的地方: 使用struts2+spring3.0的框架搭建web程序,就是使用spring来进行依赖注入(依赖注入请参考baidu上面的解释:http://baike.baid ...
- SSH (Struts2+Spring3.0+Hibernate3)框架(二) 框架的配置
一.准备工作: 1. JDK -> jdk1.6.0_17 安装(环境变量配置): JAVA_HOME = C:\ jdk1.6.0_17; PATH = %JAVA_HOME%\bin; %J ...
- 安卓4.0以上系统怎么不用root激活XPOSED框架的方法
在大多单位的引流或业务操作中,基本上都需要使用安卓的高端技术Xposed框架,近期,我们单位购买了一批新的安卓4.0以上系统,基本上都都是基于7.0以上版本,基本上都不能够刷入root超级权限,即便是 ...
- Fundebug前端JavaScript插件更新至1.6.0,新增test()方法用于测试
摘要: 1.6.0新增fundebug.test()方法用于测试,请大家及时更新. 默认情况下,Fundebug 插件能够自动捕获未处理的错误(uncaught error).另外,开发者也可以通过使 ...
- 测试JS方法运行时间
console.time(label) 和 console.timeEnd(label), 在开始的地方写上 console.time("测试 fn 速度: ") ,在结束的地方写 ...
- thinkPHP5.0框架验证码调用及点击图片刷新简单实现方法
这篇文章主要介绍了thinkPHP5.0框架验证码调用及点击图片刷新简单实现方法,结合简单示例形式分析了thinkPHP5框架验证码相关配置.后台验证.前台刷新等操作技巧,学习thinkphp源码的朋 ...
随机推荐
- Leetcode——Two Sum(easy)
题目:Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1] 代码: ...
- Jmeter多用户利用集合点瞬压并发测试
在测试一些限时秒杀类似的接口时,需要模拟多用户同时一瞬间访问接口,我们这里简单模拟多用户同时访问百度. 1.首先打开Jmeter,在测试计划下添加线程组. 2.在线程组下添加HTTP请求. 3.在HT ...
- 02.02.02 第2章 制作power bi图表(Power BI商业智能分析)
---恢复内容开始--- 02.02.02第2章 制作power bi图表 02.02.02.01 power pivot数据导入 00:08:43 02.02.02.02建立数据透视表 00:11: ...
- PE文件常用结构体
Dos头结构: typedef struct _IMAGE_DOS_HEADER { // DOS .EXE header WORD e_magic; // Magic number WORD e_c ...
- asp.net动态加载程序集创建指定类的实例及调用指定方法
以下类中有三个方法: LoadAssembly:加载指定路径的程序集 GetInstance:根据Type动态获取实例,用泛型接到返回的类型 ExecuteMothod:执行实例中的指定方法 /// ...
- 转发对python装饰器的理解
[Python] 对 Python 装饰器的理解的一些心得分享出来给大家参考 原文 http://blog.csdn.net/sxw3718401/article/details/3951958 ...
- What's the Difference Between Iterators and Generators in Python
https://www.quora.com/Whats-the-difference-between-iterators-and-generators-in-Python
- JAVA:简单添加菜单界面(swing)
package com.le.menu; import java.awt.Color; import java.awt.Container; import java.awt.FlowLayout; i ...
- jasperreports实现pdf文档的生成
1.导入jar包(pom.xml构建) <dependencies> <dependency> <groupId>com.lowagie</groupId&g ...
- linux vg lv pv
= pv由物理卷或者分区组成 pv可以组成一个或者多个vg vg可以分成多个lv 方便扩展 pvs vgs lvs 可以查看当前存在的pv vg lv 我的centos硬盘20g 使用了一 ...