This commit is contained in:
shiziyuan9527 2020-03-23 18:23:51 +08:00
parent a06b77b38c
commit e699eaf04d
3 changed files with 101 additions and 22 deletions

View File

@ -1,12 +1,16 @@
package io.metersphere.report; package io.metersphere.report;
import com.alibaba.fastjson.JSONObject;
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.report.base.Metric; import io.metersphere.report.base.Metric;
import io.metersphere.report.base.RequestStatistics;
import java.io.Reader; import java.io.Reader;
import java.io.StringReader; import java.io.StringReader;
import java.util.List; import java.util.*;
import java.util.stream.Collectors;
public class JtlResolver { public class JtlResolver {
@ -28,8 +32,82 @@ public class JtlResolver {
return null; return null;
} }
/*public List<RequestStatistics> getRequestStatistics(String jtlString) { private List<RequestStatistics> getOneRpsResult(Map<String, List<Metric>> map){
List<RequestStatistics> requestStatisticsList = new ArrayList<>();
Iterator<Map.Entry<String, List<Metric>>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, List<Metric>> entry = iterator.next();
String label = entry.getKey();
List<Metric> list = entry.getValue();
List<String> timestampList = list.stream().map(Metric::getTimestamp).collect(Collectors.toList());
int index=0;
//总的响应时间
int sumElapsed=0;
Integer failSize = 0;
Integer totalBytes = 0;
List<Integer> elapsedList = new ArrayList<Integer>();
for (int i = 0; i < list.size(); i++) {
try {
Metric row = list.get(i);
//响应时间
String elapsed = row.getElapsed();
sumElapsed += Integer.valueOf(elapsed);
elapsedList.add(Integer.valueOf(elapsed));
//成功与否
String success = row.getSuccess();
if (!"true".equals(success)){
failSize++;
}
//字节
String bytes = row.getBytes();
totalBytes += Integer.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;
}
});
Integer tp90 = elapsedList.size()*9/10;
Integer tp95 = elapsedList.size()*95/100;
Integer tp99 = elapsedList.size()*99/100;
Long l = Long.valueOf(timestampList.get(index-1)) - Long.valueOf(timestampList.get(0));
RequestStatistics requestStatistics = new RequestStatistics();
requestStatistics.setRequestLabel(label);
requestStatistics.setSamples(index+"");
requestStatistics.setAverage(sumElapsed/index+"");
/**
* TP90的计算
* 1把一段时间内全部的请求的响应时间从小到大排序获得序列A
* 2总的请求数量乘以90%获得90%对应的请求个数C
* 3从序列A中找到第C个请求它的响应时间即为TP90的值
* 其余相似的指标还有TP95, TP99
*/
requestStatistics.setTp90(elapsedList.get(tp90)+"");
requestStatistics.setTp95(elapsedList.get(tp95)+"");
requestStatistics.setTp99(elapsedList.get(tp99)+"");
requestStatistics.setMin(elapsedList.get(0)+"");
requestStatistics.setMax(elapsedList.get(index-1)+"");
requestStatistics.setErrors(String.format("%.2f",failSize*100.0/index)+"%");
requestStatistics.setKbPerSec(String.format("%.2f",totalBytes*1.0/1024/(l*1.0/1000)));
requestStatisticsList.add(requestStatistics);
}
return requestStatisticsList;
}
public List<RequestStatistics> getRequestStatistics(String jtlString) {
List<Metric> totalLines = resolver(jtlString); List<Metric> totalLines = resolver(jtlString);
}*/ Map<String, List<Metric>> map = totalLines.stream().collect(Collectors.groupingBy(Metric::getLabel));
return getOneRpsResult(map);
}
} }

View File

