diff --git a/backend/src/main/java/io/metersphere/api/jmeter/APIBackendListenerClient.java b/backend/src/main/java/io/metersphere/api/jmeter/APIBackendListenerClient.java index ff556e6a0e..96c6be1038 100644 --- a/backend/src/main/java/io/metersphere/api/jmeter/APIBackendListenerClient.java +++ b/backend/src/main/java/io/metersphere/api/jmeter/APIBackendListenerClient.java @@ -20,92 +20,88 @@ import java.util.concurrent.ConcurrentHashMap; */ public class APIBackendListenerClient extends AbstractBackendListenerClient implements Serializable { - // 与前端JMXGenerator的SPLIT对应,用于获取 测试名称 和 测试ID - private final static String SPLIT = "@@:"; - // 测试ID作为key - private final Map> queue = new ConcurrentHashMap<>(); + private final static String THREAD_SPLIT = " "; + + private final static String ID_SPLIT = "-"; + + private final static String TEST_ID = "id"; + + private final List queue = new ArrayList<>(); + + private APITestService apiTestService; + + private APIReportService apiReportService; + + // 测试ID + private String id; + + @Override + public void setupTest(BackendListenerContext context) throws Exception { + this.id = context.getParameter(TEST_ID); + apiTestService = CommonBeanFactory.getBean(APITestService.class); + if (apiTestService == null) { + LogUtil.error("apiTestService is required"); + } + + apiReportService = CommonBeanFactory.getBean(APIReportService.class); + if (apiReportService == null) { + LogUtil.error("apiReportService is required"); + } + } @Override public void handleSampleResults(List sampleResults, BackendListenerContext context) { - sampleResults.forEach(result -> { - // 将不同的测试脚本按测试ID分开 - String label = result.getSampleLabel(); - if (!label.contains(SPLIT)) { - LogUtil.error("request name format is invalid, name: " + label); - return; - } - String name = label.split(SPLIT)[0]; - String testId = label.split(SPLIT)[1]; - if (!queue.containsKey(testId)) { - List testResults = new ArrayList<>(); - queue.put(testId, testResults); - } - result.setSampleLabel(name); - queue.get(testId).add(result); - }); + queue.addAll(sampleResults); } @Override public void teardownTest(BackendListenerContext context) throws Exception { - APITestService apiTestService = CommonBeanFactory.getBean(APITestService.class); - if (apiTestService == null) { - LogUtil.error("apiTestService is required"); - return; - } + TestResult testResult = new TestResult(); + testResult.setTestId(id); + testResult.setTotal(queue.size()); - APIReportService apiReportService = CommonBeanFactory.getBean(APIReportService.class); - if (apiReportService == null) { - LogUtil.error("apiReportService is required"); - return; - } + // 一个脚本里可能包含多个场景(ThreadGroup),所以要区分开,key: 场景Id + final Map scenarios = new LinkedHashMap<>(); - queue.forEach((id, sampleResults) -> { - TestResult testResult = new TestResult(); - testResult.setTestId(id); - testResult.setTotal(sampleResults.size()); + queue.forEach(result -> { + // 线程名称: <场景名> <场景Index>-<请求Index>, 例如:Scenario 2-1 + String scenarioName = StringUtils.substringBeforeLast(result.getThreadName(), THREAD_SPLIT); + String index = StringUtils.substringAfterLast(result.getThreadName(), THREAD_SPLIT); + String scenarioId = StringUtils.substringBefore(index, ID_SPLIT); + ScenarioResult scenarioResult; + if (!scenarios.containsKey(scenarioId)) { + scenarioResult = new ScenarioResult(); + scenarioResult.setId(scenarioId); + scenarioResult.setName(scenarioName); + scenarios.put(scenarioId, scenarioResult); + } else { + scenarioResult = scenarios.get(scenarioId); + } - // key: 场景Id - final Map scenarios = new LinkedHashMap<>(); + if (result.isSuccessful()) { + scenarioResult.addSuccess(); + testResult.addSuccess(); + } else { + scenarioResult.addError(result.getErrorCount()); + testResult.addError(result.getErrorCount()); + } - sampleResults.forEach(result -> { - String thread = StringUtils.substringBeforeLast(result.getThreadName(), " "); - String order = StringUtils.substringAfterLast(result.getThreadName(), " "); - String scenarioName = StringUtils.substringBefore(thread, SPLIT); - String scenarioId = StringUtils.substringAfter(thread, SPLIT); - ScenarioResult scenarioResult; - if (!scenarios.containsKey(scenarioId)) { - scenarioResult = new ScenarioResult(); - scenarioResult.setId(scenarioId); - scenarioResult.setName(scenarioName); - scenarioResult.setOrder(StringUtils.substringBefore(order, "-")); - scenarios.put(scenarioId, scenarioResult); - } else { - scenarioResult = scenarios.get(scenarioId); - } + RequestResult requestResult = getRequestResult(result); + scenarioResult.getRequestResults().add(requestResult); + scenarioResult.addResponseTime(result.getTime()); - if (result.isSuccessful()) { - scenarioResult.addSuccess(); - testResult.addSuccess(); - } else { - scenarioResult.addError(result.getErrorCount()); - testResult.addError(result.getErrorCount()); - } + testResult.addPassAssertions(requestResult.getPassAssertions()); + testResult.addTotalAssertions(requestResult.getTotalAssertions()); - RequestResult requestResult = getRequestResult(result); - scenarioResult.getRequestResults().add(requestResult); - scenarioResult.addResponseTime(result.getTime()); - - testResult.addPassAssertions(requestResult.getPassAssertions()); - testResult.addTotalAssertions(requestResult.getTotalAssertions()); - - scenarioResult.addPassAssertions(requestResult.getPassAssertions()); - scenarioResult.addTotalAssertions(requestResult.getTotalAssertions()); - }); - testResult.getScenarios().addAll(scenarios.values()); - testResult.getScenarios().sort(Comparator.comparing(ScenarioResult::getOrder)); - apiTestService.changeStatus(id, APITestStatus.Completed); - apiReportService.complete(testResult); + scenarioResult.addPassAssertions(requestResult.getPassAssertions()); + scenarioResult.addTotalAssertions(requestResult.getTotalAssertions()); }); + + testResult.getScenarios().addAll(scenarios.values()); + testResult.getScenarios().sort(Comparator.comparing(ScenarioResult::getId)); + apiTestService.changeStatus(id, APITestStatus.Completed); + apiReportService.complete(testResult); + queue.clear(); super.teardownTest(context); } diff --git a/backend/src/main/java/io/metersphere/api/jmeter/ScenarioResult.java b/backend/src/main/java/io/metersphere/api/jmeter/ScenarioResult.java index d9f2b7d4ca..331b9b8ab1 100644 --- a/backend/src/main/java/io/metersphere/api/jmeter/ScenarioResult.java +++ b/backend/src/main/java/io/metersphere/api/jmeter/ScenarioResult.java @@ -12,8 +12,6 @@ public class ScenarioResult { private String name; - private String order; - private long responseTime; private int error = 0; diff --git a/backend/src/main/java/io/metersphere/service/UserRoleService.java b/backend/src/main/java/io/metersphere/service/UserRoleService.java index a9d48fdf0c..2cdbb8963e 100644 --- a/backend/src/main/java/io/metersphere/service/UserRoleService.java +++ b/backend/src/main/java/io/metersphere/service/UserRoleService.java @@ -45,11 +45,11 @@ public class UserRoleService { for (int i = 0; i < collect.size(); i++) { Map map = new HashMap<>(2); map.put("id", collect.get(i)); - map.put("Ids", new ArrayList<>()); + map.put("ids", new ArrayList<>()); for (int j = 0; j < userRoles.size(); j++) { String role = userRoles.get(j).getRoleId(); if (StringUtils.equals(role, collect.get(i))) { - List ids = (List) map.get("Ids"); + List ids = (List) map.get("ids"); ids.add(userRoles.get(j).getSourceId()); } } diff --git a/backend/src/main/java/io/metersphere/service/UserService.java b/backend/src/main/java/io/metersphere/service/UserService.java index 4a66cc868a..f96c460205 100644 --- a/backend/src/main/java/io/metersphere/service/UserService.java +++ b/backend/src/main/java/io/metersphere/service/UserService.java @@ -80,10 +80,10 @@ public class UserService { userRole.setSourceId("adminSourceId"); userRoleMapper.insertSelective(userRole); } else { -// if (!map.keySet().contains("Ids")) { +// if (!map.keySet().contains("ids")) { // MSException.throwException(role + " no source id"); // } - List list = (List) map.get("Ids"); + List list = (List) map.get("ids"); for (int j = 0; j < list.size(); j++) { UserRole userRole1 = new UserRole(); userRole1.setId(UUID.randomUUID().toString()); diff --git a/frontend/src/business/components/api/report/ApiReportView.vue b/frontend/src/business/components/api/report/ApiReportView.vue index 08d6aa1bf8..8f4997d733 100644 --- a/frontend/src/business/components/api/report/ApiReportView.vue +++ b/frontend/src/business/components/api/report/ApiReportView.vue @@ -1,38 +1,26 @@