修改产生报告的后台代码

This commit is contained in:
shiziyuan9527 2020-04-21 14:27:12 +08:00
parent dfa9a3ccf2
commit 1578a81d8f
2 changed files with 153 additions and 115 deletions

View File

@ -3,23 +3,16 @@ package io.metersphere.report;
import com.opencsv.bean.CsvToBean; import com.opencsv.bean.CsvToBean;
import com.opencsv.bean.CsvToBeanBuilder; import com.opencsv.bean.CsvToBeanBuilder;
import com.opencsv.bean.HeaderColumnNameMappingStrategy; import com.opencsv.bean.HeaderColumnNameMappingStrategy;
import io.metersphere.commons.utils.MsJMeterUtils;
import io.metersphere.report.base.*; import io.metersphere.report.base.*;
import io.metersphere.report.dto.ErrorsTop5DTO; import io.metersphere.report.dto.ErrorsTop5DTO;
import io.metersphere.report.dto.RequestStatisticsDTO; import io.metersphere.report.dto.RequestStatisticsDTO;
import io.metersphere.report.parse.ResultDataParse;
import org.apache.commons.lang3.StringUtils; 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.ActiveThreadsGraphConsumer;
import org.apache.jmeter.report.processor.graph.impl.HitsPerSecondGraphConsumer; import org.apache.jmeter.report.processor.graph.impl.HitsPerSecondGraphConsumer;
import org.apache.jmeter.report.processor.graph.impl.ResponseTimeOverTimeGraphConsumer; import org.apache.jmeter.report.processor.graph.impl.ResponseTimeOverTimeGraphConsumer;
import java.io.Reader; import java.io.Reader;
import java.io.StringReader; import java.io.StringReader;
import java.math.BigDecimal;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
@ -278,7 +271,7 @@ public class JtlResolver {
DecimalFormat decimalFormat = new DecimalFormat("0.00"); DecimalFormat decimalFormat = new DecimalFormat("0.00");
List<Metric> totalLineList = JtlResolver.resolver(jtlString); List<Metric> totalLineList = JtlResolver.resolver(jtlString);
// todo // todo 修改测试概览的数值
List<Metric> totalLineList2 = JtlResolver.resolver(jtlString); List<Metric> totalLineList2 = JtlResolver.resolver(jtlString);
// 时间戳转时间 // 时间戳转时间
for (Metric metric : totalLineList2) { for (Metric metric : totalLineList2) {
@ -343,115 +336,21 @@ public class JtlResolver {
} }
public static List<ChartsData> getLoadChartData(String jtlString) { public static List<ChartsData> getLoadChartData(String jtlString) {
Map<String, Object> activeThreadMap = getResultDataMap(jtlString, new ActiveThreadsGraphConsumer()); Map<String, Object> activeThreadMap = ResultDataParse.getGraphDataMap(jtlString, new ActiveThreadsGraphConsumer());
Map<String, Object> hitsMap = getResultDataMap(jtlString, new HitsPerSecondGraphConsumer()); Map<String, Object> hitsMap = ResultDataParse.getGraphDataMap(jtlString, new HitsPerSecondGraphConsumer());
List<ChartsData> activeThreadList = new ArrayList<>(); List<ChartsData> resultList = ResultDataParse.graphMapParsing(activeThreadMap, "users");
List<ChartsData> hitsList = new ArrayList<>(); List<ChartsData> hitsList = ResultDataParse.graphMapParsing(hitsMap, "hits");
mapResolver(activeThreadMap, activeThreadList, "users"); resultList.addAll(hitsList);
mapResolver(hitsMap, hitsList, "hits"); return resultList;
activeThreadList.addAll(hitsList);
return activeThreadList;
} }
public static List<ChartsData> getResponseTimeChartData(String jtlString) { public static List<ChartsData> getResponseTimeChartData(String jtlString) {
Map<String, Object> activeThreadMap = getResultDataMap(jtlString, new ActiveThreadsGraphConsumer()); Map<String, Object> activeThreadMap = ResultDataParse.getGraphDataMap(jtlString, new ActiveThreadsGraphConsumer());
Map<String, Object> responseTimeMap = getResultDataMap(jtlString, new ResponseTimeOverTimeGraphConsumer()); Map<String, Object> responseTimeMap = ResultDataParse.getGraphDataMap(jtlString, new ResponseTimeOverTimeGraphConsumer());
List<ChartsData> activeThreadList = new ArrayList<>(); List<ChartsData> resultList = ResultDataParse.graphMapParsing(activeThreadMap, "users");
List<ChartsData> responseTimeList = new ArrayList<>(); List<ChartsData> responseTimeList = ResultDataParse.graphMapParsing(responseTimeMap, "responseTime");
mapResolver(activeThreadMap, activeThreadList, "users"); resultList.addAll(responseTimeList);
mapResolver(responseTimeMap, responseTimeList, "responseTime"); return resultList;
activeThreadList.addAll(responseTimeList);
return activeThreadList;
}
public static void mapResolver(Map<String, Object> 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<String, Object> 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);
} }
public static ReportTimeInfo getReportTimeInfo(String jtlString) { public static ReportTimeInfo getReportTimeInfo(String jtlString) {

View File

@ -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<ChartsData> graphMapParsing(Map<String, Object> map, String seriesName) {
List<ChartsData> 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<String, Object> getGraphDataMap(String jtlString, AbstractOverTimeGraphConsumer timeGraphConsumer) {
AbstractOverTimeGraphConsumer abstractOverTimeGraphConsumer = timeGraphConsumer;
abstractOverTimeGraphConsumer.setGranularity(60000);
abstractOverTimeGraphConsumer.initialize();
SampleContext sampleContext = initJmeterConsumer(jtlString, abstractOverTimeGraphConsumer);
return sampleContext.getData();
}
public static Map<String, Object> 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));
}
}