用戶訪問某個網站或軟件,一般不會不停地做個各種操作,例如一次查詢,用戶需要時間查看查詢的結果是否是自己想要的。例如一次訂單提交,用戶需要時間核對自己填寫的信息是否正確等。
也就是說用戶在做某些操作時,是會有停留時間的,我把這個時間叫思考時間。但利用代碼去執行的時候是沒有時間的,當然,腳本運行本身是需要時間的,但比起人的思考時間要小很多。這也是我們為什么要用軟件來代替人的某些工作。
但有時候,我們在做性能測試時,為了更真實的模擬用戶的操作,需要給代碼加入思考時間。來看看在loadrunner是如何設置思考時間的。
打開loadrunner 的Virtual User Generator
菜單欄:Vuser ---Run-Time settings...,切換到Thark Time選項
好吧!為了更好的理解這個界面上設置,現在我們通過一個腳本來分析。
Action() { double trans_time; //定義變量 web_url("www.baidu.com", "URL=http://www.baidu.com/", "Resource=0", "RecContentType=text/html", "Referer=", "Snapshot=t1.inf", "Mode=HTML", LAST); web_url("i-1.0.0.png", "URL=http://s1.bdstatic.com/r/www/img/i-1.0.0.png", "Resource=1", "RecContentType=image/png", "Referer=http://www.baidu.com/", "Snapshot=t2.inf", LAST); web_url("favicon.ico", "URL=http://www.baidu.com/favicon.ico", "Resource=1", "RecContentType=image/x-icon", "Referer=", "Snapshot=t3.inf", LAST); web_url("su", "URL=http://suggestion.baidu.com/su?wd=&cb=window.bdsug.sugPreRequest&sid=1466&t=1362316450913", "Resource=1", "RecContentType=baiduapp/json", "Referer=http://www.baidu.com/", "Snapshot=t4.inf", LAST); lr_start_transaction("注冊"); //添加事務 lr_think_time(20); //設置思考時間 web_url("v.gif", "URL=http://nsclick.baidu.com/v.gif?pid=201&pj=www&rsv_sid=1466&fm=behs&tab=tj_reg&un=&path=http%3A%2F%2Fwww.baidu.com%2F&t=1362316485456", "Resource=1", "Referer=http://www.baidu.com/", LAST); web_url("favicon.ico_2", "URL=https://passport.baidu.com/favicon.ico", "Resource=1", "RecContentType=application/octet-stream", "Referer=", "Snapshot=t5.inf", LAST); web_link("注冊", "Text=注冊", "Snapshot=t6.inf", LAST); web_custom_request("urs.asmx", "URL=https://urs.microsoft.com/urs.asmx?MSURS-Client-Key=WI0pAZHPdb%2b3UDOD0AtzxA%3d%3d&MSURS-Patented-Lock=S1IpDfNCCC4%3d", "Method=POST", "Resource=0", "RecContentType=text/xml", "Referer=", "Snapshot=t7.inf", "Mode=HTML", "EncType=text/xml; charset=utf-8", "Body=<RepLookup v=\"3\"><G>ED8654D5-B9F0-4DD9-B3E8-F8F560086FDF</G><O>F03F2D77-79E1-4DEC-BBF8-81A5C0790160</O><D>9.0.8110.0</D><C>9.00.8112.16421</C><OS>6.1.7601.1.0</OS><I>9.0.8112.16421</I><L>zh-CN</L><R><Rq><URL>aHR0cHM6Ly9wYXNzcG9ydC5iYWlkdS5jb20vdjIvP3JlZyZyZWdUeXBlPTEmdHBsPW1uJnU9aHR0cCUzQSUyRiUyRnd3dy5iYWlkdS5jb20lMkY=</URL><O>PRE</O><T>TOP</T><HIP>220.181.111.48</HIP></Rq></R></RepLookup>", EXTRARES, "Url=https://passport.baidu.com/img/topbarnav_bg.png", "Referer=https://passport.baidu.com/v2/?reg®Type=1&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F", ENDITEM, "Url=https://passport.baidu.com/passApi/js/reg_6e270622.js", "Referer=https://passport.baidu.com/v2/?reg®Type=1&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F", ENDITEM, "Url=https://passport.baidu.com/passApi/img/small_blank_9dbbfbb1.gif", "Referer=https://passport.baidu.com/v2/?reg®Type=1&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F", ENDITEM, LAST); web_url("api", "URL=https://passport.baidu.com/v2/api/?getapi&tpl=mn&apiver=v3&tt=1362316491971&class=reg&callback=bd__cbs__c93h6w", "Resource=0", "RecContentType=text/html", "Referer=https://passport.baidu.com/v2/?reg®Type=1&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F", "Snapshot=t8.inf", "Mode=HTML", EXTRARES, "Url=/img/v.gif?type=1®Type=mail", "Referer=https://passport.baidu.com/v2/?reg®Type=1&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F", ENDITEM, LAST); web_url("api_2", "URL=https://passport.baidu.com/v2/api/?getapi&tpl=mn&apiver=v3&tt=1362316491978&class=regPhone&callback=bd__cbs__xs2rv5", "Resource=0", "RecContentType=text/html", "Referer=https://passport.baidu.com/v2/?reg®Type=1&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F", "Snapshot=t9.inf", "Mode=HTML", EXTRARES, "Url=/cgi-bin/genimage?001362316472015690A3686612594D3D2C14ABF30D0B432CF4462DF3A4B5A8E5E7B7EE094DCAB428311C96E23B35A3CB73A67533699084E25A27CF6C9745497B5C5435611E0593A615CC284B27D70CA5FFB5F771E5D7DC9EE266F6FD61A2D88FEC7B7BA20EFF5DE2CD301E011E634D5063BDDCD35A47C1EB7B5EF365B347DD5800C612B744FAEF845540239F1036CC800902957BD17F36F6547B71C198C154162F8F71077B935E6B", "Referer=https://passport.baidu.com/v2/?reg®Type=1&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F", ENDITEM, "Url=/img/bd-split.gif", "Referer=https://passport.baidu.com/v2/?reg®Type=1&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F", ENDITEM, "Url=/img/reg_tip_header_t.gif", "Referer=https://passport.baidu.com/v2/?reg®Type=1&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F", ENDITEM, "Url=/img/reg_tip_bg_bottom.gif", "Referer=https://passport.baidu.com/v2/?reg®Type=1&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F", ENDITEM, LAST); trans_time=lr_get_transaction_wasted_time("注冊"); //獲得消耗時間 if (trans_time) { lr_output_message("The duration up to the submit is %f seconds",trans_time); //打印輸出消耗實時間 }else{ lr_output_message("the duration cannot be determined. "); } lr_end_transaction("注冊",LR_AUTO); //事務結束 return 0; }
腳本有點長,但不要被嚇到了,重要的幾句我已經加了注釋。你只用關注那幾行就行了。
lgnore think time :忽視思考時間,也就說勾選這一項的時候 ,你腳本中加入的lr_think_time 函數設置是無效的。選中這一項,運行上面的腳本。
Action.c(62): The duration up to the submit is 0.032043 seconds Action.c(67): Notify: Transaction "注冊" ended with "Pass" status (Duration: 12.4113 Wasted Time: 0.0320).
雖然我在腳本中加入了lr_think_time(20); 這條語句,顯現沒起到任何作用。
Replay think time:回放思考時間
來看第一個選項:
As recorded : 取決於記錄,選種這一項再來運行上面的腳本。
Action.c(111): The duration up to the submit is 0.034508 seconds Action.c(116): Notify: Transaction "注冊" ended with "Pass" status (Duration: 50.4453 Think Time: 20.0000 Wasted Time: 0.0345). Ending action Action.
運行結果中多了一個 Think Time: 20.0000 ;這說明我們腳本中的lr_think_time(20); 起到了作用,選中這一項后,我們可以在腳本中任意的添加思考時間。
Multiply recorded think time by: 錄制思考時間乘以,使用錄制思考時間的倍數。
其實,在我們腳本錄制的過程中,根據我們的操作,腳本中就可以生成相應的思考時間。這個參數是根據腳本中的思考時間進行調整的。
例如:腳本是20秒,輸入2,那么在腳本實際運行時,思考時間將為40秒;輸入0.5 ,腳本在運行時的思考時間為10秒。下面選中此項,輸入0.5,再來查看腳本運行結果:
Action.c(111): The duration up to the submit is 0.036610 seconds Action.c(116): Notify: Transaction "注冊" ended with "Pass" status (Duration: 131.2022 Think Time: 9.9998 Wasted Time: 0.0366).
注:將原諒腳本會有0.0002秒的誤差。
Use random percentage of recorded think time : 使用錄制思考時間的百分比。
通過指定思考的時間的范圍,可以設置思考時間值的范圍。假如思考時間為20秒,這里設置的范圍為50% 到100% ,那么腳本運行時候將隨即的在這個范圍(10秒到20秒之間)內取值。下面再來查看腳本運行時間:
Action.c(111): The duration up to the submit is 0.036191 seconds Action.c(116): Notify: Transaction "注冊" ended with "Pass" status (Duration: 40.8002 Think Time: 15.0050 Wasted Time: 0.0362).
這一次Think Time: 為15.0050 ,是在10到20秒的范圍之內,你再次運行會發現這個時間不一樣,但不會超出設定的范圍。
Limit think time to: 限制思考時間的最大值。
假如腳本設置的思考時間為20秒,選中這一項並設置為10秒。那么腳本在運行時,思考時間不會超過10秒。也就是說這設置同樣也制約前面幾個選項。這個你可以自行設置驗證。
感謝:
double trans_time; //定義變量 ...... lr_start_transaction("注冊"); //添加事物 lr_think_time(8); //思考時間 ....... trans_time=lr_get_transaction_wasted_time("注冊"); //獲得消耗時間 if (trans_time) { lr_output_message("The duration up to the submit is %f seconds",trans_time); //打印輸出消耗實時間 }else{ lr_output_message("the duration cannot be determined. "); } lr_end_transaction("注冊",LR_AUTO); //事務結束
..........
感謝上面這段代碼很好幫助我們理解思考時間。
備注:添加思考時間可以更真實的模擬用戶行為,但它同時降低了用戶並發。也就是說思考時間越長,對服務器的壓力會越小。