您的位置:首页 > 运维架构

基于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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: