2

有人可以建议我,有什么方法可以计算 asp.net 页面中的方法执行时间吗?

ASP.Net Trace 不适合我,因为它忽略了 ajax 部分回发,尽管它们只是紧凑的地方。

我的页面事件由于某种未知原因执行缓慢。由于逻辑和事件执行链相当复杂,我仍然无法仅在调试器中确定罪魁祸首。

我想测量整个页面中每个方法的执行时间,但我仍然没有办法,只能在开头插入多余的代码行(如 timeBegin = DateTime.Now.... timeEnd=DateTime.Now = timeBegin 等)和每种方法的结束。这似乎是一种丑陋的方法,有人知道如何自动做到这一点吗?是否有工具可以测量可用的方法执行时间?

4

3 回答 3

1

我不是 100% 确定以下在所有情况下都有效。到目前为止,它确实如此。请注意Context.Session.Item("CURRENT_WEB_USER")是我存储当前用户的位置,因此不能作为 asp.net 会话变量的一部分随时使用。

下面的代码替换了Global.aspx,它允许整页计时和开发人员代码计时。

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
End Sub

Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
End Sub

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
End Sub

Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
End Sub

Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
End Sub

Sub Application_AcquireRequestState(sender As Object, e As System.EventArgs)
    If HttpContext.Current Is Nothing Then Return
    If HttpContext.Current.Session Is Nothing Then Return
    If TypeOf HttpContext.Current.CurrentHandler Is Page Then
        If Not Context.Items.Contains("Request_Start_Time") Then
            Dim MyPage As Page = HttpContext.Current.CurrentHandler
            AddHandler MyPage.Init, AddressOf PageInitEncountered
            AddHandler MyPage.Unload, AddressOf PageUnloadEncountered
            Context.Items.Add("Request_Start_Time", DateTime.Now)
            Dim User As String = Context.Session.Item("CURRENT_WEB_USER")
            Context.Items.Add("Request_User", User )
        End If
    End If

End Sub

Sub Application_ReleaseRequestState(sender As Object, e As System.EventArgs) 
    If HttpContext.Current Is Nothing Then Return
    If TypeOf Context.CurrentHandler Is Page Then
        Dim Page As Page = Context.CurrentHandler
        Dim StartDate As Date = Context.Items("Request_Start_Time") : If StartDate = Nothing Then Return
        Dim EndDate As Date = Now
        Dim StartProc As Date = Context.Items("Page_Init")
        Dim EndProc As Date = Context.Items("Page_Unload")
        Dim User As String = Context.Items("Request_User")
        LogEntry(StartDate, EndDate, StartProc, EndProc, User, Context.Request.Url.ToString)
    End If

End Sub

Public Sub PageInitEncountered(sender As Object, e As System.EventArgs)
    Context.Items.Add("Page_Init", DateTime.Now)
End Sub

Public Sub PageUnloadEncountered(sender As Object, e As System.EventArgs)
    Context.Items.Add("Page_Unload", DateTime.Now)
End Sub

Public Sub LogEntry(StartDate As Date, EndDate As Date, StartProc As Date, EndProc As Date, User As String, PageURL As String)
    System.Diagnostics.Debug.WriteLine(" (" & LogEntry.RequestMs & " - " & LogEntry.DurationMs & ") (" & User & ") " & PageURL)
End Sub
于 2013-05-15T10:24:40.850 回答
0

您还可以使用 asp.net 性能计数器和其他东西。但这不会衡量页面速度,但它们会衡量所有应用程序的性能。

于 2009-05-13T19:39:19.720 回答
0

这可能不是您正在寻找的,但RedGate有一个名为 Performance Profiler 的产品可以做到这一点。他们确实有 15 天的试用期,因此您甚至可以下载它并在您当前的问题上试用。恕我直言,值得每一分钱。

于 2009-05-13T16:25:55.997 回答