【问题标题】:Tracking duration of functions calls in vb.net跟踪 vb.net 中函数调用的持续时间
【发布时间】:2012-03-06 22:08:38
【问题描述】:

在我们的 VB6 应用程序中,我们添加了一些实用函数来跟踪函数花费的时间量。我们这样做是为了跟踪性能瓶颈。

基本上,它的工作原理是有两个实用函数:StartTickCount() 和 EndTickCount()。您将在每个函数中传递函数的名称,并且函数将使用字典在调用 StartTickCount() 时获取滴答计数,然后在调用 EndTickCount() 时减去滴答计数。这并不完美,因为它当然没有考虑到获取滴答计数的调用需要时间等,但基本上它适用于我们的目的。对接部分的痛苦在于确保在每个函数的开头调用 StartTickCount() 并在每个退出点调用 EndTickCount():

Private Function SomeFuction() as String
    ' indicate the function started
    StartTickCount("MyClass.SomeFunction")


    ' some logic that causes the function to end
    If (some logic) Then
        EndTickCount("MyClass.SomeFunction")
        Return "Hello!"
    End If

    ' final exit point
    EndTickCount("MyClass.SomeFunction")
    Return "World"
End Function

无论如何,是否有任何内置功能,无论是通过 VS 2010 调试器还是在 System.Reflection 命名空间中,都可以在 VB.NET 中执行类似的操作?

基本上,我想要的是记录每个函数被调用的次数、在该函数中花费的总时间、在该函数中花费的平均秒数以及在单个函数中花费的最大时间调用那个函数。

我当然可以手动编写(因为我们已经在 VB6 中做过一次),但是如果有现有的工具可以让它更容易,我宁愿使用它们。

【问题讨论】:

  • 您考虑过使用分析器吗?
  • 听起来像探查器会起作用,至少在调试模式下运行时是这样。但是我们可能还需要检查客户端服务器(没有 VS2010 工具)上的性能,那么还有任何编码选项吗?

标签: vb.net visual-studio-2010 debugging visual-studio-debugging performance-testing


【解决方案1】:

您可以通过根据您的需要修改/调整以下代码来获得相同的结果:

  'Create a variable for start time:
  Dim TimerStart As DateTime
  TimerStart = Now
 '
 'place your computing here
 '
  Dim TimeSpent As System.TimeSpan
  TimeSpent = Now.Subtract(TimerStart)
  MsgBox(TimeSpent.TotalSeconds & " seconds spent on this task")

您可以查看有关 TimeSpan 的文档,以直接以毫秒或其他格式获取时间。

【讨论】:

    【解决方案2】:

    我想看看秒表类 - 它是为这种类型的东西设计的。

    但是,已经有一个很棒的工具可以做到这一点,而且它实际上只需很少的工作就可以做得更好。它还有 10 天的免费试用期。

    http://www.jetbrains.com/profiler/

    (我不为 jetbrains 工作,也不隶属于 jetbrains - 但我希望我是因为他们制造了一些非常酷的产品)

    【讨论】:

    【解决方案3】:

    听起来您需要在应用程序中添加一些分析。您可能想从beginner's guide to profiling with VS 开始。

    您还可以在应用程序中为运行时指标添加性能计数器。见http://msdn.microsoft.com/en-us/library/w8f5kw2e(v=vs.100).aspx。

    如需更完整的工具和技术列表,请参阅http://msdn.microsoft.com/en-us/magazine/hh288073.aspx。

    【讨论】:

      【解决方案4】:

      我经常使用这个助手类。它基本上符合@AndreaAntonangeli 的建议,但安排了更简洁的代码:

      Public Class TimeSpanHelper
      
          Dim caller_name As String
          Dim timer_start As DateTime
          Dim time_spent As System.TimeSpan
          Dim prev_time As DateTime
          Dim time_diff As System.TimeSpan
      
          Public Sub New(caller As String)
              caller_name = caller
          End Sub
      
          Public Sub startClock()
              timer_start = DateTime.Now
              Console.WriteLine(caller_name & " starts at:" & timer_start.ToString)
              prev_time = timer_start
          End Sub
      
          Public Sub getElapsedTime(Optional flag As String = "")
              time_spent = Now.Subtract(timer_start)
              time_diff = Now.Subtract(prev_time)
              prev_time = DateTime.Now
              Console.WriteLine(caller_name & "-" & flag & " " & time_spent.TotalSeconds & "s from start (" & time_diff.TotalSeconds & "s from previous flag)")
          End Sub
      
          Public Sub stopClock()
              Dim timer_stop As DateTime = DateTime.Now
              time_spent = Now.Subtract(timer_start)
              time_diff = Now.Subtract(prev_time)
              Console.WriteLine(caller_name & " ends at:" & timer_stop.ToString & " - " & time_spent.TotalSeconds & " seconds elapsed from start (" & time_diff.TotalSeconds & "s from previous flag)")
          End Sub
      
      End Class
      

      使用示例:

      'Put this wherever you want to start to count 
      Dim timer As TimeSpanHelper = New TimeSpanHelper("MyFunctionName")
      timer.startClock()
      'Do something ...
      timer.getElapsedTime("flag1")
      'Do something ...
      timer.getElapsedTime("flag2")
      'Do something ...
      timer.stopClock()
      

      你的输出应该是这样的:

      MyFunctionName starts at 30/03/2017 16:57:21
      MyFunctionName - flag1 Xs from start (Xs from previous flag)
      MyFunctionName - flag2 Xs from start (Xs from previous flag)
      MyFunctionName ends at 30/03/2017 16:59:14 - 120s elapsed from start (Xs from previous flag)
      

      【讨论】:

        猜你喜欢
        • 2014-05-23
        • 2011-10-15
        • 1970-01-01
        • 2019-06-05
        • 1970-01-01
        • 2018-02-15
        • 1970-01-01
        • 1970-01-01
        • 2017-12-18
        相关资源
        最近更新 更多