LoadRunner手动编写接口性能脚本(Post请求)

前段时间接触了一下loadrunner的接口性能测试,然后尝试了一下手动编写脚本,毕竟录制这种东西,不是每次都能通的,而且录制下来的脚本,通常是有很多其他杂七杂八的请求夹杂在中间,没有达到真正的压测接口的目的;

loadrunner默认的脚本是C语言的,百度之后发现其实它支持目前所有主流的语言,如:Java User、VB User、VB script User 、Javascript User等。

 

前方高能!!!!一大批代码到来!!非战斗人员请快速撤离!!!

 下面是尝试过的几种post接口的请求方式:

Action()

{

//参数化;

lr_save_string("0b5628f2326657017903hhg", "requestId");

lr_save_string("Android", "platform");

lr_save_string("a8ika09", "deviceId");

lr_save_string("150173375600", "timestamp");

lr_save_string("official", "memberChannel");

lr_save_string("v1.0.0", "version");

//保存下面接口返回的参数;

web_reg_save_param ("S_respond","LB=","RB=",LAST);

//获取appKey并保存为参数变量

web_reg_save_param ("appKey","LB=appKey","RB=}",LAST);

web_reg_save_param("Connection","LB=Connection","RB=X",LAST);

//获取headers参数

web_save_header(REQUEST, "RequestHeader");// REQUEST为内置变量,保存请求的头信息,需在发送URL请求前注册使用,将请求头信息存入字符串RequestHeader中

web_save_header(RESPONSE, "ResponseHeader");// 将RESPONSE保存响应的头信息在字符串ResponseHeader中

web_add_header("Content-Type","application/json; charset=UTF-8");

lr_start_transaction("GetAPPKey");

web_custom_request("app-keys",

"URL=http://14.119.110.171/cbclient/app-keys",

"Method=POST",

"TargetFrame=",

"RecContentType=text/html",

"Resource=0",

"Referer=",

"Mode=HTML",//HTML或者HTTP都可以

//"EncType=application/x-www-form-urlencoded", //编码类型

//"EncType=application/json",         //指定响应头的Content-Type,这里是JSON

//"RecContentType=application/json",      //指定请求头的Content-Type,这里是JSON

//"Snapshot=t10.inf",

"Body={"

"\"requestId\":\"{requestId}\","

"\"platform\":\"{platform}\","

"\"deviceId\":\"{deviceId}\","

"\"timestamp\":\"{timestamp}\","

"\"memberChannel\":\"{memberChannel}\","

"\"version\":\"{version}\""

"}",

//"Body=&requestId=aimaii1212u19283nfna&platform=Android&deviceId=a8ika09109×tamp=8291377319798173&memberChannel=official&version=v1.0",此传参方法不work。

LAST);

lr_end_transaction("GetAPPKey", LR_AUTO);

//web_find ("Text=\"ret\":true",LAST);

//对响应报文进行转码

lr_convert_string_encoding ( lr_eval_string ("{S_respond}"),LR_ENC_UTF8 ,NULL,"respond");

//打印转码后的响应报文

lr_output_message ( "all respond is %s",lr_eval_string ("{respond}"));

lr_output_message ( "HEANDER IS %s",lr_eval_string ("{RequestHeader}"));

lr_output_message ( "HEANDER IS %s",lr_eval_string ("{ResponseHeader}"));

lr_output_message ( "\n\nConnection IS %s",lr_eval_string ("{Connection}"));

lr_start_transaction("login");

web_reg_save_param ("S_respond","LB=","RB=",LAST);

web_add_header("Content-Type","application/json; charset=UTF-8");

lr_save_string("0b5628f2326657017903190016", "requestId");

web_custom_request("login",

"URL=http://14.119.110.171/cbclient/user/login/mobile",

"Method=POST",

"TargetFrame=",

"RecContentType=text/html",

"Resource=0",

"Referer=",

"Mode=HTML",//HTML或者HTTP都可以

//"EncType=application/x-www-form-urlencoded", //编码类型

//"EncType=application/json",         //指定响应头的Content-Type,这里是JSON

//"RecContentType=application/json",      //指定请求头的Content-Type,这里是JSON

//"Snapshot=t10.inf",

"Body={"

"\"requestId\":\"{requestId}\","

"\"platform\":\"{platform}\","

"\"deviceId\":\"{deviceId}\","

"\"timestamp\":\"{timestamp}\","

"\"memberChannel\":\"{memberChannel}\","

"\"version\":\"{version}\","

"\"appKey\":\"{appKey}\","

"\"sign\":\"1234\","

"\"data\":{\"mobile\":\"{mobile}\",\"password\":\"123456\"}"

"}",

LAST);

lr_convert_string_encoding ( lr_eval_string ("{S_respond}"),LR_ENC_UTF8 ,NULL,"respond");

//打印转码后的响应报文

lr_output_message ( "%s",lr_eval_string ("{respond}"));

lr_log_message("\n\requestId is :%s",lr_eval_string("{requestId}"));

lr_output_message ( "appKey is %s",lr_eval_string ("{appKey}"));

lr_end_transaction("login", LR_AUTO);

/*

lr_start_transaction("query");

web_reg_save_param ("S_respond","LB=","RB=",LAST);

web_add_header("Content-Type","application/json; charset=UTF-8");

web_custom_request("query",

"URL=http://14.119.110.171/cbclient/",

"Method=POST",

"TargetFrame=",

"RecContentType=text/html",

"Resource=0",

"Referer=",

"Mode=HTML",//HTML或者HTTP都可以

//"EncType=application/x-www-form-urlencoded", //编码类型

//"EncType=application/json",         //指定响应头的Content-Type,这里是JSON

//"RecContentType=application/json",      //指定请求头的Content-Type,这里是JSON

//"Snapshot=t10.inf",

"Body={"

"\"requestId\":\"{requestId}\","

"\"platform\":\"{platform}\","

"\"deviceId\":\"{deviceId}\","

"\"timestamp\":\"{timestamp}\","

"\"memberChannel\":\"{memberChannel}\","

"\"version\":\"{version}\","

"\"appKey\":\"{appKey}\","

"\"sign\":\"1234\","

"\"accessToken\":\"{accessToken}\"

"}",

LAST);

lr_convert_string_encoding ( lr_eval_string ("{S_respond}"),LR_ENC_UTF8 ,NULL,"respond");

//打印转码后的响应报文

lr_output_message ( "%s",lr_eval_string ("{respond}"));

lr_log_message("\n\nusername is :%s",lr_eval_string("{requestId}"));

lr_end_transaction("query", LR_AUTO);

*/

return 0;

}

作者:Testin云测
链接:https://www.jianshu.com/p/c7a736531aee
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值