前段时间接触了一下loadrunner的接口性能测试,然后尝试了一下手动编写脚本,毕竟录制这种东西,不是每次都能通的,而且录制下来的脚本,通常是有很多其他杂七杂八的请求夹杂在中间,没有达到真正的压测接口的目的;
loadrunner默认的脚本是C语言的,百度之后发现其实它支持目前所有主流的语言,如:Java User、VB User、VB script User 、Javascript User等。
下面是尝试过的几种post接口的请求方式:
- Post接口,通过JSON方式请求:
int i;
game()
{
//赛事列表接口开始时间,时间戳
lr_save_string("","gameHoldBeginTime");
//赛事列表接口结束时间,时间戳
lr_save_string("","gameHoldEndTime");
//gameCode
//lr_save_string("1532122","gameCode");
//以下接口循环请求两次
for (i=0;i<2;i++)
{
/*胜负盘赛事列表接口*/
web_reg_save_param ("S_respond","LB=","RB=",LAST);
//获取赛事编码gameCode,提供给后续接口调用
web_reg_save_param("gameCode","LB=gameCode\":\"","RB=\"",LAST);
web_add_header("Content-Type","application/json; charset=UTF-8");
lr_start_transaction("listGames");
web_custom_request("listGames",
"URL={url}/common/listGames",
"Method=POST",
"Resource=0",
"RecContentType=application/json",
"Referer=",
"Snapshot=t3.inf",
"Mode=HTML",
"EncType=application/json; charset=UTF-8",
"Body={\"requestId\":\"{requestId}\",\"timestamp\":\"1523503209424\",\"version\":\"1.0.0\",\"clientId\":\"{clientId}\",\"accessToken\":\"{token}\",\"data\":{\"gameHoldEndTime\":\"{gameHoldEndTime}\",\"gameHoldBeginTime\":\"{gameHoldBeginTime}\",\"gameType\":\"ALL\"},\"sign\":\"{sign}\"}",
LAST);
lr_end_transaction("listGames", LR_AUTO);
//打印返回信息
lr_convert_string_encoding ( lr_eval_string ("{S_respond}"),LR_ENC_UTF8 ,NULL,"respond");
//打印转码后的响应报文
lr_output_message ( "\nlistGames :%s",lr_eval_string ("{respond}"));
//循环结束
}
}
- Post请求,使用from-data方式进行请求的时候,脚本的写法:
lr_start_transaction("saishiPost"); web_submit_data("saishiPost", "Action={url}/higame/saishiPost", "Method=POST", "TargetFrame=", "EncType=multipart/form-data",//使用的编码类型 "Referer=https://yqcqtest.100bei.com/higame/gamelist", "RecContentType=application/josn", "Mode=HTML", ITEMDATA, //下面编辑post请求的数据 "Name=gameState", "Value=WAITING", ENDITEM, //数据的name、value及结束符 "Name=riqi", "Value=2018-06-08", ENDITEM, //第二条数据的name、value及结束符 "Name=leagueCode", "Value=", ENDITEM, LAST); //结束事件 lr_end_transaction("saishiPost", LR_AUTO); //打印返回信息 lr_convert_string_encoding( lr_eval_string("{S_respond}"),LR_ENC_UTF8,NULL,"respond" ); //打印转码后的响应报文 lr_output_message("\nappInfo :%s", lr_eval_string("{respond}"));
- Get请求脚本编写方法,get请求还是比较简单的,只需要套用更改url,基本就能调通了;
/*情报接口*/ web_reg_save_param ("S_respond","LB=","RB=",LAST); //检查点 //web_reg_find("Search=Body","Text=200",LAST); web_add_header("Content-Type","application/json; charset=UTF-8"); lr_start_transaction("intelligences"); web_custom_request("intelligences", "URL={url}/guess-api-gateway/portal/admin/common/intelligences?pageSize=5", "Method=GET", "Resource=0", "Referer=", "Mode=HTTP", "EncType=application/json;charset=UTF-8", "Body=", LAST); lr_end_transaction("intelligences", LR_PASS); //打印返回信息 lr_convert_string_encoding( lr_eval_string("{S_respond}"),LR_ENC_UTF8,NULL,"respond" ); //打印转码后的响应报文 lr_output_message("\nintelligences :%s", lr_eval_string("{respond}"));
另外:使用lr_save_string方法,配置静态变量以及接口请求的字段参数。还可以使用判断语句,来区分读取不同的配置,达到更高的易维护性;