diff --git a/backend/src/main/java/io/metersphere/report/GenerateReport.java b/backend/src/main/java/io/metersphere/report/GenerateReport.java deleted file mode 100644 index 7d4bb44ec3..0000000000 --- a/backend/src/main/java/io/metersphere/report/GenerateReport.java +++ /dev/null @@ -1,135 +0,0 @@ -package io.metersphere.report; - -import com.opencsv.bean.CsvToBean; -import com.opencsv.bean.CsvToBeanBuilder; -import com.opencsv.bean.HeaderColumnNameMappingStrategy; -import io.metersphere.report.base.*; -import io.metersphere.report.parse.ResultDataParse; -import org.apache.jmeter.report.processor.ErrorsSummaryConsumer; -import org.apache.jmeter.report.processor.StatisticsSummaryConsumer; -import org.apache.jmeter.report.processor.Top5ErrorsBySamplerConsumer; -import org.apache.jmeter.report.processor.graph.impl.ActiveThreadsGraphConsumer; -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; -import java.time.LocalDateTime; -import java.time.ZoneId; -import java.time.format.DateTimeFormatter; -import java.util.*; - -public class GenerateReport { - - private static List resolver(String jtlString) { - HeaderColumnNameMappingStrategy ms = new HeaderColumnNameMappingStrategy<>(); - ms.setType(Metric.class); - try (Reader reader = new StringReader(jtlString)) { - CsvToBean cb = new CsvToBeanBuilder(reader) - .withType(Metric.class) - .withSkipLines(0) - .withMappingStrategy(ms) - .withIgnoreLeadingWhiteSpace(true) - .build(); - return cb.parse(); - } catch (Exception ex) { - ex.printStackTrace(); - } - return null; - } - - public static List getErrorsList(String jtlString) { - Map statisticsDataMap = ResultDataParse.getSummryDataMap(jtlString, new ErrorsSummaryConsumer()); - return ResultDataParse.summaryMapParsing(statisticsDataMap, Errors.class); - } - - public static List getErrorsTop5List(String jtlString) { - Map statisticsDataMap = ResultDataParse.getSummryDataMap(jtlString, new Top5ErrorsBySamplerConsumer()); - return ResultDataParse.summaryMapParsing(statisticsDataMap, ErrorsTop5.class); - } - - public static List getRequestStatistics(String jtlString) { - Map statisticsDataMap = ResultDataParse.getSummryDataMap(jtlString, new StatisticsSummaryConsumer()); - return ResultDataParse.summaryMapParsing(statisticsDataMap, Statistics.class); - } - - public static TestOverview getTestOverview(String jtlString) { - DecimalFormat decimalFormat = new DecimalFormat("0.00"); - - 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 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 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); - double avgTp90 = statisticsList.stream().map(item -> Double.parseDouble(item.getTp90())).mapToDouble(Double::doubleValue).average().orElse(0); - double avgBandwidth = statisticsList.stream().map(item -> Double.parseDouble(item.getReceived())).mapToDouble(Double::doubleValue).average().orElse(0); - - 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); - - 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)); - testOverview.setResponseTime90(decimalFormat.format(avgTp90 / 1000)); - testOverview.setAvgBandwidth(decimalFormat.format(avgBandwidth)); - return testOverview; - } - - public static List getLoadChartData(String jtlString) { - Map activeThreadMap = ResultDataParse.getGraphDataMap(jtlString, new ActiveThreadsGraphConsumer()); - Map hitsMap = ResultDataParse.getGraphDataMap(jtlString, new HitsPerSecondGraphConsumer()); - List resultList = ResultDataParse.graphMapParsing(activeThreadMap, "users"); - List hitsList = ResultDataParse.graphMapParsing(hitsMap, "hits"); - resultList.addAll(hitsList); - return resultList; - } - - public static List getResponseTimeChartData(String jtlString) { - Map activeThreadMap = ResultDataParse.getGraphDataMap(jtlString, new ActiveThreadsGraphConsumer()); - Map responseTimeMap = ResultDataParse.getGraphDataMap(jtlString, new ResponseTimeOverTimeGraphConsumer()); - List resultList = ResultDataParse.graphMapParsing(activeThreadMap, "users"); - List responseTimeList = ResultDataParse.graphMapParsing(responseTimeMap, "responseTime"); - resultList.addAll(responseTimeList); - return resultList; - } - - public static ReportTimeInfo getReportTimeInfo(String jtlString) { - List totalLineList = GenerateReport.resolver(jtlString); - - totalLineList.sort(Comparator.comparing(t0 -> Long.valueOf(t0.getTimestamp()))); - - String startTimeStamp = totalLineList.get(0).getTimestamp(); - String endTimeStamp = totalLineList.get(totalLineList.size() - 1).getTimestamp(); - - DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"); - String startTime = dtf.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(Long.parseLong(startTimeStamp)), ZoneId.systemDefault())); - String endTime = dtf.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(Long.parseLong(endTimeStamp)), ZoneId.systemDefault())); - - // todo 时间问题 - long seconds = Duration.between(Instant.ofEpochMilli(Long.parseLong(startTimeStamp)), Instant.ofEpochMilli(Long.parseLong(endTimeStamp))).getSeconds(); - ReportTimeInfo reportTimeInfo = new ReportTimeInfo(); - reportTimeInfo.setStartTime(startTime); - reportTimeInfo.setEndTime(endTime); - reportTimeInfo.setDuration(String.valueOf(seconds)); - - return reportTimeInfo; - } - -} \ No newline at end of file diff --git a/backend/src/main/java/io/metersphere/report/parse/ResultDataParse.java b/backend/src/main/java/io/metersphere/report/parse/ResultDataParse.java deleted file mode 100644 index 527bfc51a1..0000000000 --- a/backend/src/main/java/io/metersphere/report/parse/ResultDataParse.java +++ /dev/null @@ -1,190 +0,0 @@ -package io.metersphere.report.parse; - -import io.metersphere.commons.utils.MsJMeterUtils; -import io.metersphere.report.base.ChartsData; -import org.apache.jmeter.report.core.Sample; -import org.apache.jmeter.report.core.SampleMetadata; -import org.apache.jmeter.report.dashboard.JsonizerVisitor; -import org.apache.jmeter.report.processor.*; -import org.apache.jmeter.report.processor.graph.AbstractOverTimeGraphConsumer; -import java.lang.reflect.Field; -import java.math.BigDecimal; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.time.Instant; -import java.time.LocalDateTime; -import java.time.ZoneId; -import java.time.format.DateTimeFormatter; -import java.util.*; - -public class ResultDataParse { - - private static final String DATE_TIME_PATTERN = "yyyy/MM/dd HH:mm:ss"; - private static final String TIME_PATTERN = "HH:mm:ss"; - - public static List summaryMapParsing(Map map, Class clazz) { - List list = new ArrayList<>(); - for (String key : map.keySet()) { - MapResultData mapResultData = (MapResultData) map.get(key); - ListResultData items = (ListResultData) mapResultData.getResult("items"); - if (items.getSize() > 0) { - for (int i = 0; i < items.getSize(); i++) { - MapResultData resultData = (MapResultData) items.get(i); - ListResultData data = (ListResultData) resultData.getResult("data"); - int size = data.getSize(); - String[] strArray = new String[size]; - if (size > 0) { - T t = null; - for (int j = 0; j < size; j++) { - ValueResultData valueResultData = (ValueResultData) data.get(j); - if (valueResultData.getValue() == null) { - strArray[j] = ""; - } else { - String accept = valueResultData.accept(new JsonizerVisitor()); - strArray[j] = accept.replace("\\", ""); - } - } - - try { - t = setParam(clazz, strArray); - } catch (Exception e) { - e.printStackTrace(); - } - list.add(t); - } - } - } - } - return list; - } - - public static List graphMapParsing(Map map, String seriesName) { - List list = new ArrayList<>(); - // ThreadGroup - for (String key : map.keySet()) { - MapResultData mapResultData = (MapResultData) map.get(key); - ResultData maxY = mapResultData.getResult("maxY"); - ListResultData series = (ListResultData) mapResultData.getResult("series"); - if (series.getSize() > 0) { - for (int j = 0; j < series.getSize(); j++) { - MapResultData resultData = (MapResultData) series.get(j); - // data, isOverall, label, isController - ListResultData data = (ListResultData) resultData.getResult("data"); - ValueResultData label = (ValueResultData) resultData.getResult("label"); - - if (data.getSize() > 0) { - for (int i = 0; i < data.getSize(); i++) { - ListResultData listResultData = (ListResultData) data.get(i); - String result = listResultData.accept(new JsonizerVisitor()); - result = result.substring(1, result.length() - 1); - String[] split = result.split(","); - ChartsData chartsData = new ChartsData(); - BigDecimal bigDecimal = new BigDecimal(split[0]); - String timeStamp = bigDecimal.toPlainString(); - String time = null; - try { - time = formatDate(stampToDate(DATE_TIME_PATTERN, timeStamp)); - } catch (ParseException e) { - e.printStackTrace(); - } - chartsData.setxAxis(time); - chartsData.setyAxis(new BigDecimal(split[1].trim())); - if (series.getSize() == 1) { - chartsData.setGroupName(seriesName); - } else { - chartsData.setGroupName((String) label.getValue()); - } - list.add(chartsData); - } - } - } - - } - } - return list; - } - - public static Map getGraphDataMap(String jtlString, AbstractOverTimeGraphConsumer timeGraphConsumer) { - AbstractOverTimeGraphConsumer abstractOverTimeGraphConsumer = timeGraphConsumer; - abstractOverTimeGraphConsumer.setGranularity(60000); - abstractOverTimeGraphConsumer.initialize(); - SampleContext sampleContext = initJmeterConsumer(jtlString, abstractOverTimeGraphConsumer); - return sampleContext.getData(); - } - - public static Map getSummryDataMap(String jtlString, AbstractSummaryConsumer summaryConsumer) { - AbstractSummaryConsumer abstractSummaryConsumer = summaryConsumer; - SampleContext sampleContext = initJmeterConsumer(jtlString, summaryConsumer); - return sampleContext.getData(); - } - - private static SampleContext initJmeterConsumer(String jtlString, AbstractSampleConsumer abstractSampleConsumer) { - int row = 0; - // 使用反射获取properties - MsJMeterUtils.loadJMeterProperties("jmeter.properties"); - SampleMetadata sampleMetaData = createTestMetaData(); - SampleContext sampleContext = new SampleContext(); - abstractSampleConsumer.setSampleContext(sampleContext); - abstractSampleConsumer.startConsuming(); - StringTokenizer tokenizer = new StringTokenizer(jtlString, "\n"); - // 去掉第一行 - tokenizer.nextToken(); - while (tokenizer.hasMoreTokens()) { - String line = tokenizer.nextToken(); - String[] data = line.split(",", -1); - Sample sample = new Sample(row++, sampleMetaData, data); - abstractSampleConsumer.consume(sample, 0); - } - abstractSampleConsumer.stopConsuming(); - return sampleContext; - } - - // Create a static SampleMetadataObject - private static SampleMetadata createTestMetaData() { - String columnsString = "timeStamp,elapsed,label,responseCode,responseMessage,threadName,success,failureMessage,bytes,sentBytes,grpThreads,allThreads,URL,Latency,IdleTime,Connect"; - columnsString = "timeStamp,elapsed,label,responseCode,responseMessage,threadName,dataType,success,failureMessage,bytes,sentBytes,grpThreads,allThreads,URL,Latency,IdleTime,Connect"; - - String[] columns = new String[17]; - int lastComa = 0; - int columnIndex = 0; - for (int i = 0; i < columnsString.length(); i++) { - if (columnsString.charAt(i) == ',') { - columns[columnIndex] = columnsString.substring(lastComa, i); - lastComa = i + 1; - columnIndex++; - } else if (i + 1 == columnsString.length()) { - columns[columnIndex] = columnsString.substring(lastComa, i + 1); - } - } - return new SampleMetadata(',', columns); - } - - 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); - } - - private static String formatDate(String dateString) throws ParseException { - SimpleDateFormat before = new SimpleDateFormat(DATE_TIME_PATTERN); - SimpleDateFormat after = new SimpleDateFormat(TIME_PATTERN); - return after.format(before.parse(dateString)); - } - - private static T setParam(Class clazz, Object[] args) - throws Exception { - if (clazz == null || args == null) { - throw new IllegalArgumentException(); - } - T t = clazz.newInstance(); - Field[] fields = clazz.getDeclaredFields(); - if (fields == null || fields.length > args.length) { - throw new IndexOutOfBoundsException(); - } - for (int i = 0; i < fields.length; i++) { - fields[i].setAccessible(true); - fields[i].set(t, args[i]); - } - return t; - } -}