LR介面壓力測試指令碼範例
做過一段時間的介面測試任務,下面把一段介面測試的指令碼分享出來:
Action()
{
int rev=0;
web_service_call( "StepName=getPlanData_104",
"SOAPMethod=PlanDataServiceService|PlanDataServicePort|getPlanData",
"ResponseParam=response",
"Service=PlanDataServiceService",
"ExpectedResponse=SoapResult",
"Snapshot=t1450164946.inf",
BEGIN_ARGUMENTS,
"xml:request="
"<request>"
"<planId>1</planId>"
"<complexTypeName>PLANORDER</complexTypeName>"
"<pageIndex>1</pageIndex>"
"</request>",
END_ARGUMENTS,
BEGIN_RESULT,
"response=Response",
END_RESULT,
LAST);
rev =lr_save_searched_string(lr_eval_string("{Response}"),1500,0,"<responseXmlType=\"DynamicParameter\"><head>",0,2,"result");
if(rev>=0)
{
lr_output_message("交易成功!");
}
else
{
lr_output_message("交易失敗,返回值為:%s",lr_eval_string("{result}"));
}
return 0;
}
----------------------------------------------------------------------------------------
Action()
{
//首先呼叫web_reg_find()這個註冊函式,我們介面的正常返回結果都是<Code>0</Code>
web_reg_find("Search=Body", //定義查詢範圍
"SaveCount=result", //定義查詢計數變數名稱
"Text=<Code>0</Code>", //定義查詢內容
LAST);
//Name後面的值都是介面引數值,可以參考開發人員提供的介面設計文件或者錄製指令碼獲得
//介面的引數值一般都要引數化,儘可能的模擬真是場景
"Name=userid","Value={NewParam}",ENDITEM,
"Name=snsid","Value=6000",ENDITEM,
"Name=username","Value={myUserName}",ENDITEM,
LAST);
//剛才的web_reg_find()註冊函式派上用處,可以對結果進行統計
if (atoi(lr_eval_string("{result}")) > 0) //判斷如果<Code>0</Code>
//字串出現次數大於0
lr_output_message("Log on successful."); //在日誌中輸出Log
on successful
else
//如果出現次數小於等於
lr_error_message("Log on failed"); //在日誌中輸出Log on failed
return 0;
}
-------------------------------------------------------------------------------------------------------3例------------------
描述:這是一個介面壓力測試,呼叫JAVA編寫的一個函式,對返回值進行校驗,程式碼如下:
import lrapi.lr;
import com.lr.test.*;
public class Actions
{
public int init() throws Throwable {
return 0;
}//end of init
public int action() throws Throwable {
String email ="<email>";
String address_url ="http://192.168.1.12:8080/interface/index.php/v1/fun?email="+email+";
String result = "";
lr.rendezvous("ReadGO");
lr.start_transaction("FUN");
lr.start_sub_transaction("A","FUN");
lr.start_sub_transaction("B","FUN");
lr.start_sub_transaction("C","FUN");
result = test.getHttpByUrl(address_url);
//結果A
if(!test.getString(result,"NUM").equals("12") && test.getString(result,"Letter").equals("A"))
{
lr.end_transaction("FUN", lr.PASS);
lr.end_sub_transaction("A", lr.PASS);
lr.end_sub_transaction("C", lr.FAIL);
lr.end_sub_transaction("B",lr.FAIL);
lr.error_message(email);
/結果B
}
else if (test.getString(result,"NUM").equals("12") && test.getString(result,"Letter").equals("A"))
{
lr.end_transaction("FUN", lr.PASS);
lr.end_sub_transaction("A", lr.FAIL);
lr.end_sub_transaction("B",lr.PASS);
lr.end_sub_transaction("C", lr.FAIL);
lr.error_message(email);
//結果C
}
else if(test.getString(result,"Letter").equals("C"))
{
lr.end_transaction("FUN", lr.PASS);
lr.end_sub_transaction("A", lr.FAIL);
lr.end_sub_transaction("B",lr.FAIL);
lr.end_sub_transaction("C", lr.PASS);
lr.error_message(email);
}
else
{
lr.error_message(result);
lr.end_transaction("FUN", lr.FAIL);
lr.end_sub_transaction("A", lr.FAIL);
lr.end_sub_transaction("B",lr.FAIL);
lr.end_sub_transaction("C", lr.FAIL);
}
return 0;
}//end of action
public int end() throws Throwable {
return 0;
}//end of end
}
問題:
1、如果壓力只執行一次(Run until completion),通過事物數和列印email個數一致;如果壓力持續執行幾分鐘,列印email個數會大於通過的事物數,這是怎麼回事?
2、一般壓力測試只執行一次(Run until completion),還是持續執行一段時間呢?
3、對於我的程式碼,這樣做是否合適呢,有其他更好的方法嗎?
4、有跳過前端直接測試Redis處理能力的思路、方法或者例子嗎?