|
Measuring Code Execution Time (VB.Net) |
|
|
Written by: Evan Cummings
Measuring Code Execution TimeIn one of my current projects I am faced with very long running web service calls to a third party service. In the ASP.NET web environment in which I am working in, this raises some questions and issues with performance, timeouts, and the general user experience. Each of these are major topics in their own right, but one thing that can really help you drill down and debug your long running processes and calls is to time their execution. There are many sophisticated methods to do this (including commericial products). To get a general idea this is unnecessary - we can simply use the built in .NET capabilities to build a basic code execution timer.
Timing Code
For our simple timer, we will not worry about the fidelity and resolution of time. To start, lets make use of the common .NET classes DateTime and Timespan. DateTime exposes a structure that represents time, while Timespan exposes a structure for working with time intervals. This is an important disctintion because you cannot simply do calculations on DateTime objects.
First, lets store the exact moment we begin our execution time:
dim startTime as DateTime = DateTime.Now
At this point, we have the starting point; lets now begin our call, which in my case is a web service call:
dim objSvc As New WebService() dim ds As DataSet = objSvc.GetData()
Now we wait - when the web service completes, lets calculate our total execution time:
dim executionTime As TimeSpan = DateTime.Now - startTime
In this code, we take the exact moment the service call completes and find the difference between that and the time in which we started. This leaves us the execution time of the call.
At this point we use the results as needed, I will output it to the Console, for example:
Console.WriteLine("DataSet contains " & ds.Tables(0).Rows.Count & " rows - Retrieved in " & executionTime.Minutes.ToString() & " minutes and " & executionTime.Seconds.ToString() & " seconds.")
Conclusion
Thats all there is to setting up a basic code execution timer. Realize that this is neither the most useful nor the most robust approach - it does, however, work very well for quickly debugging and guaging your needs when working with long running requests.
Content - VB.NET, ASP.NET, DateTime, TimeSpan, Code Execution Time
|
|
|
|
|
|