基于aop的接口性能测试工具类
2017-01-02 19:00
351 查看
import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.List; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; /** * ClassName: ApiAnalyzeTest.java <br> * Description:接口测试类 * 用于记录接口被调用时,每调用一次所需要的时间 * 使用方法: * 1.在spring.xml中添加<aop:aspectj-autoproxy/> * 2.调用接口,*\target\classes\api_test目录下会产生测试数据日志, * 3.执行本类中的main方法,即可获得测试数据的统计分析日志test_data_analyze.txt。 * <br> * Create by: name:yuruixin <br>email: ruixin_yu@asdc.com.cn <br> * Create Time: 2016年11月21日<br> */ @Aspect @Component public class ApiAnalyzeTest { private String dir; // 切入点要拦截的类 @Pointcut("execution (* com.cmcc.service.*.*(..))") private void anyMethod() { } @Before("anyMethod()") public void doAccessCheck() { // System.out.println("前置通知"); } // 后置通知(不需要获取返回值) @AfterReturning("anyMethod()") public void doAfterReturning() { // System.out.println("后置通知:"); } // 例外通知(不需要异常信息) @AfterThrowing("anyMethod()") public void doAfterThrowing1() { // System.out.println("例外通知"); } // 最终通知 @After("anyMethod()") public void doAfter() { // System.out.println("最终通知"); } /** * Description:利用环绕通知计算接口执行时间,并分接口写入文件。 * * @param pjp * @return * @throws Throwable * @return Object * @author name:yuruixin <br> * email: ruixin_yu@asdc.com.cn **/ @Around("anyMethod()") public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable { if (dir == null) { File f = new File(this.getClass().getResource("/").getPath()); dir = f.toString(); System.setProperty("dir", dir); } File file = new File(dir + "\\api_test\\"); // 如果文件夹不存在则创建 if (!file.exists() && !file.isDirectory()) { file.mkdirs(); } long startTime = System.currentTimeMillis(); // 获取开始时间 Object object = pjp.proceed(); long endTime = System.currentTimeMillis(); // 获取结束时间 long consuming = endTime - startTime; String temp = new Date().toLocaleString() + "接口:" + pjp.getSignature().getName() + "耗时:" + consuming + "ms 大小:" + object.toString().length() + "B\r\n"; String temp1 = consuming + "\r\n"; try { FileOutputStream fos = new FileOutputStream(file + "\\" + pjp.getSignature().getName() + "_test.txt", true);// true表示在文件末尾追加 FileOutputStream fos_data = new FileOutputStream( file + "\\" + pjp.getSignature().getName() + "_test_data.txt", true);// true表示在文件末尾追加 fos.write(temp.getBytes()); fos_data.write(temp1.getBytes()); fos.close(); fos_data.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } System.out.println(temp); return object; } public void analyze() throws NumberFormatException, IOException { if (dir == null) { File f = new File(this.getClass().getResource("/").getPath()); dir = f.toString(); System.setProperty("dir", dir); } File file = new File(dir + "\\api_test"); // 如果文件夹不存在则创建 if (!file.exists() && !file.isDirectory()) { file.mkdirs(); } String file_write = file + "\\test_data_analyze.txt"; List<String> apiList = new ArrayList<String>(); apiList.add("save"); apiList.add("login"); apiList.add("bindDeviceId"); apiList.add("updateBindDeviceName"); apiList.add("unbindDeviceId"); apiList.add("findEvents"); for (int apiIndex = 0; apiIndex < apiList.size(); apiIndex++) { String file_read = file + "\\" + apiList.get(apiIndex) + "_test_data.txt"; if(new File(file_read).exists()){ List<Integer> timeConsuming_list = new ArrayList<Integer>(); // 文件绝对路径 FileReader fr = new FileReader(file_read); BufferedReader br = new BufferedReader(fr); long times = 0; long timeConsuming = 0; String s; while ((s = br.readLine()) != null) { timeConsuming_list.add(Integer.parseInt(s)); times++; timeConsuming += Integer.parseInt(s); } Collections.sort(timeConsuming_list); String temp = new Date().toLocaleString() + "|" + apiList.get(apiIndex) + "共执行" + times + "次,耗时最高" + timeConsuming_list.get(timeConsuming_list.size() - 1) + "ms 最低" + timeConsuming_list.get(0) + "ms 平均" + timeConsuming / times + "ms \r\n"; FileOutputStream fos = new FileOutputStream(file_write, true);// true表示在文件末尾追加 fos.write(temp.getBytes()); fos.close(); br.close(); } new File(file_read).delete(); } } public void readLog() throws NumberFormatException, IOException { if (dir == null) { File f = new File(this.getClass().getResource("/").getPath()); dir = f.toString(); } File file = new File(dir + "\\api_test"); // 如果文件夹不存在则创建 if (!file.exists() && !file.isDirectory()) { file.mkdirs(); } String file_write = file + "\\all_data_analyze.txt"; // 文件绝对路径 FileReader fr = new FileReader("D:\\yanglaoLogs\\log.txt"); BufferedReader br = new BufferedReader(fr); StringBuffer sBuffer = new StringBuffer(); String s; while ((s = br.readLine()) != null) { if(s.split("耗时").length>1){ sBuffer.append(s); sBuffer.append("\r\n"); } } br.close(); FileOutputStream fos = new FileOutputStream(file_write, true);// true表示在文件末尾追加 fos.write(sBuffer.toString().getBytes()); fos.close(); } public static void main(String[] args) throws Exception { new ApiAnalyzeTest().analyze(); new ApiAnalyzeTest().readLog(); } }
相关文章推荐
- 基于在生产环境中使用php性能测试工具xhprof的详解
- JMeter基于http请求的web接口性能测试总结
- 基于LR的HTTP协议接口性能测试脚本实例
- 转:几种基于HTTP协议的RPC性能比较,以及ICE接口性能测试比较
- 基于python 的性能测试工具
- 【原创】相对完整的一套以Jmeter作为工具的性能测试教程(接口性能测试,数据库性能测试以及服务器端性能监测)
- 基于jmeter,jenkins,ANT接口,性能测试框架
- 基于 CIMPLE 接口的 CIM Provider 的实现及其性能测试
- 基于在生产环境中使用php性能测试工具xhprof的详解
- hprose接口性能测试工具-附带源码
- jmeter性能测试,基于scf框架的"java请求"接口封装、环境配置与测试
- 小公司0成本基于Pythony的单元\GUI\Web自动化\性能的几个开源软件测试工具
- 基于手工接口测试的工具
- 基于在生产环境中使用php性能测试工具xhprof的详解(转)
- 基于在生产环境中使用php性能测试工具xhprof的详解
- p-unit简介 -- 一款开源的支持多线程,不同java虚拟机,性能测试工具
- 服务器性能测试典型工具介绍
- 基于反向代理的Web缓存加速现代理服务器方案 附squid性能测试
- 认识 p-unit : 一款开源的性能测试工具
- SOAP协议性能测试工具—SOAP Performer 0.1正式发布