From 737f75eb7b89f6de4747eb97b8d38b685253a5bb Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Fri, 30 Jul 2021 15:19:42 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=80=A7=E8=83=BD=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=20=E6=80=A7=E8=83=BD=E6=B5=8B=E8=AF=95=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=80=E4=B8=AAENV?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../metersphere/config/JmeterProperties.java | 7 +++ .../engine/docker/DockerTestEngine.java | 1 + .../service/PerformanceTestService.java | 46 +++++++++++++++++++ backend/src/main/java/io/metersphere/xpack | 2 +- 4 files changed, 55 insertions(+), 1 deletion(-) diff --git a/backend/src/main/java/io/metersphere/config/JmeterProperties.java b/backend/src/main/java/io/metersphere/config/JmeterProperties.java index 6d3e5d6cd3..11ef2c6ec3 100644 --- a/backend/src/main/java/io/metersphere/config/JmeterProperties.java +++ b/backend/src/main/java/io/metersphere/config/JmeterProperties.java @@ -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; + } } diff --git a/backend/src/main/java/io/metersphere/performance/engine/docker/DockerTestEngine.java b/backend/src/main/java/io/metersphere/performance/engine/docker/DockerTestEngine.java index b5a7a904c8..9ac6c02b5b 100644 --- a/backend/src/main/java/io/metersphere/performance/engine/docker/DockerTestEngine.java +++ b/backend/src/main/java/io/metersphere/performance/engine/docker/DockerTestEngine.java @@ -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(); diff --git a/backend/src/main/java/io/metersphere/performance/service/PerformanceTestService.java b/backend/src/main/java/io/metersphere/performance/service/PerformanceTestService.java index fa687c2264..2537df88ac 100644 --- a/backend/src/main/java/io/metersphere/performance/service/PerformanceTestService.java +++ b/backend/src/main/java/io/metersphere/performance/service/PerformanceTestService.java @@ -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 maxDuration = new AtomicReference<>(0); + List> pressureConfigLists = JSON.parseObject(loadTest.getLoadConfiguration(), new TypeReference>>() { + }); + // 按照最长的执行时间来确定 + pressureConfigLists.forEach(pcList -> { + + Optional 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 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; + } } diff --git a/backend/src/main/java/io/metersphere/xpack b/backend/src/main/java/io/metersphere/xpack index 687f07db78..5b06224daf 160000 --- a/backend/src/main/java/io/metersphere/xpack +++ b/backend/src/main/java/io/metersphere/xpack @@ -1 +1 @@ -Subproject commit 687f07db7831404b450bc2bba16ebd4cc54a6116 +Subproject commit 5b06224daf6b4232613a96c58d03a7f6831f4789