使用Loadrunner分别测量服务器响应时间及页面加载时间
上一篇 / 下一篇 2013-08-06 10:10:16 / 个人分类:Loadrunner
在Action模块中编写如下脚本: Public Function timeCount(url) Set dom = CreateObject("InternetExplorer.Application") 'Create an IE object Set lr = CreateObject("LoadRunner.LrApi") 'Create LoadRunner object dom.Navigate(url) 'Open the specific URL time_start = Now() 'To obtain statistics at the beginneing of time timer_start = timer() 'Get the current time in milliseconds dom.visible = True ' Set IE can be seen While dom.busy or (dom.readyState<>4) 'dom.readyState -- Got the IE status; when IE busy or is loading the readyState<>4, got the time duration wscript.sleep 1 'Time interval 1 ms, if the relatively long time interval, then is likely to take less than a state value Select Case dom.readyState 'To determine the value of dom.readyState Case 0 'IE is not initialized time0 = Now() timer0 = timer() Case 1 'Is sending Request time1 = Now() timer1 = timer() Case 2 'Request has been sent to complete time2 = Now() timer2 = timer() Case 3 'Can receive the part of response data time_3 = Now() timer_3 = timer() Case 4 'Page is loaded time4 = Now() timer4 = timer() End select wend timeCount = "Statistics Start Time:"&start_time&vbcrlf&"time0:"&time0&vbcrlf&"time1:"&time1&vbcrlf&"time2:"&time2&vbcrlf&"time3():"&time3&vbcrlf&"time4:"&time4&vbcrlf&"To complete the initializtion of IE and send request:"&(timer1-timer_start)&"seconds"&vbcrlf&"To send the completion and acceptance of server-side part of the response data:"&(timer2-timer1)&"seconds"&vbcrlf&"100% to receive and complete the HTML content parsing:"&(timer4-timer2)&"seconds"&vbcrlf& "Total spent:"&(timer4-timer_start)&"seconds" End Function Public Function Action() result = timeCount(SITEURL) 'Returns running result MsgBox result 'Output to run As a result, the line can be commented out loadrunner ' For debugging, you can comment it out End Function |
wscript对象是windows宿主脚本对象,而且是windows系统默认提供的脚本操作对象,即无须重新创建就可以使用。所以你在vbs,js中可以直接使用wscript(或者wsh)的方法和属性,但这只是说你可以直接使用wscript,不代表wscript属于vbs。 尽管QTP引入了vbs,但由于wscript不属于vbs,所以QTP未引入wscript,使用时自然报错。 |
在 Action 函数前放置 Sleep 过程
Sub Sleep(MSecs) ' loadrunner can't use wscript.sleep directly, the following code is trying to create Wscript. object
Set fso = CreateObject("Scripting.FileSystemObject")
If Fso.FileExists("sleeper.vbs")=False Then
Set bjOutputFile = fso.CreateTextFile("sleeper.vbs", True)
objOutputFile.Write "wscript.sleep WScript.Arguments(0)"
objOutputFile.Close
End If
CreateObject("WScript.Shell").Run "sleeper.vbs " & MSecs,1 , True
End Sub |
将Loadrunner中的 wscript.sleep 1 改为调用 Sleep(1) |
Sub
Sleep(MSecs) ' loadrunner can't use wscript.sleep directly, the following code
is trying to create Wscript. object Set
fso = CreateObject("Scripting.FileSystemObject") If
Fso.FileExists("sleeper.vbs")=False Then Set
bjOutputFile = fso.CreateTextFile("sleeper.vbs", True) objOutputFile.Write
"wscript.sleep WScript.Arguments(0)" objOutputFile.Close End
If CreateObject("WScript.Shell").Run
"sleeper.vbs " & MSecs,1 , True End Sub Public Function timeCount(url) Set
dom = CreateObject("InternetExplorer.Application") 'Create an IE
object Set lr
= CreateObject("LoadRunner.LrApi") 'Create LoadRunner object
dom.Navigate(url) 'Open the specific URL
time_start = Now() 'To obtain statistics at the beginneing of time
timer_start = timer() 'Get the current time in milliseconds
dom.visible = True ' Set IE can be seen While
dom.busy or (dom.readyState<>4) 'dom.readyState -- Got the IE status;
when IE busy or is loading the readyState<>4, got the time duration Sleep(1) 'Time interval 1 ms, if the relatively long time interval, then is likely to take less than a state value
Select Case dom.readyState 'To determine the value of dom.readyState
Case 0 'IE is not initialized
time0 = Now()
timer0 = timer()
Case 1 'Is sending Request
time1 = Now()
timer1 = timer()
Case 2 'Request has been sent to complete
time2 = Now()
timer2 = timer()
Case 3 'Can receive the part of response data
time_3 = Now()
timer_3 = timer()
Case 4 'Page is loaded
time4 = Now()
timer4 = timer()
End select
wend server_response_transaction_timer = timer2 - timer1 ' Get the server page_render_transaction_timer = timer4-timer_2 total_timer = timer4 - timer_start lr.log_message "The server response time is " +
lr.eval_string(server_response_transaction_timer) lr.log_message "The page render time is " +
lr.eval_string(page_render_transaction_timer) lr.log_message "Total page load time is " +
lr.eval_string(total_timer) lr.set_transaction "server_response_time",
server_response_transaction_timer, lr.PASS lr.set_transaction "page_render_time",
page_render_transaction_timer, lr.PASS lr.set_transaction "total_page_load_time", total_timer, lr.PASS Public
Function Action() SITEURL
= "http://www.yourwebsite.com" result = timeCount(SITEURL) 'Returns running result 'MsgBox result 'Output to run As a result, the line can be commented out loadrunner End Function |
Sub Sleep(MSecs) ' loadrunner can't use wscript.sleep
directly, the following code is trying to create Wscript. object Set fso =
CreateObject("Scripting.FileSystemObject") If Fso.FileExists("sleeper.vbs")=False
Then Set bjOutputFile =
fso.CreateTextFile("sleeper.vbs", True) objOutputFile.Write
"wscript.sleep WScript.Arguments(0)" objOutputFile.Close End If CreateObject("WScript.Shell").Run
"sleeper.vbs " & MSecs,1 , True End Sub
Public Function timeCount(url)
Dim i i
= 0
Dim time_3(100)
Dim timer_3(100)
Set dom = CreateObject("InternetExplorer.Application") 'Create
an IE object
Set lr = CreateObject("LoadRunner.LrApi") 'Create LoadRunner
object
dom.Navigate(url) 'Open the specific URL time_start = Now() 'To obtain statistics at the beginneing of time
timer_start = timer() 'Get the current time in milliseconds
dom.visible = True ' Set IE can be seen
While dom.busy or (dom.readyState<>4) 'dom.readyState -- Got the
IE status; when IE busy or is loading the readyState<>4, got the time
duration
Sleep(1) 'Time interval 1 ms, if the relatively long time interval, then
is likely to take less than a state value
lr.log_message "The readyState is " + lr.eval_string(dom.readyState)
' For debugging, you can comment it out before running
Select Case dom.readyState 'To determine the value of dom.readyState Case 0
'IE is not initialized time0 = Now() timer0 = timer() Case 1 'Is sending Request time1 = Now() timer1 = timer() Case 2 'Request has been sent to complete time2 = Now() timer2 = timer() Case 3 'Can receive the part of response data time_3(i) = Now() ' When
dom.readystate is changed from 2 to 3, it means server has returned the
response data, so the first value of Now() and Timer() stands for the server
response time. timer_3(i) = timer() lr.log_message "when dom.readystate
= 3 "& cstr(i) + " " + lr.eval_string(time_3(i)) ' For
debugging, you can comment it out before running i = i+1 ' Because dom.reaystate = 3
may keep in a while Case 4 'Page is loaded time4 = Now() timer4 = timer() lr.log_message "4 start at
" + lr.eval_string(time4) ' For debugging, you can comment it out before
running End select
server_response_transaction_timer = timer_3(0) - timer1 ' Get the server
page_render_transaction_timer = timer4-timer_3(0)
total_timer = timer4 - timer_start
lr.log_message "The server response time is " +
lr.eval_string(server_response_transaction_timer)
lr.log_message "The page render time is " +
lr.eval_string(page_render_transaction_timer)
lr.log_message "Total page load time is " +
lr.eval_string(total_timer)
lr.set_transaction "server_response_time",
server_response_transaction_timer, lr.PASS
lr.set_transaction "page_render_time",
page_render_transaction_timer, lr.PASS
lr.set_transaction "total_page_load_time", total_timer,
lr.PASS End Function Public Function Action() SITEURL =
"http://www.yourwebsite.com" result = timeCount(SITEURL) 'Returns running
result 'MsgBox result 'Output to run As a result, the line can be commented out loadrunner End Function |
Public Function Terminate() Set
ws=CreateObject("wscript.shell") ws.run "cmd /c taskkill /f /im iexplore.exe",0 End Function |
TAG:
我的栏目
标题搜索
日历
|
|||||||||
日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
1 | 2 | 3 | 4 | 5 | 6 | ||||
7 | 8 | 9 | 10 | 11 | 12 | 13 | |||
14 | 15 | 16 | 17 | 18 | 19 | 20 | |||
21 | 22 | 23 | 24 | 25 | 26 | 27 | |||
28 | 29 | 30 |
我的存档
数据统计
- 访问量: 123501
- 日志数: 79
- 建立时间: 2008-02-14
- 更新时间: 2013-08-09