ASP.Net页面方法执行时间监控

时间:2009-05-13 16:06:15

标签: asp.net time methods execution

任何人都可以建议我,有没有办法计算asp.net页面中的方法执行时间?

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

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

我想在整个页面中测量每个方法的执行时间,但我仍然看不到插入冗余代码行(如timeBegin = DateTime.Now .... timeEnd = DateTime.Now = timeBegin等)在每个方法的开头和结尾。这似乎是丑陋的方法,有人知道如何自动完成吗?是否有可用于衡量方法执行时间的工具?

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

这可能不是您要找的,但RedGate有一个名为Performance Profiler的产品就是这样做的。他们有15天的试用期,所以您甚至可以下载它并在当前问题上进行试用。恕我直言,值得每一分钱。

答案 2 :(得分:0)

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