From 1578a81d8f0d737d856788a173351946666ba36f Mon Sep 17 00:00:00 2001 From: shiziyuan9527 Date: Tue, 21 Apr 2020 14:27:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=A7=E7=94=9F=E6=8A=A5?= =?UTF-8?q?=E5=91=8A=E7=9A=84=E5=90=8E=E5=8F=B0=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/metersphere/report/JtlResolver.java | 129 ++-------------- .../report/parse/ResultDataParse.java | 139 ++++++++++++++++++ 2 files changed, 153 insertions(+), 115 deletions(-) create mode 100644 backend/src/main/java/io/metersphere/report/parse/ResultDataParse.java diff --git a/backend/src/main/java/io/metersphere/report/JtlResolver.java b/backend/src/main/java/io/metersphere/report/JtlResolver.java index e4b79a1c79..5e3b4d163e 100644 --- a/backend/src/main/java/io/metersphere/report/JtlResolver.java +++ b/backend/src/main/java/io/metersphere/report/JtlResolver.java @@ -3,23 +3,16 @@ package io.metersphere.report; import com.opencsv.bean.CsvToBean; import com.opencsv.bean.CsvToBeanBuilder; import com.opencsv.bean.HeaderColumnNameMappingStrategy; -import io.metersphere.commons.utils.MsJMeterUtils; import io.metersphere.report.base.*; import io.metersphere.report.dto.ErrorsTop5DTO; import io.metersphere.report.dto.RequestStatisticsDTO; +import io.metersphere.report.parse.ResultDataParse; import org.apache.commons.lang3.StringUtils; -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 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.text.ParseException; import java.text.SimpleDateFormat; @@ -278,7 +271,7 @@ public class JtlResolver { DecimalFormat decimalFormat = new DecimalFormat("0.00"); List totalLineList = JtlResolver.resolver(jtlString); - // todo + // todo 修改测试概览的数值 List totalLineList2 = JtlResolver.resolver(jtlString); // 时间戳转时间 for (Metric metric : totalLineList2) { @@ -343,115 +336,21 @@ public class JtlResolver { } public static List getLoadChartData(String jtlString) { - Map activeThreadMap = getResultDataMap(jtlString, new ActiveThreadsGraphConsumer()); - Map hitsMap = getResultDataMap(jtlString, new HitsPerSecondGraphConsumer()); - List activeThreadList = new ArrayList<>(); - List hitsList = new ArrayList<>(); - mapResolver(activeThreadMap, activeThreadList, "users"); - mapResolver(hitsMap, hitsList, "hits"); - activeThreadList.addAll(hitsList); - return activeThreadList; + 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 = getResultDataMap(jtlString, new ActiveThreadsGraphConsumer()); - Map responseTimeMap = getResultDataMap(jtlString, new ResponseTimeOverTimeGraphConsumer()); - List activeThreadList = new ArrayList<>(); - List responseTimeList = new ArrayList<>(); - mapResolver(activeThreadMap, activeThreadList, "users"); - mapResolver(responseTimeMap, responseTimeList, "responseTime"); - activeThreadList.addAll(responseTimeList); - return activeThreadList; - - } - - public static void mapResolver(Map map, List list, String seriesName) { - // ThreadGroup-1 - 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); - } - } - } - - - } - } - } - - public static Map getResultDataMap(String jtlString, AbstractOverTimeGraphConsumer timeGraphConsumer) { - int row = 0; - AbstractOverTimeGraphConsumer abstractOverTimeGraphConsumer = timeGraphConsumer; - abstractOverTimeGraphConsumer.setGranularity(60000); - // 使用反射获取properties - MsJMeterUtils.loadJMeterProperties("jmeter.properties"); // 这个路径不存在 - SampleMetadata sampleMetaData = createTestMetaData(); - SampleContext sampleContext = new SampleContext(); - abstractOverTimeGraphConsumer.setSampleContext(sampleContext); - abstractOverTimeGraphConsumer.initialize(); - abstractOverTimeGraphConsumer.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); - abstractOverTimeGraphConsumer.consume(sample, 0); - } - abstractOverTimeGraphConsumer.stopConsuming(); - return sampleContext.getData(); - } - - // 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); + 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) { diff --git a/backend/src/main/java/io/metersphere/report/parse/ResultDataParse.java b/backend/src/main/java/io/metersphere/report/parse/ResultDataParse.java new file mode 100644 index 0000000000..016fbc0855 --- /dev/null +++ b/backend/src/main/java/io/metersphere/report/parse/ResultDataParse.java @@ -0,0 +1,139 @@ +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.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.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.StringTokenizer; + +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 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)); + } +}