Merge branch 'dev' of github.com:fit2cloudrd/metersphere-server into dev

This commit is contained in:
Captain.B 2020-03-26 11:27:02 +08:00
commit 4cc73c172c
5 changed files with 181 additions and 35 deletions

View File

@ -9,6 +9,7 @@ import io.metersphere.commons.utils.Pager;
import io.metersphere.controller.request.ReportRequest;
import io.metersphere.dto.ReportDTO;
import io.metersphere.report.base.RequestStatistics;
import io.metersphere.report.base.RequestStatisticsDTO;
import io.metersphere.service.ReportService;
import io.metersphere.user.SessionUtils;
import org.apache.shiro.authz.annotation.Logical;
@ -54,7 +55,7 @@ public class ReportController {
}
@GetMapping("/content/{reportId}")
public List<RequestStatistics> getReportContent(@PathVariable String reportId) {
public RequestStatisticsDTO getReportContent(@PathVariable String reportId) {
return reportService.getReport(reportId);
}

View File

@ -5,6 +5,8 @@ import com.opencsv.bean.CsvToBeanBuilder;
import com.opencsv.bean.HeaderColumnNameMappingStrategy;
import io.metersphere.report.base.Metric;
import io.metersphere.report.base.RequestStatistics;
import io.metersphere.report.base.RequestStatisticsDTO;
import java.io.Reader;
import java.io.StringReader;
import java.text.DecimalFormat;
@ -31,9 +33,12 @@ public class JtlResolver {
return null;
}
private static List<RequestStatistics> getOneRpsResult(Map<String, List<Metric>> map){
private static RequestStatisticsDTO getOneRpsResult(Map<String, List<Metric>> map){
RequestStatisticsDTO statisticsDTO = new RequestStatisticsDTO();
List<RequestStatistics> requestStatisticsList = new ArrayList<>();
Iterator<Map.Entry<String, List<Metric>>> iterator = map.entrySet().iterator();
List<Integer> allelapse = new ArrayList<>();
Integer totalAverage = 0;
while (iterator.hasNext()) {
Map.Entry<String, List<Metric>> entry = iterator.next();
String label = entry.getKey();
@ -43,7 +48,7 @@ public class JtlResolver {
//总的响应时间
int sumElapsed=0;
Integer failSize = 0;
Integer totalBytes = 0;
Float totalBytes = 0f;
List<Integer> elapsedList = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
@ -52,7 +57,9 @@ public class JtlResolver {
//响应时间
String elapsed = row.getElapsed();
sumElapsed += Integer.valueOf(elapsed);
totalAverage += Integer.valueOf(elapsed);
elapsedList.add(Integer.valueOf(elapsed));
allelapse.add(Integer.valueOf(elapsed));
//成功与否
String success = row.getSuccess();
if (!"true".equals(success)){
@ -60,19 +67,14 @@ public class JtlResolver {
}
//字节
String bytes = row.getBytes();
totalBytes += Integer.valueOf(bytes);
totalBytes += Float.valueOf(bytes);
index++;
}catch (Exception e){
System.out.println("exception i:"+i);
}
}
Collections.sort(elapsedList, new Comparator<Integer>() {
public int compare(Integer o1, Integer o2) {
return o1-o2;
}
});
Collections.sort(elapsedList);
Integer tp90 = elapsedList.size()*90/100;
Integer tp95 = elapsedList.size()*95/100;
@ -100,16 +102,45 @@ public class JtlResolver {
requestStatistics.setErrors(String.format("%.2f",failSize*100.0/index)+"%");
requestStatistics.setKo(failSize);
/**
* #Samples/取最大值(ts+t)-取最小值(ts))*1000
* 所有的相同请求的bytes总和 / 1024 / 请求持续运行的时间=sum(bytes)/1024/total time
*/
// todo Avg Bandwidth(KBytes/s)
requestStatistics.setKbPerSec(String.format("%.2f",totalBytes*1.0/1024/(l*1.0/1000)));
requestStatisticsList.add(requestStatistics);
}
return requestStatisticsList;
Collections.sort(allelapse);
Integer totalTP90 = allelapse.size()*90/100;
Integer totalTP95 = allelapse.size()*95/100;
Integer totalTP99 = allelapse.size()*99/100;
Integer min = allelapse.get(0);
Integer max = allelapse.get(allelapse.size() - 1);
Integer allSamples = requestStatisticsList.stream().mapToInt(RequestStatistics::getSamples).sum();
Integer failSize = requestStatisticsList.stream().mapToInt(RequestStatistics::getKo).sum();
DecimalFormat df = new DecimalFormat("0.00");
double errors = (double)failSize / allSamples * 100;
String totalerrors = df.format(errors);
double average = (double)totalAverage / allSamples;
String totalaverage = df.format(average);
statisticsDTO.setRequestStatisticsList(requestStatisticsList);
statisticsDTO.setTotalLabel("Total");
statisticsDTO.setTotalSamples(String.valueOf(allSamples));
statisticsDTO.setTotalErrors(totalerrors + "%");
statisticsDTO.setTotalAverage(totalaverage);
statisticsDTO.setTotalMin(String.valueOf(min));
statisticsDTO.setTotalMax(String.valueOf(max));
statisticsDTO.setTotalTP90(String.valueOf(allelapse.get(totalTP90)));
statisticsDTO.setTotalTP95(String.valueOf(allelapse.get(totalTP95)));
statisticsDTO.setTotalTP99(String.valueOf(allelapse.get(totalTP99)));
// todo
// statisticsDTO.setTotalAvgBandwidth();
return statisticsDTO;
}
public static List<RequestStatistics> getRequestStatistics(String jtlString) {
public static RequestStatisticsDTO getRequestStatistics(String jtlString) {
List<Metric> totalLines = resolver(jtlString);
Map<String, List<Metric>> map = totalLines.stream().collect(Collectors.groupingBy(Metric::getLabel));
return getOneRpsResult(map);

View File

@ -0,0 +1,116 @@
package io.metersphere.report.base;
import java.util.List;
public class RequestStatisticsDTO extends RequestStatistics {
private List<RequestStatistics> requestStatisticsList;
private String totalLabel;
private String totalSamples;
private String totalErrors;
private String totalAverage;
private String totalMin;
private String totalMax;
private String totalTP90;
private String totalTP95;
private String totalTP99;
private String totalAvgBandwidth;
public List<RequestStatistics> getRequestStatisticsList() {
return requestStatisticsList;
}
public void setRequestStatisticsList(List<RequestStatistics> requestStatisticsList) {
this.requestStatisticsList = requestStatisticsList;
}
public String getTotalLabel() {
return totalLabel;
}
public void setTotalLabel(String totalLabel) {
this.totalLabel = totalLabel;
}
public String getTotalSamples() {
return totalSamples;
}
public void setTotalSamples(String totalSamples) {
this.totalSamples = totalSamples;
}
public String getTotalErrors() {
return totalErrors;
}
public void setTotalErrors(String totalErrors) {
this.totalErrors = totalErrors;
}
public String getTotalAverage() {
return totalAverage;
}
public void setTotalAverage(String totalAverage) {
this.totalAverage = totalAverage;
}
public String getTotalMin() {
return totalMin;
}
public void setTotalMin(String totalMin) {
this.totalMin = totalMin;
}
public String getTotalMax() {
return totalMax;
}
public void setTotalMax(String totalMax) {
this.totalMax = totalMax;
}
public String getTotalTP90() {
return totalTP90;
}
public void setTotalTP90(String totalTP90) {
this.totalTP90 = totalTP90;
}
public String getTotalTP95() {
return totalTP95;
}
public void setTotalTP95(String totalTP95) {
this.totalTP95 = totalTP95;
}
public String getTotalTP99() {
return totalTP99;
}
public void setTotalTP99(String totalTP99) {
this.totalTP99 = totalTP99;
}
public String getTotalAvgBandwidth() {
return totalAvgBandwidth;
}
public void setTotalAvgBandwidth(String totalAvgBandwidth) {
this.totalAvgBandwidth = totalAvgBandwidth;
}
}

View File

@ -8,6 +8,7 @@ import io.metersphere.controller.request.ReportRequest;
import io.metersphere.dto.ReportDTO;
import io.metersphere.report.JtlResolver;
import io.metersphere.report.base.RequestStatistics;
import io.metersphere.report.base.RequestStatisticsDTO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -44,10 +45,10 @@ public class ReportService {
return extLoadTestReportMapper.getReportTestAndProInfo(reportId);
}
public List<RequestStatistics> getReport(String id) {
public RequestStatisticsDTO getReport(String id) {
LoadTestReport loadTestReport = loadTestReportMapper.selectByPrimaryKey(id);
String content = loadTestReport.getContent();
List<RequestStatistics> requestStatistics = JtlResolver.getRequestStatistics(content);
RequestStatisticsDTO requestStatistics = JtlResolver.getRequestStatistics(content);
return requestStatistics;
}
}

View File

@ -80,37 +80,33 @@
data() {
return {
tableData: [{},{},{},{},{}],
totalInfo: {}
}
},
methods: {
initTableData() {
this.$get("/report/content/" + this.id, res => {
this.tableData = res.data;
this.tableData = res.data.requestStatisticsList;
this.totalInfo = res.data;
})
},
getSummaries (param) {
const { data } = param
getSummaries () {
const sums = []
let allSamples = data.reduce(function (total, currentValue) {
return total + currentValue.samples;
}, 0);
let failSize = data.reduce(function (total, currentValue) {
return total + currentValue.ko;
}, 0);
let allAverageTime = data.reduce(function (total, currentValue) {
return total + parseFloat(currentValue.average) * currentValue.samples;
}, 0);
sums[0] = 'Total'
sums[1] = allSamples;
sums[2] = (Math.round(failSize / allSamples * 10000) / 100) + '%';
sums[3] = (allAverageTime / allSamples).toFixed(2);
sums[4] = Math.min.apply(Math, data.map(function(o) {return parseFloat(o.min)}));
sums[5] = Math.max.apply(Math, data.map(function(o) {return parseFloat(o.max)}));
return sums
sums[0] = this.totalInfo.totalLabel;
sums[1] = this.totalInfo.totalSamples;
sums[2] = this.totalInfo.totalErrors;
sums[3] = this.totalInfo.totalAverage;
sums[4] = this.totalInfo.totalMin;
sums[5] = this.totalInfo.totalMax;
sums[6] = this.totalInfo.totalTP90;
sums[7] = this.totalInfo.totalTP95;
sums[8] = this.totalInfo.totalTP99;
return sums;
}
},
created() {
this.initTableData()
this.getSummaries()
},
props: ['id'],
watch: {
@ -119,7 +115,8 @@
let reportId = to.path.split('/')[4];
if(reportId){
this.$get("/report/content/" + reportId, res => {
this.tableData = res.data;
this.tableData = res.data.requestStatisticsList;
this.totalInfo = res.data;
})
}
}