最近使用LoadRunner 11進行了一次完整的WEB接口性能測試。
腳本基本流程如下:首先定義了一個參數保存請求返回碼,之后調用Get / Post方法,最后通過判斷返回碼來定義事務成功或失敗。
完整的Action腳本見本文下方附錄,簡單解析如下(具體函數的詳細解釋可百度或查看LoadRunner幫助文件):
這個函數注冊一個請求,以在檢索到的網頁中查找並保存一個文本字符串。它只有在執行了下一個操作函數(如web_url)后才會執行。
語法:
int web_reg_save_param(const char *ParamName, <list of Attributes>, LAST);
參數說明:
1) ParamName: 存放得到的動態內容的參數名稱
2) LB和RB (必傳):要抓取文本的左/右邊的內容
注意:
1) web_reg_save_param必須在獲取返回值的操作前面注冊,在獲取返回值的操作后面使用
2) 保存參數最大不能超過256字節,如果超過256字節請使用int web_set_max_html_param_len (const char *length )函數擴大參數保存范圍。
如:web_set_max_html_param_len (1024); //擴大參數最大保存范圍為1024字節
3) LB和RB后面跟着"/ic",則邊界大小寫都匹配(不加,即默認大小寫敏感)
如:web_reg_save_param("IsRight","LB/ic=cache-control: private\r\n\r\n","RB/ic=|",LAST);
2. web_url
發送Http GET請求函數,比較簡單,一般只需修改url地址即可
WEB表單提交函數,它用來生成表單的GET或POST請求,一般修改URL地址、請求方式(POST/GET)、參數名稱和參數值即可
4. 腳本中還有lr_start_transaction,根據判斷返回碼來lr_end_transaction pass或fail的常用方法,在此恕不贅述
完整腳本如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
Action()
{
lr_start_transaction("TestGet");
web_reg_save_param("TGCode",
"LB/IC={\"returnCode\":\"",
"RB/IC=\",\"",
LAST);
web_url("TestGet",
"Resource=0",
"RecContentType=text/html",
"Mode=HTML",
LAST);
if((strstr(lr_eval_string("{TGCode}"),"0000"))==NULL)
{
lr_end_transaction("TestGet", LR_FAIL);
lr_error_message("TestGet Failed! ReturnCode:%s",lr_eval_string ("{TGCode}"));
}
else
{
lr_end_transaction("TestGet", LR_PASS);
lr_output_message("TestGet Sucess! ReturnCode:%s",lr_eval_string ("{TGCode}"));
}
lr_start_transaction("TestPost");
web_reg_save_param("TPCode",
"LB/IC={\"returnCode\":\"",
"RB/IC=\",\"",
LAST);
web_submit_data("TestPost",
"Action=http://192.168.1.1:8080/TestPost",
"Method=POST",
"RecContentType=text/html",
"Mode=HTML",
ITEMDATA,
"Name=param1", "Value=param1", ENDITEM,
"Name=param2", "Value=param2", ENDITEM,
LAST);
if((strstr(lr_eval_string("{TPCode}"),"0000"))==NULL)
{
lr_end_transaction("TestPost", LR_FAIL);
lr_error_message("TestPost Failed!ReturnCode: %s",lr_eval_string ("{TPCode}"));
}
else
{
lr_end_transaction("TestPost", LR_PASS);
lr_output_message("TestPost Sucess! ReturnCode: %s",lr_eval_string ("{TPCode}"));
}
return 0;
}
|
http://lovesoo.org/loadrunner-getpost-performance-test-script-parsing.html