在上篇文章中有提到在獲取完成配置后尘惧,調(diào)用runcase()方法颂郎,如下:
//執(zhí)行monitorcase
function runcase(){
url="runcases";
$.post(
url,
{ //傳入?yún)?shù):lineName 業(yè)務(wù)線名稱缸浦;frequency 監(jiān)控頻率试读; smsValue 是否發(fā)送短信警儒。
"lineName" : document.getElementById("dropDownToggleName").text,
"frequency" : document.getElementById("monitorFreq").value,
"smsValue" : document.getElementById("smsValue").checked
},
function(data){
removeElementById("accordion");
removeElementById("summary");
var total = data.total;
var itemContent = "<h5 class=summary id=summary> 共找到" + total + "條數(shù)據(jù)</h5>"
var json_array = data.data;
itemContent += "<div class=panel-group id=accordion>"
for (var i = 0; i < json_array.length; i++) {
if (json_array[i].success == "false") {
itemContent += "<div class='panel panel-danger'>"
} else {
itemContent += "<div class='panel panel-success'>"
}
itemContent += "<div class=panel-heading>"
itemContent += "<h4 class=panel-title style=float:left>"
itemContent += "<a data-toggle=collapse data-parent=#accordion href=#collapse" + json_array[i].id + ">"
itemContent += "用例名稱: " + json_array[i].fileName
itemContent += "</a></h4>"
itemContent += "<a style=float:right; href=javascript:copy('" + json_array[i].id + "');>前往接口測試平臺</a>"
itemContent += "<div style=clear:both></div>"
itemContent += "</div>"
itemContent += "<div id=collapse" + json_array[i].id + " class='panel-collapse collapse'>"
itemContent += "<div class=panel-body>"
if (json_array[i].success == "false") {
itemContent += " Event: "+ JSON.stringify(json_array[i].failureMessage)
} else {
itemContent += " Event: 測試通過"
}
itemContent += "</div></div></div>"
}
itemContent += "</div>"
removeElementById("accordion");
removeElementById("summary");
$(".seperator1").after(itemContent);;
},"json")
}
web.xml配置
<servlet>
<servlet-name>runcases</servlet-name>
<servlet-class>com.xxx.servlet.RunMonitorCasesServlet</servlet-class>
<init-param>
<param-name>monitorCaseRootPath</param-name>
<param-value>此處配置jmx文件存儲路徑</param-value>
</init-param>
<init-param>
<param-name>jmeterCommand</param-name>
<param-value>此處配置jmeter執(zhí)行文件路徑</param-value>
</init-param>
<!-- 設(shè)置結(jié)果文件存儲路徑 -->
<init-param>
<param-name>caseResultPath</param-name>
<param-value>此處配置結(jié)果文件存儲路徑</param-value>
</init-param>
<!--設(shè)置各個業(yè)務(wù)線郵箱接收人 支持多個郵箱竿奏,英文逗號分隔即可 -->
<init-param>
<param-name>serverline1</param-name>
<param-value>xxx@xxx.com</param-value>
</init-param>
<init-param>
<param-name>serverline2</param-name>
<param-value>xxx@xxx.com</param-value>
</init-param>
……
<init-param>
<param-name>testmail</param-name>
<param-value>xxx@xxx.com</param-value>
</init-param>
<!-- 設(shè)置各個業(yè)務(wù)線短信接收人 支持多個手機號袄简,英文逗號分隔即可-->
<init-param>
<param-name>serverline_sms1</param-name>
<param-value>1xxxxxxxxxx</param-value>
</init-param>
<init-param>
<param-name>serverline_sms2</param-name>
<param-value>1xxxxxxxxxx</param-value>
</init-param>
……
</servlet>
<servlet-mapping>
<servlet-name>runcases</servlet-name>
<url-pattern>/runcases</url-pattern>
</servlet-mapping>
RunMonitorCasesServlet中實現(xiàn)了2個功能,
1.執(zhí)行jmx文件并返回結(jié)果
2.將執(zhí)行結(jié)果進(jìn)行分析
3.發(fā)送郵件以及短信
由于發(fā)送短信是直接調(diào)用公司同事開發(fā)的接口泛啸,再次就不說明了绿语,本篇值介紹執(zhí)行jmx文件并返回結(jié)果,并分析候址,下一篇會單獨介紹發(fā)送郵件~
定義變量
private static final long serialVersionUID = 1L;
// 定義一個ServletConfig對象
private ServletConfig config = null;
//定義case存儲路徑
private String monitorCaseRootPath = "";
//定義Jmeter執(zhí)行文件路徑
private String jmeterCommand = "";
//定義結(jié)果文件存放路徑(總路徑)
private String caseResultPath = "";
// 獲取csv解析結(jié)果列表
private ArrayList<String[]> csvList = new ArrayList<String[]>();
//定義時間戳結(jié)果差
private Long timejmx = (long) 0;
int csvindex=0;
private static Logger loginfo=Logger.getLogger(RunMonitorCasesServlet.class);
獲取文件配置信息
//獲取配置文件信息
public void init(ServletConfig config) throws ServletException {
super.init(config); // 繼承父類的init()方法
this.config = config; // 獲取配置信息
monitorCaseRootPath = config.getInitParameter("monitorCaseRootPath");
jmeterCommand = config.getInitParameter("jmeterCommand");
caseResultPath = config.getInitParameter("caseResultPath");
//配置email信息
serverLine_Mail1 = config.getInitParameter("serverline1");
serverLine_Mail2 = config.getInitParameter("serverline2");
testMail = config.getInitParameter("testmail");
mailMap.put("serverline1",serverLine_Mail1);
mailMap.put("serverline2", serverLine_Mail2);
mailMap.put("test", testMail);
//省略短信配置
}
編寫post方法(執(zhí)行jmx文件)
//定義返回類型和字符集
res.setContentType("application/json; charset=utf-8");
//輸出流對象
PrintWriter out = res.getWriter();
//獲取請求中傳輸?shù)臉I(yè)務(wù)線名稱&監(jiān)控時長
String lineName = req.getParameter("lineName");
String frequency = req.getParameter("frequency");
String smsValue=req.getParameter("smsValue");
//將監(jiān)控時長轉(zhuǎn)換為int類型吕粹,便于后面進(jìn)行處理
int frequencyValue = 0;
try {
frequencyValue = Integer.parseInt(frequency);
} catch (NumberFormatException e) {
e.printStackTrace();
}
try {
//定義返回值json
JSONObject jsonObject = new JSONObject();
//定義json包含的數(shù)組
JSONArray arraycase = new JSONArray();
//處理case存放路徑
File rootFolder = new File(monitorCaseRootPath + lineName);
File[] caseFileList = rootFolder.listFiles();
//定義caseName&當(dāng)日時間_用于定義結(jié)果文件名
String caseName = "";
String caseDate = formatTodayDate();
//創(chuàng)建當(dāng)日結(jié)果文件存放文件夾
File caseResultPathFile=new File(caseResultPath+lineName+File.separator+caseDate+File.separator);
if(!caseResultPathFile.exists()){
caseResultPathFile.mkdirs();
}
//執(zhí)行case&輸出結(jié)果
for (int i = 0; i < caseFileList.length; i++) {
// 僅處理文件
if (caseFileList[i].isFile() && caseFileList[i].toString().endsWith(".jmx") ) {
//獲取caseName的值
caseName = caseFileList[i].getName();
//創(chuàng)建結(jié)果存放文件
File resultFile = new File(
caseResultPath+lineName+File.separator+caseDate+File.separator + caseName.substring(0, caseName.lastIndexOf(".")) +"_"+ caseDate + ".csv");
if(!resultFile.exists()){
resultFile.createNewFile();
}
//執(zhí)行case并輸出csv結(jié)果文件
Process process = Runtime.getRuntime()
.exec(jmeterCommand + " -n -t " + caseFileList[i].toString() + " -l " + caseResultPath+lineName+File.separator+caseDate+File.separator
+ caseName.substring(0, caseName.lastIndexOf(".")) +"_"+ caseDate + ".csv");
process.waitFor(4, TimeUnit.SECONDS);
}
}
分析結(jié)果文件
//結(jié)果處理
//創(chuàng)建失敗計數(shù)文件,并將每條case計數(shù)設(shè)置為0
File countFile=new File(caseResultPath+lineName+"countFile.txt");
//定義執(zhí)行case總數(shù)total
int total = 0;
for (int i = 0; i < caseFileList.length; i++) {
if (caseFileList[i].isFile() && caseFileList[i].toString().endsWith(".jmx")) {
total++;
//遍歷將id,fileName存入json中
JSONObject jsonObjectcase = new JSONObject();
jsonObjectcase.put("id", total);
jsonObjectcase.put("fileName", caseFileList[i].getName().toString());
caseName = caseFileList[i].getName();
//讀取結(jié)果文件
int readCount=0;
while(readCount<3) {
//等待2s后讀取結(jié)果文件
TimeUnit.SECONDS.sleep(2);
csvList = read(
caseResultPath+lineName+File.separator+caseDate+File.separator + caseName.substring(0, caseName.lastIndexOf(".")) +"_"+ caseDate + ".csv");
File file=new File(caseResultPath+lineName+File.separator+caseDate+File.separator + caseName.substring(0, caseName.lastIndexOf(".")) +"_"+ caseDate + ".csv");
long time=file.lastModified();
Date nowTime = new Date(System.currentTimeMillis());
timejmx = nowTime.getTime() - time;
if(csvList.size()==0) {
readCount++;
if(readCount==3) {
jsonObjectcase.put("timedate", time);
jsonObjectcase.put("success", "false");
jsonObjectcase.put("failureMessage", "當(dāng)日首次寫入文件失敗");
}
}else if(timejmx / 1000 > frequencyValue) {
readCount++;
if(readCount==3) {
jsonObjectcase.put("timedate", time);
jsonObjectcase.put("success", "false");
jsonObjectcase.put("failureMessage", "無法讀取到最近一次執(zhí)行結(jié)果");
}
}else {
int csvindex=csvList.size() - 1;
//讀取結(jié)果文件的最后一條岗仑,將時間戳匹耕,結(jié)果,錯誤信息存入json中
jsonObjectcase.put("timedate", csvList.get(csvindex)[0]);
jsonObjectcase.put("success", csvList.get(csvindex)[7]);
jsonObjectcase.put("failureMessage", csvList.get(csvindex)[8]);
readCount=3;
}
}
FileReader fr = new FileReader(countFile);
//創(chuàng)建集合對象
Properties pro = new Properties();
pro.load(fr);
fr.close();
String caseFailValue =pro.getProperty(lineName+"_countnum_caseFail"+total);
String readFailValue =pro.getProperty(lineName+"_countnum_caseFail"+total);
jsonObjectcase.put("caseFailCount",caseFailValue );
jsonObjectcase.put("readFailCount",readFailValue);
//將當(dāng)前case的所有信息存入到j(luò)son數(shù)組中
arraycase.add(jsonObjectcase);
jsonObject.put("data", arraycase);
}
}
//將執(zhí)行case的計數(shù)存入數(shù)組中
jsonObject.put("total", total);
//報警邏輯荠雕,根據(jù)json+計數(shù)文件 操作發(fā)送郵件
int caseFailFlag=0;
int readFailFlag=0;
for(int i=0;i<jsonObject.getJSONArray("data").size();i++) {
//文件已經(jīng)存在
//輸入流讀取文件
FileReader fr = new FileReader(countFile);
//創(chuàng)建集合對象
Properties pro = new Properties();
pro.load(fr);
fr.close();
//獲取count值
String caseid= jsonObject.getJSONArray("data").getJSONObject(i).get("id")+"";
String caseFailValue = pro.getProperty(lineName+"_countnum_caseFail"+caseid);
String readFailValue =pro.getProperty(lineName+"_countnum_readFail"+caseid);
int caseFailCount = Integer.parseInt(caseFailValue);
int readFailCount = Integer.parseInt(readFailValue);
//首先判斷本次執(zhí)行case是否fail稳其,若fail,再進(jìn)行判斷
if(jsonObject.getJSONArray("data").getJSONObject(i).get("failureMessage")!="") {
if(!jsonObject.getJSONArray("data").getJSONObject(i).get("failureMessage").equals("當(dāng)日首次寫入文件失敗") ||
!jsonObject.getJSONArray("data").getJSONObject(i).get("failureMessage").equals("無法讀取到最近一次執(zhí)行結(jié)果") ) {
//fail計數(shù)增加1炸卑,
caseFailCount++;
//fail計數(shù)為3時既鞠,發(fā)送郵件
if(caseFailCount==3) {
pro.setProperty(lineName+"_countnum_caseFail"+caseid, caseFailCount+"");
FileWriter fw = new FileWriter(countFile);
pro.store(fw, "");
fw.close();
jsonObject.getJSONArray("data").getJSONObject(i).put("caseFailCount",caseFailCount+"");
caseFailFlag=1;
//fail計數(shù)為15時,清空計數(shù)
}else if(caseFailCount==15){
caseFailCount=0;
pro.setProperty(lineName+"_countnum_caseFail"+caseid, caseFailCount+"");
FileWriter fw = new FileWriter(countFile);
pro.store(fw, "");
fw.close();
jsonObject.getJSONArray("data").getJSONObject(i).put("caseFailCount",caseFailCount+"");
}else {
pro.setProperty(lineName+"_countnum_caseFail"+caseid, caseFailCount+"");
FileWriter fw = new FileWriter(countFile);
pro.store(fw, "");
fw.close();
jsonObject.getJSONArray("data").getJSONObject(i).put("caseFailCount",caseFailCount+"");
}
}else {
if(jsonObject.getJSONArray("data").getJSONObject(i).get("failureMessage").equals("當(dāng)日首次寫入文件失敗") &&
jsonObject.getJSONArray("data").getJSONObject(i).get("failureMessage").equals("無法讀取到最近一次執(zhí)行結(jié)果") ) {
readFailCount++;
if(readFailCount==3) {
pro.setProperty(lineName+"_countnum_readFail"+caseid, readFailCount+"");
FileWriter fw = new FileWriter(countFile);
pro.store(fw, "");
fw.close();
jsonObject.getJSONArray("data").getJSONObject(i).put("readFailCount",readFailCount+"");
readFailFlag=1;
//fail計數(shù)為15時盖文,清空計數(shù)
}else if(readFailCount==15) {
readFailCount=0;
pro.setProperty(lineName+"_countnum_readFail"+caseid, readFailCount+"");
FileWriter fw = new FileWriter(countFile);
pro.store(fw, "");
fw.close();
jsonObject.getJSONArray("data").getJSONObject(i).put("readFailCount",readFailCount+"");
}else {
pro.setProperty(lineName+"_countnum_readFail"+caseid, readFailCount+"");
FileWriter fw = new FileWriter(countFile);
pro.store(fw, "");
fw.close();
jsonObject.getJSONArray("data").getJSONObject(i).put("readFailCount",readFailCount+"");
}
}
}
//本次執(zhí)行case损趋,結(jié)果為成功,將計數(shù)清零存儲至計數(shù)文件
}else {
caseFailCount=0;
readFailCount=0;
pro.setProperty(lineName+"_countnum_caseFail"+caseid, caseFailCount+"");
pro.setProperty(lineName+"_countnum_readFail"+caseid, readFailCount+"");
FileWriter fw = new FileWriter(countFile);
pro.store(fw, "");
fw.close();
jsonObject.getJSONArray("data").getJSONObject(i).put("caseFailCount",caseFailCount+"");
jsonObject.getJSONArray("data").getJSONObject(i).put("readFailCount",readFailCount+"");
};
}
if(caseFailFlag==1) {
send("caseFail",lineName,jsonObject,mailMap);
if(smsValue.equals("true")) {
// 發(fā)送短信
}
}
if(readFailFlag==1) {
send("readFail",lineName,jsonObject,mailMap);
}
out.write(jsonObject.toString());
} catch (Exception e) {
e.printStackTrace();
out.println(e.toString());
}
out.close();