测试概览ResponseTime图表接口

This commit is contained in:
shiziyuan9527 2020-04-03 18:36:10 +08:00
parent 1c4b50e760
commit 0b0348995e
3 changed files with 122 additions and 83 deletions

View File

@ -82,6 +82,11 @@ public class ReportController {
return reportService.getLoadChartData(reportId);
}
@GetMapping("/content/res_chart/{reportId}")
public ChartsData getResponseTimeChartData(@PathVariable String reportId) {
return reportService.getResponseTimeChartData(reportId);
}
@GetMapping("/{reportId}")
public LoadTestReport getLoadTestReport(@PathVariable String reportId) {
return reportService.getLoadTestReport(reportId);

View File

@ -8,6 +8,7 @@ import io.metersphere.report.base.*;
import io.metersphere.report.dto.ErrorsTop5DTO;
import io.metersphere.report.dto.RequestStatisticsDTO;
import org.apache.commons.lang3.StringUtils;
import java.io.Reader;
import java.io.StringReader;
import java.text.DecimalFormat;
@ -19,6 +20,7 @@ import java.util.stream.Collectors;
public class JtlResolver {
private static final Integer ERRORS_TOP_SIZE = 5;
private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static List<Metric> resolver(String jtlString) {
HeaderColumnNameMappingStrategy<Metric> ms = new HeaderColumnNameMappingStrategy<>();
@ -42,8 +44,8 @@ public class JtlResolver {
List<RequestStatistics> requestStatisticsList = new ArrayList<>();
DecimalFormat decimalFormat = new DecimalFormat("0.00");
List<Metric> totalLines = resolver(jtlString);
Map<String, List<Metric>> jtlLabelMap = totalLines.stream().collect(Collectors.groupingBy(Metric::getLabel));
List<Metric> totalMetricList = resolver(jtlString);
Map<String, List<Metric>> jtlLabelMap = totalMetricList.stream().collect(Collectors.groupingBy(Metric::getLabel));
Iterator<Map.Entry<String, List<Metric>>> iterator = jtlLabelMap.entrySet().iterator();
int totalElapsedTime = 0;
@ -152,11 +154,11 @@ public class JtlResolver {
statisticsDTO.setTotalTP95(String.valueOf(allElapseTimeList.get(totalTP95)));
statisticsDTO.setTotalTP99(String.valueOf(allElapseTimeList.get(totalTP99)));
totalLines.sort(Comparator.comparing(t0 -> Long.valueOf(t0.getTimestamp())));
totalMetricList.sort(Comparator.comparing(t0 -> Long.valueOf(t0.getTimestamp())));
long ms = Long.parseLong(totalLines.get(totalLines.size()-1).getTimestamp()) - Long.parseLong(totalLines.get(0).getTimestamp())
+ Long.parseLong(totalLines.get(totalLines.size()-1).getElapsed());
double avgThroughput = (double)totalLines.size() / (ms * 1.0 / 1000);
long ms = Long.parseLong(totalMetricList.get(totalMetricList.size() - 1).getTimestamp()) - Long.parseLong(totalMetricList.get(0).getTimestamp())
+ Long.parseLong(totalMetricList.get(totalMetricList.size() - 1).getElapsed());
double avgThroughput = (double) totalMetricList.size() / (ms * 1.0 / 1000);
statisticsDTO.setTotalAvgHits(decimalFormat.format(avgThroughput));
statisticsDTO.setTotalAvgBandwidth(decimalFormat.format(totalBytes * 1.0 / 1024 / (ms * 1.0 / 1000)));
@ -165,22 +167,20 @@ public class JtlResolver {
}
public static List<Errors> getErrorsList(String jtlString) {
List<Metric> totalLines = resolver(jtlString);
List<Metric> totalMetricList = resolver(jtlString);
List<Errors> errorsList = new ArrayList<>();
DecimalFormat decimalFormat = new DecimalFormat("0.00");
List<Metric> falseList = new ArrayList<>();
for (Metric metric : totalLines) {
for (Metric metric : totalMetricList) {
if (StringUtils.equals("false", metric.getSuccess())) {
falseList.add(metric);
}
}
Map<String, List<Metric>> jtlMap = falseList.stream().collect(Collectors.groupingBy(JtlResolver::getResponseCodeAndFailureMessage));
Iterator<Map.Entry<String, List<Metric>>> iterator = jtlMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, List<Metric>> next = iterator.next();
for (Map.Entry<String, List<Metric>> next : jtlMap.entrySet()) {
String key = next.getKey();
List<Metric> metricList = next.getValue();
Errors errors = new Errors();
@ -188,7 +188,7 @@ public class JtlResolver {
errors.setErrorNumber(String.valueOf(metricList.size()));
int errorSize = metricList.size();
int errorAllSize = falseList.size();
int allSamples = totalLines.size();
int allSamples = totalMetricList.size();
errors.setPrecentOfErrors(decimalFormat.format((double) errorSize / errorAllSize * 100) + "%");
errors.setPrecentOfAllSamples(decimalFormat.format((double) errorSize / allSamples * 100) + "%");
errorsList.add(errors);
@ -202,21 +202,26 @@ public class JtlResolver {
}
public static ErrorsTop5DTO getErrorsTop5DTO(String jtlString) {
List<Metric> totalLines = resolver(jtlString);
List<Metric> totalMetricList = resolver(jtlString);
ErrorsTop5DTO top5DTO = new ErrorsTop5DTO();
List<ErrorsTop5> errorsTop5s = new ArrayList<>();
List<Metric> falseList = Objects.requireNonNull(totalLines).stream().filter(metric -> StringUtils.equals("false", metric.getSuccess())).collect(Collectors.toList());
Map<String, List<Metric>> collect = falseList.stream().collect(Collectors.groupingBy(JtlResolver::getResponseCodeAndFailureMessage));
Iterator<Map.Entry<String, List<Metric>>> iterator = collect.entrySet().iterator();
List<Metric> falseList = Objects.requireNonNull(totalMetricList).stream()
.filter(metric -> StringUtils.equals("false", metric.getSuccess()))
.collect(Collectors.toList());
while (iterator.hasNext()) {
Map.Entry<String, List<Metric>> next = iterator.next();
Map<String, List<Metric>> collect = falseList.stream()
.collect(Collectors.groupingBy(JtlResolver::getResponseCodeAndFailureMessage));
for (Map.Entry<String, List<Metric>> next : collect.entrySet()) {
String key = next.getKey();
List<Metric> metricList = next.getValue();
List<Metric> list = totalLines.stream()
.filter(metric -> StringUtils.equals(metric.getLabel(), metricList.get(0).getLabel()))
.collect(Collectors.toList());
List<Metric> list = new ArrayList<>();
for (Metric metric : totalMetricList) {
if (StringUtils.equals(metric.getLabel(), metricList.get(0).getLabel())) {
list.add(metric);
}
}
ErrorsTop5 errorsTop5 = new ErrorsTop5();
errorsTop5.setSamples(String.valueOf(list.size()));
@ -235,7 +240,7 @@ public class JtlResolver {
top5DTO.setLabel("Total");
top5DTO.setErrorsTop5List(errorsTop5s);
top5DTO.setTotalSamples(String.valueOf(totalLines.size()));
top5DTO.setTotalSamples(String.valueOf(totalMetricList.size()));
top5DTO.setTotalErrors(String.valueOf(falseList.size()));
int size = errorsTop5s.size();
// Total行 信息
@ -272,8 +277,7 @@ public class JtlResolver {
maxUsers = metricList.size();
}
for (int i = 0; i < metricList.size(); i++) {
Metric metric = metricList.get(i);
for (Metric metric : metricList) {
String elapsed = metric.getElapsed();
totalElapsed += Integer.parseInt(elapsed);
String bytes = metric.getBytes();
@ -312,7 +316,7 @@ public class JtlResolver {
public static ChartsData getLoadChartData(String jtlString) {
ChartsData data = new ChartsData();
List<Metric> total = JtlResolver.resolver(jtlString);
List<Metric> totalMetricList = JtlResolver.resolver(jtlString);
List<String> users = new ArrayList<>();
List<String> hits = new ArrayList<>();
@ -320,45 +324,30 @@ public class JtlResolver {
List<String> timeList = new ArrayList<>();
Map<String, Object> resultMap = new HashMap<>(5);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DecimalFormat decimalFormat = new DecimalFormat("0.0");
total.sort(Comparator.comparing(metric -> Long.valueOf(metric.getTimestamp())));
total.forEach(metric -> {
if (totalMetricList != null) {
for (Metric metric : totalMetricList) {
metric.setTimestamp(stampToDate(metric.getTimestamp()));
});
Map<String, List<Metric>> collect = total.stream().collect(Collectors.groupingBy(Metric::getTimestamp));
}
}
Map<String, List<Metric>> collect = Objects.requireNonNull(totalMetricList).stream().collect(Collectors.groupingBy(Metric::getTimestamp));
List<Map.Entry<String, List<Metric>>> entries = new ArrayList<>(collect.entrySet());
Collections.sort(entries, new Comparator<Map.Entry<String, List<Metric>>>() {
@Override
public int compare(Map.Entry<String, List<Metric>> t1, Map.Entry<String, List<Metric>> t2) {
Date date1 = null,date2 = null;
try {
date1 = simpleDateFormat.parse(t1.getKey());
date2 = simpleDateFormat.parse(t2.getKey());
} catch (ParseException e) {
e.printStackTrace();
}
return (int) (date1.getTime() - date2.getTime());
}
});
entries.sort(JtlResolver::sortByDate);
for (int i = 0; i < entries.size(); i++) {
for (Map.Entry<String, List<Metric>> entry : entries) {
int failSize = 0;
Map.Entry<String, List<Metric>> map = entries.get(i);
List<Metric> metrics = map.getValue();
List<Metric> metrics = entry.getValue();
Map<String, List<Metric>> metricsMap = metrics.stream().collect(Collectors.groupingBy(Metric::getThreadName));
int maxUsers = metricsMap.size();
for (int j = 0; j < metrics.size(); j++) {
Metric metric = metrics.get(j);
String success = metric.getSuccess();
if (!"true".equals(success)){
for (Metric metric : metrics) {
String isSuccess = metric.getSuccess();
if (!"true".equals(isSuccess)) {
failSize++;
}
}
// todo
timeList.add(map.getKey());
timeList.add(entry.getKey());
hits.add(decimalFormat.format(metrics.size() * 1.0 / maxUsers));
users.add(String.valueOf(maxUsers));
errors.add(String.valueOf(failSize));
@ -376,10 +365,47 @@ public class JtlResolver {
return data;
}
public static ChartsData getResponseTimeChartData(String jtlString) {
ChartsData chartsData = new ChartsData();
List<Metric> totalMetricList = JtlResolver.resolver(jtlString);
totalMetricList.forEach(metric -> {
metric.setTimestamp(stampToDate(metric.getTimestamp()));
});
Map<String, List<Metric>> metricMap = totalMetricList.stream().collect(Collectors.groupingBy(Metric::getTimestamp));
List<Map.Entry<String, List<Metric>>> entries = new ArrayList<>(metricMap.entrySet());
entries.sort(JtlResolver::sortByDate);
List<String> resTimeList = new ArrayList<>();
List<String> timestampList = new ArrayList<>();
for (Map.Entry<String, List<Metric>> entry : entries) {
List<Metric> metricList = entry.getValue();
int sumElapsedTime = metricList.stream().mapToInt(metric -> Integer.parseInt(metric.getElapsed())).sum();
timestampList.add(entry.getKey());
resTimeList.add(String.valueOf(sumElapsedTime / metricList.size()));
}
chartsData.setxAxis(StringUtils.join(",", timestampList));
chartsData.setSerices(StringUtils.join(",", resTimeList));
return chartsData;
}
private static String stampToDate(String s) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long lt = Long.parseLong(s);
Date date = new Date(lt);
return simpleDateFormat.format(date);
}
private static int sortByDate(Map.Entry<String, List<Metric>> map1, Map.Entry<String, List<Metric>> map2) {
Date date1 = null, date2 = null;
try {
date1 = simpleDateFormat.parse(map1.getKey());
date2 = simpleDateFormat.parse(map2.getKey());
} catch (ParseException e) {
e.printStackTrace();
}
return (int) (Objects.requireNonNull(date1).getTime() - Objects.requireNonNull(date2).getTime());
}
}

View File

@ -92,6 +92,14 @@ public class ReportService {
return chartsData;
}
public ChartsData getResponseTimeChartData(String id) {
checkReportStatus(id);
LoadTestReportWithBLOBs loadTestReport = loadTestReportMapper.selectByPrimaryKey(id);
String content = loadTestReport.getContent();
ChartsData chartsData = JtlResolver.getResponseTimeChartData(content);
return chartsData;
}
public void checkReportStatus(String reportId) {
LoadTestReportWithBLOBs loadTestReport = loadTestReportMapper.selectByPrimaryKey(reportId);
String reportStatus = loadTestReport.getStatus();