前天接到了一个测试任务,要求测试一下ES(elsticsearch)在不同并发下的查询效率。如图:
业务场景是在客户端根据具体车牌查询相关车辆信息,结果返回前10条记录。 从图中可以看到,接口的请求参数和返回结果均是JSON字符串,请求可以用POST或者GET方法。先说GET方法:
一、GET方法测试
- Insert - New step -选择Custom Request - web_url
- 填入相应参数
- 生成脚本,并修改如下
Action() { //添加集合点 lr_rendezvous("jihedian"); lr_start_transaction("getTop10"); //插入检查点,检查返回值是否包含kakoTypeName web_reg_find( "Search=Body", "Text=kakoTypeName", LAST ); //发送get请求 web_url("www.abc.com", "URL=http://192.168.3.33:9200/_search?{%22query%22:{%22bool%22:{%22must%22:[{%22term%22:{%22plateNumNond%22:%22%E9%B2%81{NewParam}%22}}],%22must_not%22:[],%22should%22:[]}},%22from%22:0,%22size%22:10,%22sort%22:[],%22aggs%22:{}}", "TargetFrame=", "Resource=0", "RecContentType=application/json", "Snapshot=t1.inf", "Mode=HTML", LAST ); lr_end_transaction("getTop10", LR_AUTO); //打印本次取的车牌号 lr_output_message( "the platenum is #%s", lr_eval_string( "{NewParam}" ) ); return 0; }
4. 查看返回结果
Virtual User Script started at : 2016-09-22 14:16:53 Starting action vuser_init. Web Turbo Replay of LoadRunner 11.0.0 for Windows 7; build 8859 (Aug 18 2010 20:14:31) [MsgId: MMSG-27143] Run Mode: HTML [MsgId: MMSG-26000] Run-Time Settings file: "F:\PassCarSearch\ESqueryByPlateNumNond_GET\\default.cfg" [MsgId: MMSG-27141] Ending action vuser_init. Running Vuser... Starting iteration 1. Starting action Action. Action.c(4): Rendezvous jihedian Action.c(5): Notify: Transaction "getTop10" started. Action.c(7): Registering web_reg_find was successful [MsgId: MMSG-26390] Action.c(12): Registered web_reg_find successful for "Text=kakoTypeName" (count=10) [MsgId: MMSG-26364] Action.c(12): web_url("www.abc.com") was successful, 9058 body bytes, 88 header bytes [MsgId: MMSG-26386] Action.c(20): Notify: Transaction "getTop10" ended with "Pass" status (Duration: 3.1371 Wasted Time: 0.4776). Action.c(22): the platenum is #UT5387 Ending action Action. Ending iteration 1.
说明:
- 返回结果中可以看到检查点和事务都成功了,表明我们的脚本编写无误。
- 查看服务器返回的结果需在Vuser-Runtime-settings的log选项下,勾选Enable-logging、Extended log、Data returned by server 。
二、POST方法测试
在用POST方法创建脚本时遇到了点波折——先是使用了函数web_submit_date,执行时报错,查询资料没找到原因,不知道是不是该函数不支持JSON串,有兴趣的可以自己试下。然后尝试用web_custom_request函数,执行后返回的结果都正确,ok,就它了。
- Insert - New step -选择Custom Request - web_custom_request
- 填入相应参数
- 生成脚本,并修改如下(参数中的引号"前需要加斜杠\转译)
Action() { lr_start_transaction("querybypost"); //插入检查点,检查返回值是否包含t_query_data web_reg_find( "Text=max_score", LAST ); web_custom_request("querybypost", //VuGen中树形视图中显示的名称 "Url=http://192.168.3.33:9200/_search", //请求url "Method=POST", "Resource=0", "Mode=HTTP", //请求方式 "Referer=", "EncType=application/json", //指定响应头的Content-Type,这里是JSON "RecContentType=application/json", //指定请求头的Content-Type,这里是JSON "Body={\"query\":{\"bool\":{\"must\":[{\"term\":{\"plateNumNond\":\"<PlateNumNond>\"}}],\"must_not\":[],\"should\":[]}},\"from\":0,\"size\":10,\"sort\":[],\"aggs\":{}}:", //body的内容 LAST); lr_end_transaction("querybypost", LR_AUTO); lr_output_message( "PlateNumNond on iteration #%s", lr_eval_string( "<PlateNumNond>" ) ); }
4. 查看返回结果
Virtual User Script started at : 2016-09-21 16:40:04 Starting action vuser_init. Web Turbo Replay of LoadRunner 11.0.0 for Windows 7; build 8859 (Aug 18 2010 20:14:31) [MsgId: MMSG-27143] Run Mode: HTML [MsgId: MMSG-26000] Run-Time Settings file: "F:\PassCarSearch\ESqueryByPlateNumNond_POST\\default.cfg" [MsgId: MMSG-27141] Ending action vuser_init. Running Vuser... Starting iteration 1. Starting action Action. Action.c(6): Notify: Transaction "querybypost" started. Action.c(9): Registering web_reg_find was successful [MsgId: MMSG-26390] Action.c(14): Warning: The string '"plateNumNond":"B23456"' with parameter delimiters is not a parameter. Action.c(14): Warning: The string '' with parameter delimiters is not a parameter. Action.c(14): t=770ms: 87-byte response headers for "http://192.168.3.33:9200/_search" (RelFrameId=1, Internal ID=1) Action.c(14): HTTP/1.1 200 OK\r\n Action.c(14): Content-Type: application/json; charset=UTF-8\r\n Action.c(14): Content-Length: 124\r\n Action.c(14): \r\n Action.c(14): t=808ms: 124-byte response body for "http://192.168.3.33:9200/_search" (RelFrameId=1, Internal ID=1) Action.c(14): {"took":9,"timed_out":false,"_shards":{"total":40,"successful":40,"failed":0},"hits":{"tot Action.c(14): al":0,"max_score":null,"hits":[]}} Action.c(14): Error -26366: "Text=t_query_data" not found for web_reg_find [MsgId: MERR-26366] Action.c(14): web_custom_request("querybypost") highest severity level was "ERROR", 124 body bytes, 87 header bytes [MsgId: MMSG-26388] Action.c(14): Notify: Transaction "querybypost" ended with "Fail" status (Duration: 0.9686 Wasted Time: 0.7094). Ending action Action. Ending iteration 1. Ending Vuser... Starting action vuser_end. Ending action vuser_end. Vuser Terminated.
好吧,报错了。返回结果中的提示是Warning: The string '"plateNumNond":"B23456"' with parameter delimiters is not a parameter.
本着有问题找度娘的一贯态度,将这句话复制到度娘中检索,但结果不遂人愿,没有查询到解决思路。中间不断的思考是不是自己的代码出了问题,但最终把怀疑一一排除。后来突然想到在loadrunner中,参数化的标志是{},我在body里面的{}并不是参数化,而是json的格式。。。终于找到原因了,接下来就简单了,只需在Tool - General Options - Parameterization 中将Parameter Braces 改为<>即可,如图
5. 重新运行,查看结果。 看到以下结果,ok,搞定,收工!
Virtual User Script started at : 2016-09-21 17:49:10 Starting action vuser_init. Web Turbo Replay of LoadRunner 11.0.0 for Windows 7; build 8859 (Aug 18 2010 20:14:31) [MsgId: MMSG-27143] Run Mode: HTML [MsgId: MMSG-26000] Run-Time Settings file: "F:\PassCarSearch\ESqueryByPlateNumNond_POST\\default.cfg" [MsgId: MMSG-27141] Ending action vuser_init. Running Vuser... Starting iteration 1. Starting action Action. Action.c(3): Notify: Transaction "querybypost" started. Action.c(6): Registering web_reg_find was successful [MsgId: MMSG-26390] Action.c(11): Notify: Parameter Substitution: parameter "PlateNumNond" = "鲁UTR294" Action.c(11): t=1729ms: 87-byte response headers for "http://192.168.3.33:9200/_search" (RelFrameId=1, Internal ID=1) Action.c(11): HTTP/1.1 200 OK\r\n Action.c(11): Content-Type: application/json; charset=UTF-8\r\n Action.c(11): Content-Length: 124\r\n Action.c(11): \r\n Action.c(11): t=1885ms: 124-byte response body for "http://192.168.3.33:9200/_search" (RelFrameId=1, Internal ID=1) Action.c(11): {"took":2,"timed_out":false,"_shards":{"total":40,"successful":40,"failed":0},"hits":{"tot Action.c(11): al":0,"max_score":null,"hits":[]}} Action.c(11): Registered web_reg_find successful for "Text=max_score" (count=1) [MsgId: MMSG-26364] Action.c(11): web_custom_request("querybypost") was successful, 124 body bytes, 87 header bytes [MsgId: MMSG-26386] Action.c(22): Notify: Transaction "querybypost" ended with "Pass" status (Duration: 1.8611 Wasted Time: 0.6362). Action.c(24): Notify: Parameter Substitution: parameter "PlateNumNond" = "鲁UTR294" Action.c(24): PlateNumNond on iteration #鲁UTR294 Ending action Action. Ending iteration 1.
三、web_custom_request和web_submit_data区别
在解决问题的过程中查询了web_custom_request和web_submit_data区别,现附录如下:
- web_custom_request方法可以发送POST和GET类型的请求;
- web_submit_data只能发送POST类型的请求;
- 所有web_submit_data方法发送的请求都可以使用web_custom_request来实现
- web_custom_request可以实现web_submit_data无法实现的请求,比如“查询所有邮件并删除”这个案例中,查询时我们使用关联把所有邮件对应的标识抓取成一个数组,如果使用web_submit_data来完成这个删除的请求,需要很多个web_submit_data请求才能完成,但使用web_custom_request就可以通过一个请求完成,方法是自己写代码拼一个eb_custom_request
- 方法POST请求的Body值。
- web_submit_data
请求中提交的数据格式:“Name=属性名称,”,“Value=属性值”
例如:
"Name=username″,"Value=12044″, ENDITEM, "Name=password″,"Value=123456″, ENDITEM, "Name=typeId″,"Value=1″, ENDITEM,
如果想提交的某个属性包含包含多个值(比如说批量删除),单个web_submit_data就无法处理了,只能通过多个web_submit_data来处理。
2. web_custom_request
提交的数据(body)格式:“Body=属性名称=属性值&属性名称=属性值&……”
下面是一个典型的web_submit_data和web_custom_request请求,可以看到web_custom_request中提交的数据(body)是以这样的方式存在的,如下:
web_submit_data("searchRecvOrgsname", "Action=http://{url}/searchRecvOrgsname", "Method=POST", "TargetFrame=", "RecContentType=text/html", "Referer=http://{url}/login_wj;jsessionid={jsessionid}", "Snapshot=t18.inf", "Mode=HTML", ITEMDATA, "Name=orgsId", "Value={orgsId}", ENDITEM, "Name=code", "Value={order_end_station_code}", ENDITEM, LAST); web_custom_request("searchVehiclePopUp", "URL=http://{url}/searchVehiclePopUp", "Method=POST", "TargetFrame=", "Resource=0", "RecContentType=text/html", "Referer=http://{url}/login_wanjia;jsessionid={jsessionid}", "Snapshot=t19.inf", "Mode=HTML", "EncType=application/x-www-form-urlencoded; charset=UTF-8", "Body=&orgsId={orgsId}&order_start_station_id={order_start_station_id}&targetcode=order_truck_no&targetname=order_truck_name&targetid=order_truck_id", LAST);
两种情况下的POST请求会被LoadRunner录制为web_custom_request:
- 上文提到的批量提交多条同属性名称的数据的请求
- header属性x-requested-by值为XMLHttpRequest的POST请求
这两种实现请求的方法还有一个需要注意的地方就是web_custom_request中body中的属性值如果包含一些特殊字符,必须通过URL编码,否则Web服务器会返回500错误,一个典型的例子是如果Body中包含ViewState,ViewState中常常有“=”之类的特殊字符,此时必须通过URL编码,LoadRuner中提供了一个这样的编码转换函数:
web_convert_param(“vs1″,
“SourceEncoding=HTML”,“TargetEncoding=URL”, LAST);
3. web_custom_request函数详解
A.语法:
int web_custom_request( const char *RequestName, , [EXTRARES, ,] LAST );
B.返回值:返回LR_PASS(0)代表成功,LR_FAIL(1)代表失败。
C.参数:
(1)RequestName:步骤的名称,VuGen中树形视图中显示的名称。
(2)List of Attribute:属性列表,支持的属性有以下几种:
a. URL:页面地址。
b. Method:页面的提交方式,POST或GET。
c. EncType:编码类型。
d. TargetFrame:当前链接或资源所在Frame的名称。
除了Frame的名字,还可以指定下面的参数:
_BLANK:打开一个空窗口。
_PARENT:把最新更改过的的Frame替换为它的上级。
_SELF:替换最新更改过的的Frame。
_TOP:替换整个页面。