Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tracking duration of functions calls in vb.net

In our VB6 applications, we added a few utility functions for tracking the amount of time spent in a function. We did this to track performance bottlenecks.

Basically, how it worked was there were two utility functions: StartTickCount() and EndTickCount(). You would pass the name of the function in each, and the functions would use a dictionary to get the tick count when StartTickCount() was called, and then subtract the tick count when EndTickCount() was called. This wasn't perfect because it didn't of course take into consideration that calls to get tick count takes time, etc, but basically it worked for our purposes. The pain in the butt part was making sure to call StartTickCount() at the beginning of each function and EndTickCount() at each exit point:

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

Anyway, is there any functionality built in, either through the VS 2010 debugger or in the System.Reflection namespace, to do something similar in VB.NET?

Basically, what I want is to log the number of times each function is called, the total amount to time spent in that function, the average number of seconds spent in that function, and the maximum amount of time spent in a single call in that function.

I can certainly write this by hand (since we already did once in VB6), but if there are existing tools to make it easier, I'd rather use them.

like image 700
scott.korin Avatar asked Feb 15 '12 14:02

scott.korin


1 Answers

You can achieve the same results by modifying/adapting the following code to your needs:

  '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")

You can check documentation about TimeSpan to get directly time in milliseconds or in other formats.

like image 50
Andrea Antonangeli Avatar answered Oct 12 '22 21:10

Andrea Antonangeli