refactor(接口测试): 重构任务中心全部停止方法提升性能

--bug=1013002 --user=赵勇 【测试计划】github#13137任务中心全部停止接口响应时间太长 https://www.tapd.cn/55049933/s/1153389
This commit is contained in:
fit2-zhao 2022-05-06 14:29:35 +08:00 committed by 刘瑞斌
parent d36090178c
commit e4411ded9a
12 changed files with 280 additions and 164 deletions

View File

@ -41,6 +41,9 @@ public class ApiCaseParallelExecuteService {
// 获取可以执行的资源池 // 获取可以执行的资源池
BaseSystemConfigDTO baseInfo = CommonBeanFactory.getBean(SystemParameterService.class).getBaseInfo(); BaseSystemConfigDTO baseInfo = CommonBeanFactory.getBean(SystemParameterService.class).getBaseInfo();
for (String testId : executeQueue.keySet()) { for (String testId : executeQueue.keySet()) {
if (Thread.currentThread().isInterrupted()) {
break;
}
ApiDefinitionExecResult result = executeQueue.get(testId); ApiDefinitionExecResult result = executeQueue.get(testId);
String reportId = result.getId(); String reportId = result.getId();
JmeterRunRequestDTO runRequest = new JmeterRunRequestDTO(testId, reportId, runMode, null); JmeterRunRequestDTO runRequest = new JmeterRunRequestDTO(testId, reportId, runMode, null);

View File

@ -131,7 +131,7 @@ public class ExecThreadPoolExecutor {
BlockingQueue workerQueue = threadPool.getQueue(); BlockingQueue workerQueue = threadPool.getQueue();
workerQueue.forEach(item -> { workerQueue.forEach(item -> {
ExecTask task = (ExecTask) item; ExecTask task = (ExecTask) item;
if (task.getRequest() != null && StringUtils.equals(task.getRequest().getReportId(), reportId)) { if (task != null && task.getRequest() != null && StringUtils.equals(task.getRequest().getReportId(), reportId)) {
workerQueue.remove(item); workerQueue.remove(item);
} }
}); });

View File

@ -36,6 +36,9 @@ public class ApiScenarioParallelService {
// 获取可以执行的资源池 // 获取可以执行的资源池
BaseSystemConfigDTO baseInfo = CommonBeanFactory.getBean(SystemParameterService.class).getBaseInfo(); BaseSystemConfigDTO baseInfo = CommonBeanFactory.getBean(SystemParameterService.class).getBaseInfo();
for (String reportId : executeQueue.keySet()) { for (String reportId : executeQueue.keySet()) {
if (Thread.currentThread().isInterrupted()) {
break;
}
RunModeDataDTO dataDTO = executeQueue.get(reportId); RunModeDataDTO dataDTO = executeQueue.get(reportId);
JmeterRunRequestDTO runRequest = new JmeterRunRequestDTO(dataDTO.getTestId(), StringUtils.isNotEmpty(serialReportId) ? serialReportId : reportId, request.getRunMode(), null); JmeterRunRequestDTO runRequest = new JmeterRunRequestDTO(dataDTO.getTestId(), StringUtils.isNotEmpty(serialReportId) ? serialReportId : reportId, request.getRunMode(), null);
runRequest.setReportType(StringUtils.isNotEmpty(serialReportId) ? RunModeConstants.SET_REPORT.toString() : RunModeConstants.INDEPENDENCE.toString()); runRequest.setReportType(StringUtils.isNotEmpty(serialReportId) ? RunModeConstants.SET_REPORT.toString() : RunModeConstants.INDEPENDENCE.toString());

View File

@ -15,7 +15,7 @@ public class JmeterThreadUtils {
currentGroup.enumerate(lstThreads); currentGroup.enumerate(lstThreads);
StringBuilder threadNames = new StringBuilder(); StringBuilder threadNames = new StringBuilder();
for (int i = 0; i < noThreads; i++) { for (int i = 0; i < noThreads; i++) {
if (StringUtils.isNotEmpty(lstThreads[i].getName()) && lstThreads[i].getName().startsWith(name)) { if (lstThreads[i]!=null && StringUtils.isNotEmpty(lstThreads[i].getName()) && lstThreads[i].getName().startsWith(name)) {
System.out.println("异常强制处理线程编号:" + i + " = " + lstThreads[i].getName()); System.out.println("异常强制处理线程编号:" + i + " = " + lstThreads[i].getName());
LogUtil.error("异常强制处理线程编号:" + i + " = " + lstThreads[i].getName()); LogUtil.error("异常强制处理线程编号:" + i + " = " + lstThreads[i].getName());
threadNames.append(lstThreads[i].getName()).append(""); threadNames.append(lstThreads[i].getName()).append("");

View File

@ -459,6 +459,30 @@ public class ApiExecutionQueueService {
}); });
} }
public void stop(List<String> reportIds) {
ApiExecutionQueueDetailExample example = new ApiExecutionQueueDetailExample();
example.createCriteria().andReportIdIn(reportIds);
List<ApiExecutionQueueDetail> details = executionQueueDetailMapper.selectByExample(example);
List<String> queueIds = new ArrayList<>();
details.forEach(item -> {
if (!queueIds.contains(item.getQueueId())) {
queueIds.add(item.getQueueId());
}
});
executionQueueDetailMapper.deleteByExample(example);
for (String queueId : queueIds) {
ApiExecutionQueue queue = queueMapper.selectByPrimaryKey(queueId);
// 更新测试计划报告
if (queue != null && StringUtils.isNotEmpty(queue.getReportId())) {
CommonBeanFactory.getBean(TestPlanReportService.class).finishedTestPlanReport(queue.getReportId(), "Stopped");
queueMapper.deleteByPrimaryKey(queueId);
}
}
}
/** /**
* 性能测试监听检查 * 性能测试监听检查
* *

View File

@ -1,10 +1,10 @@
package io.metersphere.base.mapper.ext; package io.metersphere.base.mapper.ext;
import io.metersphere.api.dto.QueryAPIReportRequest; import io.metersphere.api.dto.QueryAPIReportRequest;
import io.metersphere.api.dto.automation.APIScenarioReportResult;
import io.metersphere.api.dto.datacount.ExecutedCaseInfoResult; import io.metersphere.api.dto.datacount.ExecutedCaseInfoResult;
import io.metersphere.base.domain.ApiDefinitionExecResult; import io.metersphere.base.domain.ApiDefinitionExecResult;
import io.metersphere.base.domain.ApiDefinitionExecResultExpand; import io.metersphere.base.domain.ApiDefinitionExecResultExpand;
import io.metersphere.task.dto.TaskCenterRequest;
import io.metersphere.track.dto.PlanReportCaseDTO; import io.metersphere.track.dto.PlanReportCaseDTO;
import org.apache.ibatis.annotations.InsertProvider; import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
@ -44,4 +44,7 @@ public interface ExtApiDefinitionExecResultMapper {
@InsertProvider(type = ExtApiDefinitionExecResultProvider.class, method = "insertListSql") @InsertProvider(type = ExtApiDefinitionExecResultProvider.class, method = "insertListSql")
void sqlInsert(List<ApiDefinitionExecResult> list); void sqlInsert(List<ApiDefinitionExecResult> list);
List<ApiDefinitionExecResult> findByProjectIds(@Param("request") TaskCenterRequest request);
} }

View File

@ -275,4 +275,11 @@
#{value} #{value}
</foreach> </foreach>
</update> </update>
<select id="findByProjectIds" resultType="io.metersphere.base.domain.ApiDefinitionExecResult" parameterType="java.lang.String">
select actuator ,id from api_definition_exec_result where status in ("running","starting","waiting") and project_id in
<foreach collection="request.projects" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</select>
</mapper> </mapper>

View File

@ -5,6 +5,7 @@ import io.metersphere.api.dto.automation.APIScenarioReportResult;
import io.metersphere.api.dto.datacount.ApiDataCountResult; import io.metersphere.api.dto.datacount.ApiDataCountResult;
import io.metersphere.base.domain.ApiScenarioReport; import io.metersphere.base.domain.ApiScenarioReport;
import io.metersphere.dto.ApiReportCountDTO; import io.metersphere.dto.ApiReportCountDTO;
import io.metersphere.task.dto.TaskCenterRequest;
import io.metersphere.track.dto.PlanReportCaseDTO; import io.metersphere.track.dto.PlanReportCaseDTO;
import org.apache.ibatis.annotations.InsertProvider; import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
@ -44,4 +45,7 @@ public interface ExtApiScenarioReportMapper {
@InsertProvider(type = ExtApiScenarioReportProvider.class, method = "insertListSql") @InsertProvider(type = ExtApiScenarioReportProvider.class, method = "insertListSql")
void sqlInsert(List<APIScenarioReportResult> list); void sqlInsert(List<APIScenarioReportResult> list);
List<ApiScenarioReport> findByProjectIds(@Param("request") TaskCenterRequest request);
} }

View File

@ -428,4 +428,12 @@
#{value} #{value}
</foreach> </foreach>
</update> </update>
<select id="findByProjectIds" resultType="io.metersphere.base.domain.ApiScenarioReport" parameterType="java.lang.String">
select actuator ,id from api_scenario_report where status in ("running","starting","waiting") and project_id in
<foreach collection="request.projects" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</select>
</mapper> </mapper>

View File

@ -18,4 +18,8 @@ public interface ExtTaskMapper {
List<String> checkActuator (@Param("actuator") String actuator); List<String> checkActuator (@Param("actuator") String actuator);
int stopScenario(@Param("request") TaskCenterRequest request);
int stopApi(@Param("request") TaskCenterRequest request);
} }

View File

@ -4,58 +4,68 @@
<select id="getTasks" resultType="io.metersphere.task.dto.TaskCenterDTO" <select id="getTasks" resultType="io.metersphere.task.dto.TaskCenterDTO"
parameterType="java.lang.String"> parameterType="java.lang.String">
SELECT tt.* FROM ( SELECT tt.* FROM (
(select t.id,if(t.scenario_id like "[\"%\"]", t.name,t.scenario_name) as name ,'SCENARIO' as executionModule,t.report_type, ifnull(t2.name,'LOCAL') as actuator, t1.`name` as executor,t.create_time as executionTime, t.trigger_mode as triggerMode ,t.status as executionStatus (select t.id,if(t.scenario_id like "[\"%\"]", t.name,t.scenario_name) as name ,'SCENARIO' as
from api_scenario_report t left join `user` t1 ON t.user_id = t1.id left join test_resource_pool t2 on t.actuator = t2.id executionModule,t.report_type, ifnull(t2.name,'LOCAL') as actuator, t1.`name` as executor,t.create_time as
where to_days(FROM_UNIXTIME(t.create_time/1000))= to_days(now()) and t.execute_type !='Debug' and t.execute_type !='Marge' and t.project_id in executionTime, t.trigger_mode as triggerMode ,t.status as executionStatus
<foreach collection="request.projects" item="id" separator="," open="(" close=")"> from api_scenario_report t left join `user` t1 ON t.user_id = t1.id left join test_resource_pool t2 on
#{id} t.actuator = t2.id
</foreach> where to_days(FROM_UNIXTIME(t.create_time/1000))= to_days(now()) and t.execute_type !='Debug' and t.execute_type
<if test="request.triggerMode != null and request.triggerMode != ''"> !='Marge' and t.project_id in
and t.trigger_mode = #{request.triggerMode} <foreach collection="request.projects" item="id" separator="," open="(" close=")">
</if> #{id}
<if test="request.executionStatus != null and request.executionStatus != ''"> </foreach>
and t.status = #{request.executionStatus} <if test="request.triggerMode != null and request.triggerMode != ''">
</if> and t.trigger_mode = #{request.triggerMode}
<if test="request.executor != null and request.executor != ''"> </if>
and t.user_id = #{request.executor} <if test="request.executionStatus != null and request.executionStatus != ''">
</if> and t.status = #{request.executionStatus}
) </if>
UNION ALL <if test="request.executor != null and request.executor != ''">
(select t.id,t.name,'API' as executionModule, t.report_type, ifnull(t2.name,'LOCAL') as actuator, t1.`name` as executor,t.create_time as executionTime, ifnull(t.trigger_mode,'MANUAL') as triggerMode ,ifnull(t.status,'Saved') as executionStatus and t.user_id = #{request.executor}
from api_definition_exec_result t left join `user` t1 ON t.user_id = t1.id left join test_resource_pool t2 on t.actuator = t2.id </if>
where to_days(FROM_UNIXTIME(t.create_time/1000))= to_days(now()) and t.project_id in )
<foreach collection="request.projects" item="id" separator="," open="(" close=")"> UNION ALL
#{id} (select t.id,t.name,'API' as executionModule, t.report_type, ifnull(t2.name,'LOCAL') as actuator, t1.`name` as
</foreach> executor,t.create_time as executionTime, ifnull(t.trigger_mode,'MANUAL') as triggerMode
,ifnull(t.status,'Saved') as executionStatus
<if test="request.triggerMode != null and request.triggerMode != ''"> from api_definition_exec_result t left join `user` t1 ON t.user_id = t1.id left join test_resource_pool t2 on
and t.trigger_mode = #{request.triggerMode} t.actuator = t2.id
</if> where to_days(FROM_UNIXTIME(t.create_time/1000))= to_days(now()) and t.project_id in
<if test="request.executionStatus != null and request.executionStatus != ''"> <foreach collection="request.projects" item="id" separator="," open="(" close=")">
and t.status = #{request.executionStatus} #{id}
</if> </foreach>
<if test="request.executor != null and request.executor != ''">
and t.user_id = #{request.executor} <if test="request.triggerMode != null and request.triggerMode != ''">
</if> and t.trigger_mode = #{request.triggerMode}
and (t.integrated_report_id is null or t.integrated_report_id = 'null') </if>
<if test="request.executionStatus != null and request.executionStatus != ''">
and t.status = #{request.executionStatus}
</if>
<if test="request.executor != null and request.executor != ''">
and t.user_id = #{request.executor}
</if>
and (t.integrated_report_id is null or t.integrated_report_id = 'null')
)
UNION ALL
(select t.id,t.name,'PERFORMANCE' as executionModule,'PERFORMANCE' as report_type, ifnull(t2.name,'LOCAL') as
actuator, t1.`name` as executor,t.create_time as executionTime, t.trigger_mode as triggerMode ,t.`status` as
executionStatus
from load_test_report t left join `user` t1 ON t.user_id = t1.id left join test_resource_pool t2 on
t.test_resource_pool_id = t2.id
where to_days(FROM_UNIXTIME(t.create_time/1000))= to_days(now()) and t.project_id in
<foreach collection="request.projects" item="id" separator="," open="(" close=")">
#{id}
</foreach>
<if test="request.triggerMode != null and request.triggerMode != ''">
and t.trigger_mode = #{request.triggerMode}
</if>
<if test="request.executionStatus != null and request.executionStatus != ''">
and t.status = #{request.executionStatus}
</if>
<if test="request.executor != null and request.executor != ''">
and t.user_id = #{request.executor}
</if>
) )
UNION ALL
(select t.id,t.name,'PERFORMANCE' as executionModule,'PERFORMANCE' as report_type, ifnull(t2.name,'LOCAL') as actuator, t1.`name` as executor,t.create_time as executionTime, t.trigger_mode as triggerMode ,t.`status` as executionStatus
from load_test_report t left join `user` t1 ON t.user_id = t1.id left join test_resource_pool t2 on t.test_resource_pool_id = t2.id
where to_days(FROM_UNIXTIME(t.create_time/1000))= to_days(now()) and t.project_id in
<foreach collection="request.projects" item="id" separator="," open="(" close=")">
#{id}
</foreach>
<if test="request.triggerMode != null and request.triggerMode != ''">
and t.trigger_mode = #{request.triggerMode}
</if>
<if test="request.executionStatus != null and request.executionStatus != ''">
and t.status = #{request.executionStatus}
</if>
<if test="request.executor != null and request.executor != ''">
and t.user_id = #{request.executor}
</if>
)
)tt ORDER BY tt.executionTime DESC )tt ORDER BY tt.executionTime DESC
</select> </select>
@ -78,61 +88,70 @@
<select id="getRunningTasks" resultType="java.lang.Integer" parameterType="java.lang.String"> <select id="getRunningTasks" resultType="java.lang.Integer" parameterType="java.lang.String">
SELECT count(tt.id) FROM ( SELECT count(tt.id) FROM (
(select t.id,'SCENARIO' as executionModule,t.report_type, ifnull(t2.name,'LOCAL') as actuator, t1.`name` as executor,t.create_time as executionTime, t.trigger_mode as triggerMode ,t.status as executionStatus (select t.id,'SCENARIO' as executionModule,t.report_type, ifnull(t2.name,'LOCAL') as actuator, t1.`name` as
from api_scenario_report t left join `user` t1 ON t.user_id = t1.id left join test_resource_pool t2 on t.actuator = t2.id executor,t.create_time as executionTime, t.trigger_mode as triggerMode ,t.status as executionStatus
where to_days(FROM_UNIXTIME(t.create_time/1000))= to_days(now()) and t.execute_type !='Debug' and t.execute_type !='Marge' and t.project_id in from api_scenario_report t left join `user` t1 ON t.user_id = t1.id left join test_resource_pool t2 on
<foreach collection="request.projects" item="id" separator="," open="(" close=")"> t.actuator = t2.id
#{id} where to_days(FROM_UNIXTIME(t.create_time/1000))= to_days(now()) and t.execute_type !='Debug' and t.execute_type
</foreach> !='Marge' and t.project_id in
<if test="request.triggerMode != null and request.triggerMode != ''"> <foreach collection="request.projects" item="id" separator="," open="(" close=")">
and t.trigger_mode = #{request.triggerMode} #{id}
</if> </foreach>
<if test="request.executionStatus != null and request.executionStatus != ''"> <if test="request.triggerMode != null and request.triggerMode != ''">
and t.status = #{request.executionStatus} and t.trigger_mode = #{request.triggerMode}
</if> </if>
<if test="request.executor != null and request.executor != ''"> <if test="request.executionStatus != null and request.executionStatus != ''">
and t.user_id = #{request.executor} and t.status = #{request.executionStatus}
</if> </if>
and t.status in ("running","starting","waiting") <if test="request.executor != null and request.executor != ''">
) and t.user_id = #{request.executor}
UNION ALL </if>
(select t.id,'API' as executionModule, t.report_type, ifnull(t2.name,'LOCAL') as actuator, t1.`name` as executor,t.create_time as executionTime, ifnull(t.trigger_mode,'MANUAL') as triggerMode ,ifnull(t.status,'Saved') as executionStatus and t.status in ("running","starting","waiting")
from api_definition_exec_result t left join `user` t1 ON t.user_id = t1.id left join test_resource_pool t2 on t.actuator = t2.id )
where to_days(FROM_UNIXTIME(t.create_time/1000))= to_days(now()) and t.project_id in UNION ALL
<foreach collection="request.projects" item="id" separator="," open="(" close=")"> (select t.id,'API' as executionModule, t.report_type, ifnull(t2.name,'LOCAL') as actuator, t1.`name` as
#{id} executor,t.create_time as executionTime, ifnull(t.trigger_mode,'MANUAL') as triggerMode
</foreach> ,ifnull(t.status,'Saved') as executionStatus
from api_definition_exec_result t left join `user` t1 ON t.user_id = t1.id left join test_resource_pool t2 on
t.actuator = t2.id
where to_days(FROM_UNIXTIME(t.create_time/1000))= to_days(now()) and t.project_id in
<foreach collection="request.projects" item="id" separator="," open="(" close=")">
#{id}
</foreach>
<if test="request.triggerMode != null and request.triggerMode != ''"> <if test="request.triggerMode != null and request.triggerMode != ''">
and t.trigger_mode = #{request.triggerMode} and t.trigger_mode = #{request.triggerMode}
</if> </if>
<if test="request.executionStatus != null and request.executionStatus != ''"> <if test="request.executionStatus != null and request.executionStatus != ''">
and t.status = #{request.executionStatus} and t.status = #{request.executionStatus}
</if> </if>
<if test="request.executor != null and request.executor != ''"> <if test="request.executor != null and request.executor != ''">
and t.user_id = #{request.executor} and t.user_id = #{request.executor}
</if> </if>
and (t.integrated_report_id is null or t.integrated_report_id = 'null') and (t.integrated_report_id is null or t.integrated_report_id = 'null')
and t.status in ("running","starting","waiting") and t.status in ("running","starting","waiting")
) )
UNION ALL UNION ALL
(select t.id,'PERFORMANCE' as executionModule,'PERFORMANCE' as report_type, ifnull(t2.name,'LOCAL') as actuator, t1.`name` as executor,t.create_time as executionTime, t.trigger_mode as triggerMode ,t.`status` as executionStatus (select t.id,'PERFORMANCE' as executionModule,'PERFORMANCE' as report_type, ifnull(t2.name,'LOCAL') as actuator,
from load_test_report t left join `user` t1 ON t.user_id = t1.id left join test_resource_pool t2 on t.test_resource_pool_id = t2.id t1.`name` as executor,t.create_time as executionTime, t.trigger_mode as triggerMode ,t.`status` as
where to_days(FROM_UNIXTIME(t.create_time/1000))= to_days(now()) and t.project_id in executionStatus
<foreach collection="request.projects" item="id" separator="," open="(" close=")"> from load_test_report t left join `user` t1 ON t.user_id = t1.id left join test_resource_pool t2 on
#{id} t.test_resource_pool_id = t2.id
</foreach> where to_days(FROM_UNIXTIME(t.create_time/1000))= to_days(now()) and t.project_id in
<if test="request.triggerMode != null and request.triggerMode != ''"> <foreach collection="request.projects" item="id" separator="," open="(" close=")">
and t.trigger_mode = #{request.triggerMode} #{id}
</if> </foreach>
<if test="request.executionStatus != null and request.executionStatus != ''"> <if test="request.triggerMode != null and request.triggerMode != ''">
and t.status = #{request.executionStatus} and t.trigger_mode = #{request.triggerMode}
</if> </if>
<if test="request.executor != null and request.executor != ''"> <if test="request.executionStatus != null and request.executionStatus != ''">
and t.user_id = #{request.executor} and t.status = #{request.executionStatus}
</if> </if>
and t.status in ("running","starting","waiting") <if test="request.executor != null and request.executor != ''">
) and t.user_id = #{request.executor}
</if>
and t.status in ("running","starting","waiting")
)
)tt; )tt;
</select> </select>
@ -155,4 +174,21 @@
actuator = #{actuator} actuator = #{actuator}
AND `status` in ('Running','Waiting') AND `status` in ('Running','Waiting')
</select> </select>
<update id="stopScenario">
update api_scenario_report set status ='STOP' where
project_id in
<foreach collection="request.projects" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</update>
<update id="stopApi">
update api_definition_exec_result set status ='STOP' where
project_id in
<foreach collection="request.projects" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</update>
</mapper> </mapper>

View File

@ -5,6 +5,7 @@ import io.metersphere.api.dto.automation.TaskRequest;
import io.metersphere.api.exec.queue.ExecThreadPoolExecutor; import io.metersphere.api.exec.queue.ExecThreadPoolExecutor;
import io.metersphere.api.exec.queue.PoolExecBlockingQueueUtil; import io.metersphere.api.exec.queue.PoolExecBlockingQueueUtil;
import io.metersphere.api.jmeter.JMeterService; import io.metersphere.api.jmeter.JMeterService;
import io.metersphere.api.jmeter.JmeterThreadUtils;
import io.metersphere.api.service.ApiExecutionQueueService; import io.metersphere.api.service.ApiExecutionQueueService;
import io.metersphere.base.domain.*; import io.metersphere.base.domain.*;
import io.metersphere.base.mapper.ApiDefinitionExecResultMapper; import io.metersphere.base.mapper.ApiDefinitionExecResultMapper;
@ -16,6 +17,7 @@ import io.metersphere.base.mapper.ext.ExtApiScenarioReportMapper;
import io.metersphere.base.mapper.ext.ExtLoadTestReportMapper; import io.metersphere.base.mapper.ext.ExtLoadTestReportMapper;
import io.metersphere.base.mapper.ext.ExtTaskMapper; import io.metersphere.base.mapper.ext.ExtTaskMapper;
import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.LogUtil;
import io.metersphere.commons.utils.SessionUtils;
import io.metersphere.dto.NodeDTO; import io.metersphere.dto.NodeDTO;
import io.metersphere.jmeter.LocalRunner; import io.metersphere.jmeter.LocalRunner;
import io.metersphere.performance.service.PerformanceTestService; import io.metersphere.performance.service.PerformanceTestService;
@ -125,73 +127,89 @@ public class TaskService {
} }
} }
public String stop(List<TaskRequest> reportIds) { public String stop(List<TaskRequest> taskRequests) {
if (CollectionUtils.isNotEmpty(reportIds)) { if (CollectionUtils.isNotEmpty(taskRequests)) {
List<TaskRequest> stopTasks = taskRequests.stream().filter(s -> StringUtils.isNotEmpty(s.getReportId())).collect(Collectors.toList());
// 聚类同一批资源池的一批发送 // 聚类同一批资源池的一批发送
Map<String, List<String>> poolMap = new HashMap<>(); Map<String, List<String>> poolMap = new HashMap<>();
for (TaskRequest request : reportIds) { // 单条停止
String actuator = null; if (CollectionUtils.isNotEmpty(stopTasks) && stopTasks.size() == 1) {
if (StringUtils.isNotEmpty(request.getReportId())) { // 从队列移除
// 从队列移除 TaskRequest request = stopTasks.get(0);
execThreadPoolExecutor.removeQueue(request.getReportId()); execThreadPoolExecutor.removeQueue(request.getReportId());
apiExecutionQueueService.stop(request.getReportId()); apiExecutionQueueService.stop(request.getReportId());
PoolExecBlockingQueueUtil.offer(request.getReportId()); PoolExecBlockingQueueUtil.offer(request.getReportId());
if (StringUtils.equals(request.getType(), "API")) { if (StringUtils.equals(request.getType(), "API")) {
ApiDefinitionExecResult result = apiDefinitionExecResultMapper.selectByPrimaryKey(request.getReportId()); ApiDefinitionExecResult result = apiDefinitionExecResultMapper.selectByPrimaryKey(request.getReportId());
if (result != null) { if (result != null) {
result.setStatus("STOP"); result.setStatus("STOP");
apiDefinitionExecResultMapper.updateByPrimaryKeySelective(result); apiDefinitionExecResultMapper.updateByPrimaryKeySelective(result);
actuator = result.getActuator(); extracted(poolMap, request.getReportId(), result.getActuator());
}
} else if (StringUtils.equals(request.getType(), "SCENARIO")) {
ApiScenarioReport report = apiScenarioReportMapper.selectByPrimaryKey(request.getReportId());
if (report != null) {
report.setStatus("STOP");
apiScenarioReportMapper.updateByPrimaryKeySelective(report);
actuator = report.getActuator();
}
} else if (StringUtils.equals(request.getType(), "PERFORMANCE")) {
performanceTestService.stopTest(request.getReportId(), false);
} }
extracted(poolMap, request, actuator); }
} else { if (StringUtils.equals(request.getType(), "SCENARIO")) {
if (StringUtils.equals(request.getType(), "API")) { ApiScenarioReport report = apiScenarioReportMapper.selectByPrimaryKey(request.getReportId());
List<ApiDefinitionExecResult> result = extApiDefinitionExecResultMapper.selectApiResultByProjectId(request.getProjectId()); if (report != null) {
if (CollectionUtils.isNotEmpty(result)) { report.setStatus("STOP");
for (ApiDefinitionExecResult item : result) { apiScenarioReportMapper.updateByPrimaryKeySelective(report);
item.setStatus("STOP"); extracted(poolMap, request.getReportId(), report.getActuator());
apiDefinitionExecResultMapper.updateByPrimaryKeySelective(item); }
actuator = item.getActuator(); }
request.setReportId(item.getId()); if (StringUtils.equals(request.getType(), "PERFORMANCE")) {
extracted(poolMap, request, actuator); performanceTestService.stopTest(request.getReportId(), false);
}
} else {
try {
// 全部停止
Map<String, TaskRequest> taskRequestMap = taskRequests.stream().collect(Collectors.toMap(TaskRequest::getType, taskRequest -> taskRequest));
// 获取工作空间项目
TaskCenterRequest taskCenterRequest = new TaskCenterRequest();
taskCenterRequest.setProjects(this.getOwnerProjectIds(SessionUtils.getUserId()));
// 结束掉未分发完成的任务
JmeterThreadUtils.stop("PLAN-CASE");
JmeterThreadUtils.stop("API-CASE-RUN");
JmeterThreadUtils.stop("SCENARIO-PARALLEL-THREAD");
if (taskRequestMap.containsKey("API")) {
List<ApiDefinitionExecResult> results = extApiDefinitionExecResultMapper.findByProjectIds(taskCenterRequest);
if (CollectionUtils.isNotEmpty(results)) {
for (ApiDefinitionExecResult item : results) {
extracted(poolMap, item.getId(), item.getActuator());
// 从队列移除 // 从队列移除
execThreadPoolExecutor.removeQueue(item.getId()); execThreadPoolExecutor.removeQueue(item.getId());
apiExecutionQueueService.stop(item.getId());
PoolExecBlockingQueueUtil.offer(item.getId()); PoolExecBlockingQueueUtil.offer(item.getId());
} }
} }
} else if (StringUtils.equals(request.getType(), "SCENARIO")) { extTaskMapper.stopApi(taskCenterRequest);
List<ApiScenarioReport> reports = extApiScenarioReportMapper.selectReportByProjectId(request.getProjectId()); // 清理队列并停止测试计划报告
List<String> ids = results.stream().map(ApiDefinitionExecResult::getId).collect(Collectors.toList());
apiExecutionQueueService.stop(ids);
}
if (taskRequestMap.containsKey("SCENARIO")) {
List<ApiScenarioReport> reports = extApiScenarioReportMapper.findByProjectIds(taskCenterRequest);
if (CollectionUtils.isNotEmpty(reports)) { if (CollectionUtils.isNotEmpty(reports)) {
for (ApiScenarioReport report : reports) { for (ApiScenarioReport report : reports) {
report.setStatus("STOP");
apiScenarioReportMapper.updateByPrimaryKeySelective(report); extracted(poolMap, report.getId(), report.getActuator());
actuator = report.getActuator();
request.setReportId(report.getId());
extracted(poolMap, request, actuator);
// 从队列移除 // 从队列移除
execThreadPoolExecutor.removeQueue(report.getId()); execThreadPoolExecutor.removeQueue(report.getId());
apiExecutionQueueService.stop(report.getId());
PoolExecBlockingQueueUtil.offer(report.getId()); PoolExecBlockingQueueUtil.offer(report.getId());
} }
// 清理队列并停止测试计划报告
List<String> ids = reports.stream().map(ApiScenarioReport::getId).collect(Collectors.toList());
extTaskMapper.stopScenario(taskCenterRequest);
// 清理队列并停止测试计划报告
apiExecutionQueueService.stop(ids);
} }
} else if (StringUtils.equals(request.getType(), "PERFORMANCE")) { }
List<LoadTestReport> loadTestReports = extLoadTestReportMapper.selectReportByProjectId(request.getProjectId()); if (taskRequestMap.containsKey("PERFORMANCE")) {
List<LoadTestReport> loadTestReports = extLoadTestReportMapper.selectReportByProjectId(taskRequestMap.get("PERFORMANCE").getProjectId());
if (CollectionUtils.isNotEmpty(loadTestReports)) { if (CollectionUtils.isNotEmpty(loadTestReports)) {
for (LoadTestReport loadTestReport : loadTestReports) { for (LoadTestReport loadTestReport : loadTestReports) {
performanceTestService.stopTest(loadTestReport.getId(), false); performanceTestService.stopTest(loadTestReport.getId(), false);
request.setReportId(loadTestReport.getId());
extracted(poolMap, request, actuator);
// 从队列移除 // 从队列移除
execThreadPoolExecutor.removeQueue(loadTestReport.getId()); execThreadPoolExecutor.removeQueue(loadTestReport.getId());
apiExecutionQueueService.stop(loadTestReport.getId()); apiExecutionQueueService.stop(loadTestReport.getId());
@ -199,26 +217,32 @@ public class TaskService {
} }
} }
} }
} catch (Exception e) {
LogUtil.error(e);
} }
if (!poolMap.isEmpty()) { }
this.send(poolMap); if (!poolMap.isEmpty()) {
} this.send(poolMap);
} }
} }
return "SUCCESS"; return "SUCCESS";
} }
private void extracted(Map<String, List<String>> poolMap, TaskRequest request, String actuator) { private void extracted(Map<String, List<String>> poolMap, String reportId, String actuator) {
if (StringUtils.isEmpty(reportId)) {
return;
}
if (StringUtils.isNotEmpty(actuator) && !StringUtils.equals(actuator, "LOCAL")) { if (StringUtils.isNotEmpty(actuator) && !StringUtils.equals(actuator, "LOCAL")) {
if (poolMap.containsKey(actuator)) { if (poolMap.containsKey(actuator)) {
poolMap.get(actuator).add(request.getReportId()); poolMap.get(actuator).add(reportId);
} else { } else {
poolMap.put(actuator, new ArrayList<String>() {{ poolMap.put(actuator, new ArrayList<String>() {{
this.add(request.getReportId()); this.add(reportId);
}}); }});
} }
} else { } else {
new LocalRunner().stop(request.getReportId()); new LocalRunner().stop(reportId);
JmeterThreadUtils.stop(reportId);
} }
} }
} }