|
|
@@ -1,10 +1,14 @@
|
|
|
package cn.seecoder.web.service.impl.APITest;
|
|
|
|
|
|
import cn.seecoder.web.dao.APITest.APITestMapper;
|
|
|
+import cn.seecoder.web.dao.pipeline.PipelineMapper;
|
|
|
+import cn.seecoder.web.dao.pipeline.PipelineRecordMapper;
|
|
|
import cn.seecoder.web.model.po.APITest.APITestInfo;
|
|
|
import cn.seecoder.web.model.po.APITest.APITestResult;
|
|
|
import cn.seecoder.web.model.po.APITest.TestInfo;
|
|
|
import cn.seecoder.web.model.po.APITest.TestResult;
|
|
|
+import cn.seecoder.web.model.po.pipeline.PipelinePO;
|
|
|
+import cn.seecoder.web.model.po.pipeline.PipelineRecordPO;
|
|
|
import cn.seecoder.web.model.vo.APITest.APITestBodyVO;
|
|
|
import cn.seecoder.web.model.vo.APITest.APITestInfoVO;
|
|
|
import cn.seecoder.web.model.vo.APITest.APITestResultVO;
|
|
|
@@ -22,10 +26,12 @@ import java.io.OutputStreamWriter;
|
|
|
import java.net.HttpURLConnection;
|
|
|
import java.net.URL;
|
|
|
import java.net.URLEncoder;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
import java.text.DecimalFormat;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.CountDownLatch;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
@Service
|
|
|
public class APITestServiceImpl implements APITestService {
|
|
|
@@ -33,9 +39,15 @@ public class APITestServiceImpl implements APITestService {
|
|
|
|
|
|
private final APITestMapper apiTestMapper;
|
|
|
|
|
|
+ private final PipelineMapper pipelineMapper;
|
|
|
+
|
|
|
+ private final PipelineRecordMapper pipelineRecordMapper;
|
|
|
+
|
|
|
@Autowired
|
|
|
- public APITestServiceImpl(APITestMapper apiTestMapper) {
|
|
|
+ public APITestServiceImpl(APITestMapper apiTestMapper, PipelineMapper pipelineMapper, PipelineRecordMapper pipelineRecordMapper) {
|
|
|
this.apiTestMapper = apiTestMapper;
|
|
|
+ this.pipelineMapper = pipelineMapper;
|
|
|
+ this.pipelineRecordMapper = pipelineRecordMapper;
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -78,13 +90,13 @@ public class APITestServiceImpl implements APITestService {
|
|
|
Map<String, Object> params = parseJson2Map(testInfo.getParams());
|
|
|
int qps = testInfo.getQps();
|
|
|
String method = testInfo.getMethod();
|
|
|
-
|
|
|
+ Integer projectId = testInfo.getProjectId();
|
|
|
//
|
|
|
|
|
|
if (method.equals("GET")) { // GET方法
|
|
|
- handleRequest(url, params, qps, testId, "GET");
|
|
|
+ handleRequest(url, params, qps, testId, "GET", projectId);
|
|
|
} else if (method.equals("POST")) { // POST方法
|
|
|
- handleRequest(url, params, qps, testId, "POST");
|
|
|
+ handleRequest(url, params, qps, testId, "POST", projectId);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -105,15 +117,13 @@ public class APITestServiceImpl implements APITestService {
|
|
|
}
|
|
|
// 将JSON格式的字符串转为 Map类型
|
|
|
public Map<String, Object> parseJson2Map(String str) {
|
|
|
- Map<String, Object> map = new HashMap<String, Object>();
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
JSONObject json = JSONObject.fromObject(str);
|
|
|
for (Object k:json.keySet()) {
|
|
|
Object v = json.get(k);
|
|
|
if (v instanceof JSONArray) {
|
|
|
- List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
|
|
|
- Iterator<JSONObject> it = ((JSONArray)v).iterator();
|
|
|
- while (it.hasNext()) {
|
|
|
- JSONObject json2 = it.next();
|
|
|
+ List<Map<String, Object>> list = new ArrayList<>();
|
|
|
+ for (JSONObject json2 : (Iterable<JSONObject>) v) {
|
|
|
list.add(parseJson2Map(json2.toString()));
|
|
|
}
|
|
|
map.put(k.toString(), list);
|
|
|
@@ -155,7 +165,7 @@ public class APITestServiceImpl implements APITestService {
|
|
|
|
|
|
// 分隔字符串
|
|
|
public List<Integer> splitTimeList(String time) {
|
|
|
- List<Integer> timeList = new ArrayList<Integer>();
|
|
|
+ List<Integer> timeList = new ArrayList<>();
|
|
|
if (time.length() == 0) {
|
|
|
return timeList;
|
|
|
}
|
|
|
@@ -167,36 +177,33 @@ public class APITestServiceImpl implements APITestService {
|
|
|
}
|
|
|
|
|
|
// 处理请求
|
|
|
- public void handleRequest(String url, Map<String, Object> params, int qps, int testId, String method) throws InterruptedException {
|
|
|
+ public void handleRequest(String url, Map<String, Object> params, int qps, int testId, String method, Integer projectId) 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 = new int[2];
|
|
|
- if (method.equals("GET")) {
|
|
|
- arr = doGet(url, params);
|
|
|
- } else if (method.equals("POST")) {
|
|
|
- try {
|
|
|
- arr = doPost(url, params);
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
- timeList.add(arr[1]);
|
|
|
- if (arr[0] == 1) success[0]++;
|
|
|
- else failure[0]++;
|
|
|
- try{
|
|
|
- Thread.sleep(100);
|
|
|
- } catch (InterruptedException e) {
|
|
|
+ Runnable task = () -> {
|
|
|
+ for (int i = 0; i < qps / 10; i++) {
|
|
|
+ int[] arr = new int[2];
|
|
|
+ if (method.equals("GET")) {
|
|
|
+ arr = doGet(url, params);
|
|
|
+ } else if (method.equals("POST")) {
|
|
|
+ try {
|
|
|
+ arr = doPost(url, params);
|
|
|
+ } catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
+ 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(apiTestMapper);
|
|
|
+ APITestServiceImpl apiTestService = new APITestServiceImpl(apiTestMapper, pipelineMapper, pipelineRecordMapper);
|
|
|
apiTestService.startTaskAllInOnce(10, task);
|
|
|
Integer max_time = Collections.max(timeList);
|
|
|
Integer min_time = Collections.min(timeList);
|
|
|
@@ -205,33 +212,44 @@ public class APITestServiceImpl implements APITestService {
|
|
|
sum_time += time;
|
|
|
}
|
|
|
DecimalFormat df = new DecimalFormat("#.00");
|
|
|
- String tl = "";
|
|
|
+ StringBuilder tl = new StringBuilder();
|
|
|
for (int j = 0; j < timeList.size(); j++) {
|
|
|
- if (j != timeList.size() - 1) tl += timeList.get(j).toString() + ",";
|
|
|
- else tl += timeList.get(j).toString();
|
|
|
+ if (j != timeList.size() - 1) tl.append(timeList.get(j).toString()).append(",");
|
|
|
+ else tl.append(timeList.get(j).toString());
|
|
|
}
|
|
|
Date d = new Date();
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
String executeTime = sdf.format(d);
|
|
|
+
|
|
|
+ //获取最新的部署构建信息id
|
|
|
+ List<Integer> pipelineIds = pipelineMapper.selectByProjectId(projectId).stream().map(PipelinePO::getId).collect(Collectors.toList());
|
|
|
+ PipelineRecordPO latestRecord;
|
|
|
+ if (pipelineIds.size() == 0){
|
|
|
+ latestRecord = null;
|
|
|
+ } else {
|
|
|
+ latestRecord = pipelineRecordMapper.selectPipelinesLatestRecord(pipelineIds);
|
|
|
+ }
|
|
|
+
|
|
|
TestResult testResult = TestResult.builder()
|
|
|
.testId(testId)
|
|
|
.executeTime(executeTime)
|
|
|
.success(success[0])
|
|
|
.failure(failure[0])
|
|
|
- .timeList(tl)
|
|
|
+ .timeList(tl.toString())
|
|
|
.maxTime(max_time)
|
|
|
.minTime(min_time)
|
|
|
.averageTime((Double.valueOf(df.format(sum_time / qps))))
|
|
|
+ .pipelineHistoryId(latestRecord==null? -1:latestRecord.getId())
|
|
|
.build();
|
|
|
apiTestMapper.execute(testResult);
|
|
|
}
|
|
|
|
|
|
// 处理GET请求
|
|
|
public int[] doGet(String url, Map<String, Object> params) {
|
|
|
- HttpURLConnection connection = null;
|
|
|
+ HttpURLConnection connection;
|
|
|
BufferedReader br = null;
|
|
|
- StringBuffer sb = new StringBuffer();
|
|
|
- String result = "";
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ StringBuilder result = new StringBuilder();
|
|
|
String path = "";
|
|
|
int[] arr = new int[2];
|
|
|
int flag = 0;
|
|
|
@@ -249,8 +267,8 @@ public class APITestServiceImpl implements APITestService {
|
|
|
String temp_path = sb.toString();
|
|
|
path = temp_path.substring(0, temp_path.length() - 1);
|
|
|
}
|
|
|
- String full_url = "";
|
|
|
- if (path != "") {
|
|
|
+ String full_url;
|
|
|
+ if (!path.equals("")) {
|
|
|
full_url = url + "?" + path;
|
|
|
} else {
|
|
|
full_url = url;
|
|
|
@@ -278,10 +296,10 @@ public class APITestServiceImpl implements APITestService {
|
|
|
} else {
|
|
|
flag = 0;
|
|
|
}
|
|
|
- br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
|
|
|
+ br = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
|
|
|
String line;
|
|
|
while ((line = br.readLine()) != null) {
|
|
|
- result += line;
|
|
|
+ result.append(line);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
@@ -307,12 +325,12 @@ public class APITestServiceImpl implements APITestService {
|
|
|
String param = gson.toJson(params);
|
|
|
OutputStreamWriter out = null;
|
|
|
BufferedReader reader = null;
|
|
|
- String response = "";
|
|
|
+ StringBuilder response = new StringBuilder();
|
|
|
long start = System.currentTimeMillis();
|
|
|
int[] arr = new int[2];
|
|
|
int flag = 0;
|
|
|
try {
|
|
|
- URL httpUrl = null;
|
|
|
+ URL httpUrl;
|
|
|
httpUrl = new URL(url);
|
|
|
HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();
|
|
|
conn.setRequestMethod("POST");
|
|
|
@@ -336,8 +354,8 @@ public class APITestServiceImpl implements APITestService {
|
|
|
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
|
|
String lines;
|
|
|
while ((lines = reader.readLine()) != null) {
|
|
|
- lines = new String(lines.getBytes(), "utf-8");
|
|
|
- response += lines;
|
|
|
+ lines = new String(lines.getBytes(), StandardCharsets.UTF_8);
|
|
|
+ response.append(lines);
|
|
|
}
|
|
|
reader.close();
|
|
|
// 断开连接
|
|
|
@@ -369,20 +387,18 @@ public class APITestServiceImpl implements APITestService {
|
|
|
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() {
|
|
|
+ Thread t = new Thread(() -> {
|
|
|
+ try {
|
|
|
+ startGate.await();
|
|
|
try {
|
|
|
- startGate.await();
|
|
|
- try {
|
|
|
- task.run();
|
|
|
- } finally {
|
|
|
- endGate.countDown();
|
|
|
- }
|
|
|
- } catch (InterruptedException ie) {
|
|
|
- ie.printStackTrace();
|
|
|
+ task.run();
|
|
|
+ } finally {
|
|
|
+ endGate.countDown();
|
|
|
}
|
|
|
+ } catch (InterruptedException ie) {
|
|
|
+ ie.printStackTrace();
|
|
|
}
|
|
|
- };
|
|
|
+ });
|
|
|
t.start();
|
|
|
}
|
|
|
long startTime = System.nanoTime();
|