refactor(性能测试): 性能测试启动增加一个ENV

This commit is contained in:
Captain.B 2021-07-30 15:19:42 +08:00 committed by 刘瑞斌
parent f06f4290c9
commit 737f75eb7b
4 changed files with 55 additions and 1 deletions

View File

@ -18,4 +18,11 @@ public class JmeterProperties {
private String heap = "-Xms1g -Xmx1g -XX:MaxMetaspaceSize=256m";
private String gcAlgo = "-XX:+UseG1GC -XX:MaxGCPauseMillis=100 -XX:G1ReservePercent=20";
private Report report = new Report();
@Getter
@Setter
public static class Report {
private Integer granularity = 60000;
}
}

View File

@ -94,6 +94,7 @@ public class DockerTestEngine extends AbstractEngine {
env.put("THREAD_NUM", "0");// 传入0表示不用修改线程数
env.put("HEAP", HEAP);
env.put("GC_ALGO", GC_ALGO);
env.put("GRANULARITY", performanceTestService.getGranularity(this.getReportId()).toString());
StartTestRequest startTestRequest = new StartTestRequest();

View File

@ -1,6 +1,8 @@
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;
@ -14,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;
@ -27,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;
@ -56,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
@ -649,6 +654,7 @@ public class PerformanceTestService {
/**
* 一键更新由接口用例或者场景用例转换的性能测试
*
* @param request
*/
public void syncApi(EditTestPlanRequest request) {
@ -780,4 +786,44 @@ public class PerformanceTestService {
mapper.insert(scenarioLoadTest);
});
}
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());
JSONObject advancedConfig = JSON.parseObject(loadTest.getAdvancedConfiguration());
if (advancedConfig.getInteger("granularity") != null) {
granularity = advancedConfig.getInteger("granularity");
return 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 granularity;
}
}

@ -1 +1 @@
Subproject commit 687f07db7831404b450bc2bba16ebd4cc54a6116
Subproject commit 5b06224daf6b4232613a96c58d03a7f6831f4789