前天接到了一個測試任務,要求測試一下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:替換整個頁面。
