移除代码
This commit is contained in:
parent
61e88258f0
commit
24be510be8
|
@ -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<Metric> resolver(String jtlString) {
|
||||
HeaderColumnNameMappingStrategy<Metric> ms = new HeaderColumnNameMappingStrategy<>();
|
||||
ms.setType(Metric.class);
|
||||
try (Reader reader = new StringReader(jtlString)) {
|
||||
CsvToBean<Metric> cb = new CsvToBeanBuilder<Metric>(reader)
|
||||
.withType(Metric.class)
|
||||
.withSkipLines(0)
|
||||
.withMappingStrategy(ms)
|
||||
.withIgnoreLeadingWhiteSpace(true)
|
||||
.build();
|
||||
return cb.parse();
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static List<Errors> getErrorsList(String jtlString) {
|
||||
Map<String, Object> statisticsDataMap = ResultDataParse.getSummryDataMap(jtlString, new ErrorsSummaryConsumer());
|
||||
return ResultDataParse.summaryMapParsing(statisticsDataMap, Errors.class);
|
||||
}
|
||||
|
||||
public static List<ErrorsTop5> getErrorsTop5List(String jtlString) {
|
||||
Map<String, Object> statisticsDataMap = ResultDataParse.getSummryDataMap(jtlString, new Top5ErrorsBySamplerConsumer());
|
||||
return ResultDataParse.summaryMapParsing(statisticsDataMap, ErrorsTop5.class);
|
||||
}
|
||||
|
||||
public static List<Statistics> getRequestStatistics(String jtlString) {
|
||||
Map<String, Object> 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<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, 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, 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);
|
||||
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<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);
|
||||
|
||||
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<ChartsData> getLoadChartData(String jtlString) {
|
||||
Map<String, Object> activeThreadMap = ResultDataParse.getGraphDataMap(jtlString, new ActiveThreadsGraphConsumer());
|
||||
Map<String, Object> hitsMap = ResultDataParse.getGraphDataMap(jtlString, new HitsPerSecondGraphConsumer());
|
||||
List<ChartsData> resultList = ResultDataParse.graphMapParsing(activeThreadMap, "users");
|
||||
List<ChartsData> hitsList = ResultDataParse.graphMapParsing(hitsMap, "hits");
|
||||
resultList.addAll(hitsList);
|
||||
return resultList;
|
||||
}
|
||||
|
||||
public static List<ChartsData> getResponseTimeChartData(String jtlString) {
|
||||
Map<String, Object> activeThreadMap = ResultDataParse.getGraphDataMap(jtlString, new ActiveThreadsGraphConsumer());
|
||||
Map<String, Object> responseTimeMap = ResultDataParse.getGraphDataMap(jtlString, new ResponseTimeOverTimeGraphConsumer());
|
||||
List<ChartsData> resultList = ResultDataParse.graphMapParsing(activeThreadMap, "users");
|
||||
List<ChartsData> responseTimeList = ResultDataParse.graphMapParsing(responseTimeMap, "responseTime");
|
||||
resultList.addAll(responseTimeList);
|
||||
return resultList;
|
||||
}
|
||||
|
||||
public static ReportTimeInfo getReportTimeInfo(String jtlString) {
|
||||
List<Metric> 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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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 <T> List<T> summaryMapParsing(Map<String, Object> map, Class<T> clazz) {
|
||||
List<T> 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<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));
|
||||
}
|
||||
|
||||
private static <T> T setParam(Class<T> 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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue