报告-测试概览

This commit is contained in:
shiziyuan9527 2020-04-22 17:16:08 +08:00
parent 653460fd8e
commit be78fc5939
1 changed files with 26 additions and 68 deletions

View File

@ -5,7 +5,6 @@ import com.opencsv.bean.CsvToBeanBuilder;
import com.opencsv.bean.HeaderColumnNameMappingStrategy;
import io.metersphere.report.base.*;
import io.metersphere.report.parse.ResultDataParse;
import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.report.processor.ErrorsSummaryConsumer;
import org.apache.jmeter.report.processor.StatisticsSummaryConsumer;
import org.apache.jmeter.report.processor.Top5ErrorsBySamplerConsumer;
@ -14,6 +13,7 @@ import org.apache.jmeter.report.processor.graph.impl.HitsPerSecondGraphConsumer;
import org.apache.jmeter.report.processor.graph.impl.ResponseTimeOverTimeGraphConsumer;
import java.io.Reader;
import java.io.StringReader;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.time.Duration;
import java.time.Instant;
@ -21,12 +21,9 @@ import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
public class GenerateReport {
private static final String DATE_TIME_PATTERN = "yyyy/MM/dd HH:mm:ss";
private static List<Metric> resolver(String jtlString) {
HeaderColumnNameMappingStrategy<Metric> ms = new HeaderColumnNameMappingStrategy<>();
ms.setType(Metric.class);
@ -60,71 +57,38 @@ public class GenerateReport {
}
public static TestOverview getTestOverview(String jtlString) {
TestOverview testOverview = new TestOverview();
DecimalFormat decimalFormat = new DecimalFormat("0.00");
List<Metric> totalLineList = GenerateReport.resolver(jtlString);
// todo 修改测试概览的数值
List<Metric> totalLineList2 = GenerateReport.resolver(jtlString);
// 时间戳转时间
for (Metric metric : totalLineList2) {
metric.setTimestamp(stampToDate(DATE_TIME_PATTERN, metric.getTimestamp()));
}
Map<String, Object> activeDataMap = ResultDataParse.getGraphDataMap(jtlString, new ActiveThreadsGraphConsumer());
List<ChartsData> usersList = ResultDataParse.graphMapParsing(activeDataMap, "users");
Optional<ChartsData> max = usersList.stream().max(Comparator.comparing(ChartsData::getyAxis));
int maxUser = max.get().getyAxis().setScale(0, BigDecimal.ROUND_UP).intValue();
Map<String, List<Metric>> collect2 = Objects.requireNonNull(totalLineList2).stream().collect(Collectors.groupingBy(Metric::getTimestamp));
List<Map.Entry<String, List<Metric>>> entries = new ArrayList<>(collect2.entrySet());
int maxUsers = 0;
for (Map.Entry<String, List<Metric>> entry : entries) {
List<Metric> metrics = entry.getValue();
Map<String, List<Metric>> metricsMap = metrics.stream().collect(Collectors.groupingBy(Metric::getThreadName));
if (metricsMap.size() > maxUsers) {
maxUsers = metricsMap.size();
}
}
Map<String, Object> hitsDataMap = ResultDataParse.getGraphDataMap(jtlString, new HitsPerSecondGraphConsumer());
List<ChartsData> hitsList = ResultDataParse.graphMapParsing(hitsDataMap, "hits");
double hits = hitsList.stream().map(ChartsData::getyAxis)
.mapToDouble(BigDecimal::doubleValue)
.average().orElse(0);
Map<String, List<Metric>> collect = totalLineList.stream().collect(Collectors.groupingBy(Metric::getTimestamp));
Iterator<Map.Entry<String, List<Metric>>> iterator = collect.entrySet().iterator();
Map<String, Object> errorDataMap = ResultDataParse.getSummryDataMap(jtlString, new StatisticsSummaryConsumer());
List<Statistics> statisticsList = ResultDataParse.summaryMapParsing(errorDataMap, Statistics.class);
Optional<Double> error = statisticsList.stream().map(item -> Double.parseDouble(item.getError())).reduce(Double::sum);
int totalElapsed = 0;
float totalBytes = 0f;
Map<String, Object> responseDataMap = ResultDataParse.getGraphDataMap(jtlString, new ResponseTimeOverTimeGraphConsumer());
List<ChartsData> responseDataList = ResultDataParse.graphMapParsing(responseDataMap, "response");
double responseTime = responseDataList.stream().map(ChartsData::getyAxis)
.mapToDouble(BigDecimal::doubleValue)
.average().orElse(0);
while (iterator.hasNext()) {
Map.Entry<String, List<Metric>> entry = iterator.next();
List<Metric> metricList = entry.getValue();
for (Metric metric : metricList) {
String elapsed = metric.getElapsed();
totalElapsed += Integer.parseInt(elapsed);
String bytes = metric.getBytes();
totalBytes += Float.parseFloat(bytes);
}
}
totalLineList.sort(Comparator.comparing(t0 -> Long.valueOf(t0.getTimestamp())));
testOverview.setMaxUsers(String.valueOf(maxUsers));
List<Metric> list90 = totalLineList.subList(0, totalLineList.size() * 9 / 10);
long sum = list90.stream().mapToLong(metric -> Long.parseLong(metric.getElapsed())).sum();
double avg90 = (double) sum / 1000 / list90.size();
testOverview.setResponseTime90(decimalFormat.format(avg90));
long timesStampStart = Long.parseLong(totalLineList.get(0).getTimestamp());
long timesStampEnd = Long.parseLong(totalLineList.get(totalLineList.size() - 1).getTimestamp());
long time = timesStampEnd - timesStampStart + Long.parseLong(totalLineList.get(totalLineList.size() - 1).getElapsed());
double avgThroughput = (double) totalLineList.size() / (time * 1.0 / 1000);
testOverview.setAvgThroughput(decimalFormat.format(avgThroughput));
List<Metric> falseList = totalLineList.stream().filter(metric -> StringUtils.equals("false", metric.getSuccess())).collect(Collectors.toList());
double errors = falseList.size() * 1.0 / totalLineList.size() * 100;
testOverview.setErrors(decimalFormat.format(errors));
double avg = totalElapsed * 1.0 / totalLineList.size() / 1000;
testOverview.setAvgResponseTime(decimalFormat.format(avg));
double bandwidth = totalBytes * 1.0 / time;
testOverview.setAvgBandwidth(decimalFormat.format(bandwidth));
TestOverview testOverview = new TestOverview();
testOverview.setMaxUsers(String.valueOf(maxUser));
testOverview.setAvgThroughput(decimalFormat.format(hits));
testOverview.setErrors(decimalFormat.format(error.get()));
testOverview.setAvgResponseTime(decimalFormat.format(responseTime / 1000));
// todo
testOverview.setResponseTime90("0");
testOverview.setAvgBandwidth("0");
return testOverview;
}
@ -168,10 +132,4 @@ public class GenerateReport {
return reportTimeInfo;
}
private static String stampToDate(String pattern, String timeStamp) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(Long.parseLong(timeStamp)), ZoneId.systemDefault());
return localDateTime.format(dateTimeFormatter);
}
}