经典 ASP XMLHttp 发送速度很慢

Classic ASP XMLHttp Send very slow

我继承了一个经典的 asp 项目,作为升级过程的一部分,我们将大量业务逻辑转移到 REST API(WebApi 2.2 )

写了api的授权端点,asp站点可以调用,但是比直接通过Postman调用慢

(我是一名 C# 编码员而不是 VBScript 编码员,因此以下代码可能令人反感)

Asp代码:

' Send a prebuilt HTTP request and handle the response
' Returns true if the request returns a 200 response, False otherwise 
' Response body is placed in Response
' ErrorMessage is set to return status text if an error code is returned  

Function HandleRequest(ByRef objRequest, strBody)
    set profiler = Server.CreateObject("Softwing.Profiler")
    HandleRequest = False

    ' Add auth token if we have it
    If Not m_accessToken&"" = "" Then
        objRequest.SetRequestHeader "Authorization", "Bearer " & m_accessToken 
    End If

    ' Originating IP for proxy forwarding
    If Not m_clientIp&"" = "" Then
        objRequest.SetRequestHeader "X-Forwarded-For", m_clientIp
    End If

    On Error Resume Next


    If (strBody&"" = "") Then
        objRequest.Send()
    Else
        profiler.ProfileStart()
        objRequest.Send(strBody)
        flSendRequest = profiler.ProfileStop()
    End If


    If Err.Number = 0 Then
        Dim jsonResponse

        If (objRequest.ResponseText&"" <> "") Then
            profiler.ProfileStart()
            set jsonResponse = JSON.parse(objRequest.ResponseText)
            flJson = profiler.ProfileStop()
            set m_Response = jsonResponse
        End If

        If objRequest.Status = 200 Then
            HandleRequest = True
            m_errorMessage = ""
        Else
            m_errorMessage = objRequest.statusText
        End If
    Else
        m_errorMessage = "Unable to connect to Api server"
    End If
    On Error GoTo 0
End Function

你可以看到里面有一些分析代码。

下面的post请求耗时392ms

POST localhost:5000/oauth/token
Content-Type application/x-www-form-urlencoded

client_id:ABCDEF0-ABCD-ABCD-ABCD-ABCDEF-ABCDEF01234
client_secret:aBcDeF0123456789aBcDeF0123456789=
username:demo
password:demo
grant_type:password

如果我通过 Postman 直接向 Api 发出相同的请求,则需要 30 毫秒。

慢了 13 倍多。

什么给了?

编辑

Softwing Profiler 的原始结果:

flJson          10.9583865754112    
flSendRequest   392.282022557137    

所以在 lengthy-ish discussion with the @J-Tolley it looks as though the issue is with the Softwing.Profiler documentation 之后;

all results are given in milliseconds

即使在页面的前面它指出;

has a ten milliseconds resolution

之前没有单独使用过 Softwing.Profiler 组件,建议在经典 ASP 环境中使用的任何人使用 SlTiming class library provided by 4GuysFromRolla.

来实现它

that article 中,它甚至警告任何使用 Softwing.Profiler ProfileStop() 方法的人;

Be aware that Softwing.Profiler's ProfileStop method returns a value in ticks (tenths of milliseconds).