|
|
@@ -11,7 +11,15 @@ import seecoder.devcloud.web.model.vo.APITest.APITestBodyVO;
|
|
|
import seecoder.devcloud.web.model.vo.APITest.APITestInfoVO;
|
|
|
import seecoder.devcloud.web.service.APITest.APITestService;
|
|
|
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
import java.util.*;
|
|
|
+import java.util.concurrent.CountDownLatch;
|
|
|
|
|
|
@Service
|
|
|
public class APITestServiceImpl implements APITestService {
|
|
|
@@ -28,7 +36,8 @@ public class APITestServiceImpl implements APITestService {
|
|
|
.createTime(apiTestBodyVO.getCreateTime())
|
|
|
.username(apiTestBodyVO.getUsername())
|
|
|
.url(apiTestBodyVO.getUrl())
|
|
|
- .params("{}")
|
|
|
+ .params(JSONObject.fromObject(apiTestBodyVO.getParams()).toString())
|
|
|
+ .method(apiTestBodyVO.getMethod())
|
|
|
.qps(apiTestBodyVO.getQps())
|
|
|
.state("进行中")
|
|
|
.success(0)
|
|
|
@@ -42,6 +51,7 @@ public class APITestServiceImpl implements APITestService {
|
|
|
public void deleteAPITest(Integer projectId) {
|
|
|
apiTestMapper.delete(projectId);
|
|
|
}
|
|
|
+
|
|
|
@Override
|
|
|
public List<APITestInfoVO> getAPITestsByProjectId(Integer projectId) {
|
|
|
List<APITest> apiTestInfoVOs = apiTestMapper.selectByProjectId(projectId);
|
|
|
@@ -52,6 +62,20 @@ public class APITestServiceImpl implements APITestService {
|
|
|
return apiTestInfoVOList;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public void executeAPITest(Integer testId) throws InterruptedException {
|
|
|
+ APITest apiTest = apiTestMapper.selectByTestId(testId);
|
|
|
+ String url = apiTest.getUrl();
|
|
|
+ Map<String, Object> params = parseJson2Map(apiTest.getParams());
|
|
|
+ int qps = apiTest.getQps();
|
|
|
+ String method = apiTest.getMethod();
|
|
|
+
|
|
|
+ System.out.println(method);
|
|
|
+ if (method.equals("GET")) { // GET方法
|
|
|
+ simulateHttpGet(url, params, qps, testId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
// 将数据库中读取的 APITestInfoMysql 转为 APITestInfo
|
|
|
public APITestInfo transform(APITest apiTest) {
|
|
|
APITestInfo apiTestInfo = new APITestInfo();
|
|
|
@@ -62,6 +86,7 @@ public class APITestServiceImpl implements APITestService {
|
|
|
apiTestInfo.setUsername(apiTest.getUsername());
|
|
|
apiTestInfo.setUrl(apiTest.getUrl());
|
|
|
apiTestInfo.setParams(parseJson2Map(apiTest.getParams()));
|
|
|
+ apiTestInfo.setMethod(apiTest.getMethod());
|
|
|
apiTestInfo.setQps(apiTest.getQps());
|
|
|
apiTestInfo.setState(apiTest.getState());
|
|
|
apiTestInfo.setExecuteTime(apiTest.getExecuteTime());
|
|
|
@@ -104,4 +129,147 @@ public class APITestServiceImpl implements APITestService {
|
|
|
}
|
|
|
return timeList;
|
|
|
}
|
|
|
+
|
|
|
+ // 处理GET方法
|
|
|
+ public void simulateHttpGet(String url, Map<String, Object> params, int qps, int testId) throws InterruptedException {
|
|
|
+ ArrayList<Integer> timeList = new ArrayList<>();
|
|
|
+ final int[] success = {0};
|
|
|
+ final int[] failure = {0};
|
|
|
+ Runnable task = new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ for (int i = 0; i < qps / 10; i++) {
|
|
|
+ int[] arr = doGet(url, params);
|
|
|
+ timeList.add(arr[1]);
|
|
|
+ if (arr[0] == 1) success[0]++;
|
|
|
+ else failure[0]++;
|
|
|
+ try{
|
|
|
+ Thread.sleep(100);
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+ APITestServiceImpl apiTestService = new APITestServiceImpl();
|
|
|
+ apiTestService.startTaskAllInOnce(10, task);
|
|
|
+ String tl = "";
|
|
|
+ for (int j = 0; j < timeList.size(); j++) {
|
|
|
+ if (j != timeList.size() - 1) tl += timeList.get(j).toString() + ",";
|
|
|
+ else tl += timeList.get(j).toString();
|
|
|
+ }
|
|
|
+ Date d = new Date();
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ String executeTime = sdf.format(d);
|
|
|
+ System.out.println(success[0]);
|
|
|
+ apiTestMapper.update("完成", executeTime, success[0], failure[0], tl, testId);
|
|
|
+ }
|
|
|
+
|
|
|
+ //
|
|
|
+ public int[] doGet(String url, Map<String, Object> params) {
|
|
|
+ HttpURLConnection connection = null;
|
|
|
+ BufferedReader br = null;
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
+ String result = "";
|
|
|
+ String path = "";
|
|
|
+ int[] arr = new int[2];
|
|
|
+ int flag = 0;
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
+ try {
|
|
|
+ if (params.size() == 1) {
|
|
|
+ for (String name:params.keySet()) {
|
|
|
+ sb.append(name).append("=").append(URLEncoder.encode((String) params.get(name), "UTF-8"));
|
|
|
+ }
|
|
|
+ path = sb.toString();
|
|
|
+ } else if (params.size() >= 2) {
|
|
|
+ for (String name:params.keySet()) {
|
|
|
+ sb.append(name).append("=").append(URLEncoder.encode((String) params.get(name), "UTF-8")).append("&");
|
|
|
+ }
|
|
|
+ String temp_path = sb.toString();
|
|
|
+ path = temp_path.substring(0, temp_path.length() - 1);
|
|
|
+ }
|
|
|
+ String full_url = "";
|
|
|
+ if (path != "") {
|
|
|
+ full_url = url + "?" + path;
|
|
|
+ } else {
|
|
|
+ full_url = url;
|
|
|
+ }
|
|
|
+
|
|
|
+// System.out.println(full_url);
|
|
|
+ URL connURL = new URL(full_url);
|
|
|
+
|
|
|
+ connection = (HttpURLConnection) connURL.openConnection();
|
|
|
+ // 设置一些属性
|
|
|
+// connection.setRequestMethod("GET");
|
|
|
+ connection.setRequestProperty("Accept", "*/*");
|
|
|
+ connection.setRequestProperty("Connection", "Keep-Alive");
|
|
|
+ connection.setRequestProperty("User-Agent",
|
|
|
+ "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
|
|
|
+ connection.setConnectTimeout(15000);
|
|
|
+ connection.setReadTimeout(60000);
|
|
|
+
|
|
|
+ // 建立连接
|
|
|
+ connection.connect();
|
|
|
+ if (connection.getResponseCode() == 200) {
|
|
|
+ flag = 1;
|
|
|
+ } else {
|
|
|
+ flag = 0;
|
|
|
+ }
|
|
|
+// Map<String, List<String>> headers = connection.getHeaderFields();
|
|
|
+// for (String key:headers.keySet()) {
|
|
|
+// System.out.println(key + "\t: \t" + headers.get(key));
|
|
|
+// }
|
|
|
+ br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
|
|
|
+ String line;
|
|
|
+ while ((line = br.readLine()) != null) {
|
|
|
+ result += line;
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ if (br != null)
|
|
|
+ br.close();
|
|
|
+ } catch (IOException ex) {
|
|
|
+ ex.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ long end = System.currentTimeMillis();
|
|
|
+// System.out.println(result);
|
|
|
+ int time = Long.valueOf(end - start).intValue();
|
|
|
+ arr[0] = flag;
|
|
|
+ arr[1] = time;
|
|
|
+ return arr;
|
|
|
+ }
|
|
|
+
|
|
|
+ public long startTaskAllInOnce(int threadNums, final Runnable task) throws InterruptedException {
|
|
|
+ final CountDownLatch startGate = new CountDownLatch(1);
|
|
|
+ final CountDownLatch endGate = new CountDownLatch(threadNums);
|
|
|
+ for (int i = 0; i < threadNums; i++) {
|
|
|
+ Thread t = new Thread() {
|
|
|
+ public void run() {
|
|
|
+ try {
|
|
|
+ startGate.await();
|
|
|
+ try {
|
|
|
+ task.run();
|
|
|
+ } finally {
|
|
|
+ endGate.countDown();
|
|
|
+ }
|
|
|
+ } catch (InterruptedException ie) {
|
|
|
+ ie.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+ t.start();
|
|
|
+ }
|
|
|
+ long startTime = System.nanoTime();
|
|
|
+ System.out.println(startTime + " [" + Thread.currentThread() + "] All thread is ready, concurrent going...");
|
|
|
+ // 因开启门只需一个开关,所以立马就开启开始门
|
|
|
+ startGate.countDown();
|
|
|
+ // 等等结束门开启
|
|
|
+ endGate.await();
|
|
|
+ long endTime = System.nanoTime();
|
|
|
+ System.out.println(endTime + " [" + Thread.currentThread() + "] All thread is completed.");
|
|
|
+ return endTime - startTime;
|
|
|
+ }
|
|
|
}
|