第一步:新建一个空的maven工程。
第二步:把pom.xml文件更新成如下内容
4.0.0
xiangyoukeji.gis.zrc
JmeterTestResult
0.1
JmeterTestResult
Demo project for Spring Boot
1.8
com.alibaba
fastjson
1.2.62
commons-io
commons-io
2.4
org.apache.commons
commons-lang3
3.4
GeoSolutions
http://maven.geo-solutions.it/
org.apache.maven.plugins
maven-compiler-plugin
1.8
maven-assembly-plugin
false
jar-with-dependencies
xiangyoukeji.gis.gisportalTest.StatisticsForReportOneFile
make-assembly
package
assembly
注意pom文件中指定了 mainClass ,在这个示例中的,mainClass是xiangyoukeji.gis.gisportalTest.StatisticsForReportOneFile
第三步:创建xiangyoukeji.gis.gisportalTest.StatisticsForReportOneFile.java 类。
内容如下:
package xiangyoukeji.gis.gisportalTest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
public class StatisticsForReportOneFile {
public static void main(String[] args) throws IOException {
String resultFilePath = args[0]; // 运行时指定参数
String statisticSaveLocation = resultFilePath +File.separator+"statistics";
File resultDir = new File(resultFilePath);
List resultFiles = Arrays.asList(resultDir.listFiles());
List summaryResult = new ArrayList<>();
for(File resultFile : resultFiles) {
String resultFileName = resultFile.getName();
List everyResultFiles = Arrays.asList(resultFile.listFiles());
if("statistics".equals(resultFileName)) {
continue;
}
List headerLine = getHeaderLine();
for(File everyResultFile : everyResultFiles) {
String everyResultFileName = everyResultFile.getName(); // 如5_70_11
if(everyResultFile == null) {
continue;
}
File statisticsJsonFile = null;
try {
statisticsJsonFile = getStatisticsJsonFile(everyResultFile);
String json = FileUtils.readFileToString(statisticsJsonFile);
JSONObject jsonObject = JSON.parseObject(json);
JSONObject totalJsonObj = jsonObject.getJSONObject("Total");
String colval = getColVal(totalJsonObj, everyResultFile);
colval = resultFileName + "," + colval;
headerLine.add(colval);
} catch (Exception e) {
System.out.println(resultFileName+"_"+everyResultFileName+"出了点小问题");
}
}
summaryResult.addAll(headerLine);
}
removeDumplicateHeader(summaryResult);
FileUtils.writeLines(new File(statisticSaveLocation+File.separator+"summary.csv"), summaryResult);
}
private static void removeDumplicateHeader(List summaryResult) {
int i=0;
Iterator it = summaryResult.iterator();
while(it.hasNext()) {
String line = it.next();
if(line.contains("案列名称") && i!=0) {
it.remove();
}
i++;
}
}
private static String getColVal(JSONObject totalJsonObj, File everyResultFile) throws Exception {
// String sample = totalJsonObj.getString("sample");
// String transaction = totalJsonObj.getString("transaction");
String sampleCount = totalJsonObj.getString("sampleCount");
String errorCount = totalJsonObj.getString("errorCount");
String errorPct = totalJsonObj.getString("errorPct") + "%";
String meanResTime = totalJsonObj.getString("meanResTime");
String str =
sampleCount + "," +
errorCount + "," +
errorPct + "," +
meanResTime;
if(Integer.parseInt(errorCount) == 1) {
String errorReason = findErrorReason(everyResultFile);
str+=","+errorReason;
}
return str;
}
private static String findErrorReason(File everyResultFile) throws Exception {
File[] fileArr = everyResultFile.listFiles();
if(fileArr==null) {
throw new Exception("空");
}
List files = Arrays.asList(fileArr);
File resultTxtFile = null;
for(File file : files) {
if(!file.isDirectory()) {
resultTxtFile = file;
break;
}
}
String line = FileUtils.readLines(resultTxtFile).get(1);
String str = line.split(",")[3] + ":" + line.split(",")[4];
return str;
}
private static File getStatisticsJsonFile(File everyResultFile) throws Exception {
File[] fileArr = everyResultFile.listFiles();
if(fileArr==null) {
throw new Exception("空");
}
List files = Arrays.asList(fileArr);
File reportDir = null;
for(File file : files) {
if(file.isDirectory()) {
reportDir = file;
break;
}
}
File[] statisticsFiles = reportDir.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
if(name.equals("statistics.json"))
{
return true;
}
else {
return false;
}
}
});
return statisticsFiles[0];
}
private static List getHeaderLine() {
String header = "案列名称,sampleCount,errorCount,errorPct,meanResTime(ms),失败原因";
List list = new ArrayList<>();
list.add(header);
return list;
}
}
第四步:执行maven的打包命令,得到jar包
第五步:在命令行中 执行命令
java -jar xxx.jar F:\my_work\xiangyoukeji\自动化测试结果\result
其中xxx 代表jar的名称,根据你的jar名称进行调整, 后面的是 传入到主类main 方法的参数