@ -5,39 +5,39 @@ import com.opencsv.bean.CsvBindByName;
public class Metric { public class Metric {
// timestamp,elapsed,label,responseCode,responseMessage,threadName,dataType,success,failureMessage,bytes,sentBytes,grpThreads,allThreads,URL,Latency,IdleTime,Connect // timestamp,elapsed,label,responseCode,responseMessage,threadName,dataType,success,failureMessage,bytes,sentBytes,grpThreads,allThreads,URL,Latency,IdleTime,Connect
@CsvBindByName(column = "timestamp") @CsvBindByName(column = "timestamp") // 访问开始时间
private String timestamp; private String timestamp;
@CsvBindByName(column = "elapsed") @CsvBindByName(column = "elapsed") // 访问开始到结束的用时 - 响应时间
private String elapsed; private String elapsed;
@CsvBindByName(column = "label") @CsvBindByName(column = "label") // 请求的标签
private String label; private String label;
@CsvBindByName(column = "responseCode") @CsvBindByName(column = "responseCode") // 响应码
private String responseCode; private String responseCode;
@CsvBindByName(column = "responseMessage") @CsvBindByName(column = "responseMessage") // 响应信息
private String responseMessage; private String responseMessage;
@CsvBindByName(column = "threadName") @CsvBindByName(column = "threadName") // 请求所属线程
private String threadName; private String threadName;
@CsvBindByName(column = "dataType") @CsvBindByName(column = "dataType") // 数据类型
private String dataType; private String dataType;
@CsvBindByName(column = "success") @CsvBindByName(column = "success") // 访问是否成功
private String success; private String success;
@CsvBindByName(column = "failureMessage") @CsvBindByName(column = "failureMessage") // 访问失败信息
private String failureMessage; private String failureMessage;
@CsvBindByName(column = "bytes") @CsvBindByName(column = "bytes") //
private String bytes; private String bytes;
@CsvBindByName(column = "sentBytes") @CsvBindByName(column = "sentBytes") //
private String sentBytes; private String sentBytes;
@CsvBindByName(column = "grpThreads") @CsvBindByName(column = "grpThreads") // 线程组
private String grpThreads; private String grpThreads;
@CsvBindByName(column = "allThreads") @CsvBindByName(column = "allThreads") //
private String allThreads; private String allThreads;
@CsvBindByName(column = "URL") @CsvBindByName(column = "URL") //
private String url; private String url;
@CsvBindByName(column = "Latency") @CsvBindByName(column = "Latency") // 延时
private String latency; private String latency;
@CsvBindByName(column = "IdleTime") @CsvBindByName(column = "IdleTime") // 闲置时间
private String idleTime; private String idleTime;
@CsvBindByName(column = "Connect") @CsvBindByName(column = "Connect") //
private String connect; private String connect;

View File

@ -209,6 +209,7 @@ public class JtlTest {
Map.Entry<String, List<Metric>> entry = iterator.next(); Map.Entry<String, List<Metric>> entry = iterator.next();
String label = entry.getKey(); String label = entry.getKey();
List<Metric> list = entry.getValue(); List<Metric> list = entry.getValue();
List<String> timestampList = list.stream().map(Metric::getTimestamp).collect(Collectors.toList());
int index=0; int index=0;
//总的响应时间 //总的响应时间
int sumElapsed=0; int sumElapsed=0;
@ -248,7 +249,7 @@ public class JtlTest {
Integer tp95 = elapsedList.size()*95/100; Integer tp95 = elapsedList.size()*95/100;
Integer tp99 = elapsedList.size()*99/100; Integer tp99 = elapsedList.size()*99/100;
// Long l = timestampList.get(index-1) - timestampList.get(0); Long l = Long.valueOf(timestampList.get(index-1)) - Long.valueOf(timestampList.get(0));
RequestStatistics sceneResult = new RequestStatistics(); RequestStatistics sceneResult = new RequestStatistics();
sceneResult.setRequestLabel(label); sceneResult.setRequestLabel(label);
@ -260,7 +261,7 @@ public class JtlTest {
sceneResult.setMin(elapsedList.get(0)+""); sceneResult.setMin(elapsedList.get(0)+"");
sceneResult.setMax(elapsedList.get(index-1)+""); sceneResult.setMax(elapsedList.get(index-1)+"");
sceneResult.setErrors(String.format("%.2f",failSize*100.0/index)+"%"); sceneResult.setErrors(String.format("%.2f",failSize*100.0/index)+"%");
// sceneResult.setKbPerSec(String.format("%.2f",totalBytes*1.0/1024/(l*1.0/1000))); sceneResult.setKbPerSec(String.format("%.2f",totalBytes*1.0/1024/(l*1.0/1000)));
System.out.println(JSONObject.toJSONString(sceneResult)); System.out.println(JSONObject.toJSONString(sceneResult));
System.out.println(); System.out.println();