From cf9507da4be0a88231cb7c3b7d97723b734d47c1 Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Tue, 3 Aug 2021 19:20:35 +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=E9=87=8D=E6=9E=84,=20?= =?UTF-8?q?=E7=BB=9F=E4=B8=80=E8=81=9A=E5=90=88=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/PerformanceTestService.java | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) 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 1dac9534dd..713569d722 100644 --- a/backend/src/main/java/io/metersphere/performance/service/PerformanceTestService.java +++ b/backend/src/main/java/io/metersphere/performance/service/PerformanceTestService.java @@ -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 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 -1; // 表示计算报告自己决定 + return granularity; } }