【问题标题】:ASP.Net page methods execution time monitoringASP.Net页面方法执行时间监控
【发布时间】:2013-05-15 18:24:40
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: asp.net time methods execution


    【解决方案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
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 谢谢。但是“配置文件方法级性能”菜单项导致消息框“活动项目的目标不是可执行文件。选择构建可执行程序的项目,或在项目设置中设置外部程序。 (活动项目是我的网络应用程序)。所以我不知道如何让它工作。
      • 嗯,根据您尝试运行项目的位置(IIS 或 Visual Studio Web),您可以选择一个不同的选项,将探查器与正在运行的应用程序绑定的位置。我已经能够成功调试 VS 和 IIS 站点。查看演示视频,因为它们将引导您完成操作。
      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多