refactor(性能测试): 性能测试重构, 统一聚合时间

This commit is contained in:
Captain.B 2021-08-03 19:20:35 +08:00 committed by 刘瑞斌
parent 0659334ca2
commit cf9507da4b
1 changed files with 30 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package io.metersphere.performance.service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import io.metersphere.api.dto.JmxInfoDTO;
import io.metersphere.api.dto.automation.ApiScenarioBatchRequest;
import io.metersphere.api.dto.automation.ApiScenrioExportJmx;
@ -15,6 +16,7 @@ import io.metersphere.base.mapper.ext.ExtLoadTestReportMapper;
import io.metersphere.commons.constants.*;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.*;
import io.metersphere.config.JmeterProperties;
import io.metersphere.config.KafkaProperties;
import io.metersphere.controller.request.OrderRequest;
import io.metersphere.controller.request.QueryScheduleRequest;
@ -28,6 +30,7 @@ import io.metersphere.log.utils.ReflexObjectUtil;
import io.metersphere.log.vo.DetailColumn;
import io.metersphere.log.vo.OperatingLogDetails;
import io.metersphere.log.vo.performance.PerformanceReference;
import io.metersphere.performance.base.GranularityData;
import io.metersphere.performance.dto.LoadTestExportJmx;
import io.metersphere.performance.engine.Engine;
import io.metersphere.performance.engine.EngineFactory;
@ -57,6 +60,7 @@ import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
@Service
@ -785,6 +789,7 @@ public class PerformanceTestService {
}
public Integer getGranularity(String reportId) {
Integer granularity = CommonBeanFactory.getBean(JmeterProperties.class).getReport().getGranularity();
try {
LoadTestReportWithBLOBs report = loadTestReportMapper.selectByPrimaryKey(reportId);
LoadTestWithBLOBs loadTest = loadTestMapper.selectByPrimaryKey(report.getTestId());
@ -792,9 +797,33 @@ public class PerformanceTestService {
if (advancedConfig.getInteger("granularity") != null) {
return advancedConfig.getInteger("granularity") * 1000;// 单位是ms
}
AtomicReference<Integer> maxDuration = new AtomicReference<>(0);
List<List<JSONObject>> pressureConfigLists = JSON.parseObject(loadTest.getLoadConfiguration(), new TypeReference<List<List<JSONObject>>>() {
});
// 按照最长的执行时间来确定
pressureConfigLists.forEach(pcList -> {
Optional<Integer> maxOp = pcList.stream()
.filter(pressureConfig -> StringUtils.equalsAnyIgnoreCase(pressureConfig.getString("key"), "hold", "duration"))
.map(pressureConfig -> pressureConfig.getInteger("value"))
.max(Comparator.naturalOrder());
Integer max = maxOp.orElse(0);
if (maxDuration.get() < max) {
maxDuration.set(max);
}
});
Optional<GranularityData.Data> dataOptional = GranularityData.dataList.stream()
.filter(data -> maxDuration.get() >= data.getStart() && maxDuration.get() <= data.getEnd())
.findFirst();
if (dataOptional.isPresent()) {
GranularityData.Data data = dataOptional.get();
granularity = data.getGranularity();
}
} catch (Exception e) {
LogUtil.error(e);
}
return -1; // 表示计算报告自己决定
return granularity;
}
}