在vb.net中跟踪函数调用的持续时间

时间:2012-02-15 14:52:02

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

在我们的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中做了一次),但是如果现有的工具可以让它变得更容易,我宁愿使用它们。

4 个答案:

答案 0 :(得分:7)

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

  '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的文档,以便以毫秒或其他格式直接获取时间。

答案 1 :(得分:3)

我看看秒表类 - 它专为此类事物而设计。

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

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

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

答案 2 :(得分:2)

听起来您需要在应用程序中添加一些分析。您可能希望从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

答案 3 :(得分:0)

我非常使用这个助手类。它基本上完成了@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)