【发布时间】:2023-03-10 07:19:01
【问题描述】:
为了测量网站性能,我创建了一个简单的日志记录 sub,以查看在执行经典 asp 页面期间哪些部分较慢。
sub log(logText)
dim fs, f
set fs = Server.CreateObject("Scripting.FileSystemObject")
set f = fs.OpenTextFile("log.txt", 8, true)
f.WriteLine(now() & " - " & logText)
f.Close
set f = Nothing
set fs = Nothing
end sub
log "Loading client countries"
set myR = Server.CreateObject("ADODB.RecordSet")
myR.ActiveConnection = aConnection
myR.CursorLocation=2
myR.CursorType=3
myR.LockType=2
myR.Source = "SELECT * FROM CC ORDER BY ccName ASC"
log "Opening db connection"
myR.open()
log "Opened db connection"
以下是结果(仅显示时间部分):
...
11:13:01 - Loading client countries
11:13:06 - Opening db connection
11:13:06 - Opened db connection
...
创建ADODBRecordSet 并设置一些属性大约需要 5 秒。这并不总是发生,有时代码执行得很快,但通常在我重新加载页面时
几分钟后,加载时间或多或少与示例相同。这真的是服务器/资源问题还是我应该考虑重写代码? (我最好的选择是写在C# 中,但无论如何我必须先研究这段代码才能继续前进)。
更新:我在收到第一条评论后添加了更多代码,以提供更多代码。说明:代码创建了一个带有检索到的国家/地区的combobox(选择菜单)(它从上一段代码停止的地方继续)。
if not myR.eof then
clientCountries = myR.getrows
send("<option value='0'>Select country</option>")
log "Creating combobox options"
for i = 0 to ubound(clientCountries, 2)
if cstr(session("clientCountry")) <> "" then
if cstr(clientCountries(1, i)) = cstr(session("clientCountry")) then
isSelected = "selected" else isSelected = ""
end if
end if
if cstr(session("clientCountry")) = "" then
if cstr(clientCountries(1, i)) = "23" then
isSelected = "selected"
else
isSelected = ""
end if
end if
optionString = ""
optionString = clientCountries(2, i)
send("<option value='" & clientCountries(1, i) & "' " & isSelected & ">" & convertToProperCase(optionString) & "</option>")
next
log "Created combobox options"
end if
myR.Close
myR.ActiveConnection.close
myR.ActiveConnection = nothing
set myR = nothing
log "Loaded client countries"
接下来的两个日志条目如下:
11:13:06 - Creating combobox options
11:13:06 - Created combobox options
...
到目前为止,SELECT 查询作为页面的其余部分(2 或 3 个查询更多)或多或少在下一秒内执行。唯一减慢页面速度的部分是您在日志的第一部分中可以看到的内容。我不确定是否可以分析 SQLServer,因为我只有 cPanel 访问权限。这是我知道的唯一方法,除非我可以看看其他东西。
【问题讨论】:
-
您是否尝试过分析 SQL 服务器?我想看看你的选择真的花了多长时间。
-
@NathanRice 我已经用更多信息更新了这个问题,感谢您的关注。
-
需要多长时间(作为猜测)?这样的事情不应该超过几毫秒。
-
一个小的改进:在 FOR 循环中应该只有一个 IF 语句。交换它们并使用 ELSE 而不是第二个 IF。
-
@BlueSix 不幸的是,从我目前的研究来看,经典的 asp 无法通过毫秒。
标签: performance optimization asp-classic