refactor(性能测试): 更改查询步长

This commit is contained in:
shiziyuan9527 2021-04-15 11:55:04 +08:00 committed by 刘瑞斌
parent 3d2bb18b8c
commit f7bd3d443d
2 changed files with 58 additions and 1 deletions

View File

@ -0,0 +1,39 @@
package io.metersphere.performance.base;
import lombok.Getter;
import lombok.Setter;
import java.util.ArrayList;
import java.util.List;
public class GranularityData {
public static List<Data> dataList = new ArrayList<>();
static {
dataList.add(new Data(0, 100, 1));
dataList.add(new Data(101, 500, 5));
dataList.add(new Data(501, 1000, 10));
dataList.add(new Data(1001, 3000, 30));
dataList.add(new Data(3001, 6000, 60));
dataList.add(new Data(6001, 30000, 300));
dataList.add(new Data(30001, 60000, 600));
dataList.add(new Data(60001, 180000, 1800));
dataList.add(new Data(180001, 360000, 3600));
dataList.add(new Data(360000, Integer.MAX_VALUE, 3600));
}
@Getter
@Setter
public static class Data {
private Integer start;
private Integer end;
private Integer granularity;
Data(Integer start, Integer end, Integer granularity) {
this.start = start;
this.end = end;
this.granularity = granularity;
}
}
}

View File

@ -15,6 +15,7 @@ import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.DateUtils;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.dto.NodeDTO;
import io.metersphere.performance.base.GranularityData;
import io.metersphere.performance.base.ReportTimeInfo;
import io.metersphere.performance.controller.request.MetricDataRequest;
import io.metersphere.performance.controller.request.MetricQuery;
@ -57,13 +58,13 @@ public class MetricQueryService {
List<MetricData> metricDataList = new ArrayList<>();
long endTime = metricRequest.getEndTime();
long startTime = metricRequest.getStartTime();
int step = metricRequest.getStep();
long reliableEndTime;
if (endTime > System.currentTimeMillis()) {
reliableEndTime = System.currentTimeMillis();
} else {
reliableEndTime = endTime;
}
int step = getGranularity(startTime, reliableEndTime);
Optional.ofNullable(metricRequest.getMetricDataQueries()).ifPresent(metricDataQueries -> metricDataQueries.forEach(query -> {
String promQL = query.getPromQL();
@ -256,4 +257,21 @@ public class MetricQueryService {
return result;
}
private Integer getGranularity(long start, long end) {
Integer granularity = 15;
try {
int duration = (int) (end - start) / 1000;
Optional<GranularityData.Data> dataOptional = GranularityData.dataList.stream()
.filter(data -> duration >= data.getStart() && duration <= data.getEnd())
.findFirst();
if (dataOptional.isPresent()) {
GranularityData.Data data = dataOptional.get();
granularity = data.getGranularity();
}
} catch (Exception e) {
LogUtil.error(e.getMessage() ,e);
}
return granularity;
}
}