Created On:  26 October 2011

Problem:

How can I accurately measure elapsed time in SilkTest Workbench?

Resolution:

The following code sample will demonstrate How accurately measure elapsed time in SilkTest Workbench.

Cut and paste the script below into an empty .net Script.

---- code sample ----
Imports System.Threading      ' Thread.Sleep method
Imports System.Diagnostics    ' stopwatch type
 
 
Public Module Main
      Dim _desktop As Desktop = Agent.Desktop
      Public Sub Main()
      ' Create a Stopwatch and sleep for zero milliseconds.
      Dim stopwatch As Stopwatch = stopwatch.StartNew
      ' do stuff or use Thread.Sleep
      Thread.Sleep(0)
      stopwatch.Stop()
 
      ' Write the current time.
      Console.WriteLine(stopwatch.ElapsedMilliseconds)
      Console.WriteLine(DateTime.Now.ToLongTimeString)
      'Console.WriteLine(DateTime.Now.TimeOfDay)
 
      ' Start a new Stopwatch.
      stopwatch = stopwatch.StartNew
      ' do stuff or use Thread.Sleep
      Thread.Sleep(10000)
      stopwatch.Stop()
      Console.WriteLine(stopwatch.ElapsedMilliseconds)
      Console.WriteLine(DateTime.Now.ToLongTimeString)
      'Console.WriteLine(DateTime.Now.TimeOfDay)
 
      ' Start a new Stopwatch.
      stopwatch = stopwatch.StartNew
      ' do stuff or use Thread.Sleep
      Thread.Sleep(1000)
      stopwatch.Stop()
      Console.WriteLine(stopwatch.ElapsedMilliseconds)
      'Console.WriteLine(DateTime.Now.TimeOfDay)
      End Sub
End Module
---- end of code sample ----

This will print the following to the output.

.NET Script 'Untitled_38' Started
10000
16:31:47
9999
16:31:57
999
.NET Script 'Untitled_38' Completed