-->

Loadrunner Get&Post方法性能测试脚本解析

2020-12-04 03:25发布

最近使用LoadRunner 11进行了一次完整的WEB接口性能测试。

脚本基本流程如下:首先定义了一个参数保存请求返回码,之后调用Get / Post方法,最后通过判断返回码来定义事务成功或失败。

完整的Action脚本见本文下方附录,简单解析如下(具体函数的详细解释可百度或查看LoadRunner帮助文件):

1. web_reg_save_param

这个函数注册一个请求,以在检索到的网页中查找并保存一个文本字符串。它只有在执行了下一个操作函数(如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地址即可

3. web_submit_data

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",
"URL=http://192.168.1.1:8080/GetPage",
"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

标签: