Loadrunner 11 测试API的性能,java vuser 模拟Post,Get 请求
本例模拟测试 3个API(1.用户登录,2. 一个获得频道列表3. 获得频道详细信息);业务逻辑是:用户登录获得token,通过token,请求频道列表,获取频道为4的频道详情
Action部分代码如下(注意 是java vuser脚本):
import lrapi.lr; import lrapi.web; public class Actions { String htmlBody; public int init() throws Throwable { return 0; } public int action() throws Throwable { // 手动关联,获得登录成功后的token web.reg_save_param("xtoken",new String[]{ "LB=token\":\"", "RB=\",", "LAST"}); // 1. 发送 POST请求(用户登录API) web.custom_request("ViewStreamLogin", "Method=POST", new String[]{ "URL=http:/***.****.com/1/login?user_name=639126504508&password=A1111111", htmlBody, "TargetFrame=", "LAST"}); lr.log_message(lr.eval_string("<xtoken>")); // 打印获得的token值 // 加入header需要token等 web.add_header("X-Auth-Token",lr.eval_string("<xtoken>")); web.add_header("X-User-Agent","ANDROID/Nexus 5/Android###"); // 2. 发送 Get请求(频道列表API) web.custom_request("ViewStreamGetChannelList", "Method=GET", new String[]{"URL=http://***.****.com/1/smart/live", "LAST"}); // 加入header需要token等(注意: 每个API请求浅表都要header,不然会报错) web.add_header("X-Auth-Token",lr.eval_string("<xtoken>")); web.add_header("X-User-Agent","ANDROID/Nexus 5/Android###"); // 3. 发送Get请求(获得频道4详细内容API) web.custom_request("ViewStreamGetLiveChannelDetail", "Method=GET", new String[]{"URL=http://portal2.viewstream.ph/1/smart/live/4", "LAST"}); return 0;}//end of action public int end() throws Throwable { return 0; }//end of end }
注意问题:
1. 需要导入 lrapi.web 包
2. java vuser 脚本和c语言有区别,个人觉得java更灵活
3. 在手动关联时,对于特殊字符,前边需要转义,方法 前边加 \
转义字符总结
在做手动关联时,取边界值的时候,会经常用到转义字符,现将转义字符整理如下:
\b 退格
\f 换页
\n 换行
\r 回车
\t 水平制表
\v 垂直制表
\\ 反斜杠
\? 问号字符
\' 单引号字符
\" 双引号字符
\0 空字符