性能测试增加执行人

This commit is contained in:
liangdaliang
2025-06-17 10:05:48 +08:00
parent efa41a7217
commit 49d63847ee
7 changed files with 26 additions and 23 deletions

View File

@@ -120,6 +120,7 @@ public class PerformanceTestController extends BaseController {
@PostMapping("/addAndExecute")
public AjaxResult addAndExecute(@RequestBody PerformanceTestQO performanceTestQO) {
try {
String createBy = getLoginUser().getUsername();
Long l = performanceTestService.insertPerformanceTest(performanceTestQO);
// 获取新增的任务完整信息
PerformanceTestVO newTaskVo = performanceTestService.selectPerformanceTestById(l);
@@ -135,11 +136,9 @@ public class PerformanceTestController extends BaseController {
// 执行性能测试
try {
CompletableFuture.runAsync(() -> {
performanceTestCaseReportService.executePerformanceTestAndReport(l, jmeterHomePath, 2);
performanceTestCaseReportService.executePerformanceTestAndReport(l, jmeterHomePath, 2, createBy);
});
return toAjax(true);
// Long l1 = performanceTestCaseReportService.executePerformanceTestAndReport(l, jmeterHomePath, 2);
// return success(l1);
} catch (Exception e) {
log.error("执行失败!", e);
return error("执行失败!");
@@ -184,6 +183,7 @@ public class PerformanceTestController extends BaseController {
@Log(title = "性能测试", businessType = BusinessType.UPDATE)
@PutMapping("/editAndExecute")
public AjaxResult editAndExecute(@RequestBody PerformanceTestQO performanceTestQO) {
String createBy = getLoginUser().getUsername();
try {
PerformanceTestVO originalTask = performanceTestService.selectPerformanceTestById(performanceTestQO.getId());
Long l = performanceTestService.updatePerformanceTest(performanceTestQO);
@@ -200,11 +200,9 @@ public class PerformanceTestController extends BaseController {
}
try {
CompletableFuture.runAsync(() -> {
performanceTestCaseReportService.executePerformanceTestAndReport(l, jmeterHomePath, 2);
performanceTestCaseReportService.executePerformanceTestAndReport(l, jmeterHomePath, 2, createBy);
});
return toAjax(true);
// Long l1 = performanceTestCaseReportService.executePerformanceTestAndReport(l, jmeterHomePath, 2);
// return success(l1);
} catch (Exception e) {
log.error("执行失败!", e);
return error("执行失败!"+e);
@@ -232,11 +230,10 @@ public class PerformanceTestController extends BaseController {
// @PreAuthorize("@ss.hasPermi('system:test:remove')")
@GetMapping("/executeNow")
public AjaxResult executeNow(@RequestParam Long id) {
String createBy = getLoginUser().getUsername();
CompletableFuture.runAsync(() -> {
performanceTestCaseReportService.executePerformanceTestAndReport(id, jmeterHomePath, 2);
performanceTestCaseReportService.executePerformanceTestAndReport(id, jmeterHomePath, 2, createBy);
});
return toAjax(true);
// Long l1 = performanceTestCaseReportService.executePerformanceTestAndReport(id, jmeterHomePath, 2);
// return success(l1);
}
}

View File

@@ -97,6 +97,9 @@ public class TestTaskController extends BaseController {
try {
testTask.setCreateBy(getLoginUser().getUsername());
testTask.setCreateTime(DateUtils.getNowDate());
if (testTask.getStatus() == null) {
testTask.setStatus(2);
}
int result = testTaskService.insertTestTask(testTask);
// 如果任务状态是启用且没有被删除,则添加到定时任务管理器
@@ -106,6 +109,7 @@ public class TestTaskController extends BaseController {
return toAjax(result);
} catch (Exception e) {
logger.error("新增失败:", e);
return error("新增失败:" + e.getMessage());
}
}

View File

@@ -17,9 +17,10 @@ public interface IPerformanceTestCaseReportService
* @param id
* @param jmeterHomePath
* @param triggerType 触发方式1-定时任务2-手动
* @param createBy 创建人
* @return 返回本次性能测试的批次id
*/
public Long executePerformanceTestAndReport(Long id, String jmeterHomePath, Integer triggerType);
public Long executePerformanceTestAndReport(Long id, String jmeterHomePath, Integer triggerType, String createBy);
/**
* 查询性能测试用例报告

View File

@@ -51,7 +51,7 @@ public class PerformanceTestCaseReportServiceImpl implements IPerformanceTestCas
private ITestCaseStepService testCaseStepService;
@Override
public Long executePerformanceTestAndReport(Long id, String jmeterHomePath, Integer triggerType) {
public Long executePerformanceTestAndReport(Long id, String jmeterHomePath, Integer triggerType, String createBy) {
Long sid = System.currentTimeMillis();
PerformanceTest performanceTest = performanceTestMapper.selectPerformanceTestById(id);
PerformanceTestCase performanceTestCase = new PerformanceTestCase();
@@ -89,6 +89,7 @@ public class PerformanceTestCaseReportServiceImpl implements IPerformanceTestCas
if (!CollectionUtils.isEmpty(jmeterResultList)) {
LabelStatsEntity lastElement = jmeterResultList.get(jmeterResultList.size() - 1);
PerformanceTestCaseReport performanceTestCaseReport = new PerformanceTestCaseReport();
performanceTestCaseReport.setCreateBy(createBy);
performanceTestCaseReport.setPerformanceId(id);
performanceTestCaseReport.setTestCaseId(relateTestCase.getTestCaseId());
performanceTestCaseReport.setSid(sid);

View File

@@ -30,16 +30,16 @@ public class DynamicTaskManager {
@Resource
private PerformanceTestMapper performanceTestMapper;
@Resource
private TaskScheduler taskScheduler;
@Resource
private IPerformanceTestCaseReportService performanceTestCaseReportService;
private final Map<Long, ScheduledFuture<?>> scheduledTasks = new ConcurrentHashMap<>();
private final Map<Long, PerformanceTest> taskCache = new ConcurrentHashMap<>();
@PostConstruct
public void init() {
loadTasks(); // 初始加载
@@ -104,7 +104,7 @@ public class DynamicTaskManager {
List<PerformanceTest> tasks = performanceTestMapper.selectPerformanceTestList(performanceTest);
tasks.forEach(this::scheduleTask);
}
private void scheduleTask(PerformanceTest task) {
try {
CronTrigger trigger = new CronTrigger(task.getCrontab());
@@ -118,17 +118,17 @@ public class DynamicTaskManager {
log.error("无效的cron表达式: " + task.getCrontab());
}
}
private void executeTask(PerformanceTest task) {
// 执行业务逻辑
log.info("执行任务: " + task.getId());
try{
performanceTestCaseReportService.executePerformanceTestAndReport(task.getId(),jmeterHomePath,1);
performanceTestCaseReportService.executePerformanceTestAndReport(task.getId(),jmeterHomePath,1,"system");
} catch (Exception e) {
log.error("执行失败!",e);
}
}
private void refreshTasks() {
PerformanceTest performanceTest =new PerformanceTest();
performanceTest.setCrontabStatus(1);
@@ -137,7 +137,7 @@ public class DynamicTaskManager {
Set<Long> currentIds = currentTasks.stream()
.map(PerformanceTest::getId)
.collect(Collectors.toSet());
// 处理新增或更新的任务
currentTasks.forEach(task -> {
Long taskId = task.getId();
@@ -162,4 +162,4 @@ public class DynamicTaskManager {
}
});
}
}
}