diff --git a/backend/src/main/java/io/metersphere/report/GenerateReport.java b/backend/src/main/java/io/metersphere/report/GenerateReport.java index 12d7f17686..9cd66713ab 100644 --- a/backend/src/main/java/io/metersphere/report/GenerateReport.java +++ b/backend/src/main/java/io/metersphere/report/GenerateReport.java @@ -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 resolver(String jtlString) { HeaderColumnNameMappingStrategy 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 totalLineList = GenerateReport.resolver(jtlString); - // todo 修改测试概览的数值 - List totalLineList2 = GenerateReport.resolver(jtlString); - // 时间戳转时间 - for (Metric metric : totalLineList2) { - metric.setTimestamp(stampToDate(DATE_TIME_PATTERN, metric.getTimestamp())); - } + Map activeDataMap = ResultDataParse.getGraphDataMap(jtlString, new ActiveThreadsGraphConsumer()); + List usersList = ResultDataParse.graphMapParsing(activeDataMap, "users"); + Optional max = usersList.stream().max(Comparator.comparing(ChartsData::getyAxis)); + int maxUser = max.get().getyAxis().setScale(0, BigDecimal.ROUND_UP).intValue(); - Map> collect2 = Objects.requireNonNull(totalLineList2).stream().collect(Collectors.groupingBy(Metric::getTimestamp)); - List>> entries = new ArrayList<>(collect2.entrySet()); - int maxUsers = 0; - for (Map.Entry> entry : entries) { - List metrics = entry.getValue(); - Map> metricsMap = metrics.stream().collect(Collectors.groupingBy(Metric::getThreadName)); - if (metricsMap.size() > maxUsers) { - maxUsers = metricsMap.size(); - } - } + Map hitsDataMap = ResultDataParse.getGraphDataMap(jtlString, new HitsPerSecondGraphConsumer()); + List hitsList = ResultDataParse.graphMapParsing(hitsDataMap, "hits"); + double hits = hitsList.stream().map(ChartsData::getyAxis) + .mapToDouble(BigDecimal::doubleValue) + .average().orElse(0); - Map> collect = totalLineList.stream().collect(Collectors.groupingBy(Metric::getTimestamp)); - Iterator>> iterator = collect.entrySet().iterator(); + Map errorDataMap = ResultDataParse.getSummryDataMap(jtlString, new StatisticsSummaryConsumer()); + List statisticsList = ResultDataParse.summaryMapParsing(errorDataMap, Statistics.class); + Optional error = statisticsList.stream().map(item -> Double.parseDouble(item.getError())).reduce(Double::sum); - int totalElapsed = 0; - float totalBytes = 0f; + Map responseDataMap = ResultDataParse.getGraphDataMap(jtlString, new ResponseTimeOverTimeGraphConsumer()); + List responseDataList = ResultDataParse.graphMapParsing(responseDataMap, "response"); + double responseTime = responseDataList.stream().map(ChartsData::getyAxis) + .mapToDouble(BigDecimal::doubleValue) + .average().orElse(0); - while (iterator.hasNext()) { - Map.Entry> entry = iterator.next(); - List 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 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 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); - } - } \ No newline at end of file