PowerShell How-Tousing a Stopwatch In PowerShell
Monitoring the velocity at which code is finished will give a window into avenues to enrich it for efficiency.
Ever ask yourself how lengthy a bit of PowerShell code takes to execute? might be you've got a script that now and again gets hung up on some method, and you'd like a way to terminate the script if it takes too long. if so, you're going to like the device.Diagnostics.Stopwatch .web class available in PowerShell. the use of a Stopwatch object means that you can exactly measure a birth and prevent time which opens up every kind of alternatives in a PowerShell script.
to use the Stopwatch object, let's first create an object the use of a static components called StartNew().$stopwatch = [system.diagnostics.stopwatch]::StartNew()
once the article has been created, it's invariably running within the background. every time the price of $stopwatch is checked, you'll see a different time all the way down to the microsecond degree.
PS C:\> $stopwatch = [system.diagnostics.stopwatch]::StartNew()PS C:\> $stopwatch
IsRunning Elapsed ElapsedMilliseconds ElapsedTicks--------- ------- ------------------- ------------true 00:00:02.2956772 2299 22990551
PS C:\> $stopwatch
IsRunning Elapsed ElapsedMilliseconds ElapsedTicks--------- ------- ------------------- ------------true 00:00:04.0694321 4069 40694669
PS C:\> $stopwatch
IsRunning Elapsed ElapsedMilliseconds ElapsedTicks--------- ------- ------------------- ------------true 00:00:05.1999316 5199 51999528We can additionally use the Elapsed property to peer the entire time metrics easier.PS C:\> $stopwatch.Elapsed
Days : 0Hours : 0Minutes : 1Seconds : 23Milliseconds : 795Ticks : 837958187TotalDays : 0.000969859012731481TotalHours : 0.0232766163055556TotalMinutes : 1.39659697833333TotalSeconds : 83.7958187TotalMilliseconds : 83795.8187
we will check to see if the stopwatch is running or not by using the IsRunning property and eventually stop the stopwatch through the use of the cease() formulation.PS C:\> $stopwatch.IsRunningTruePS C:\> $stopwatch.stop()PS C:\> $stopwatch.IsRunningFalse
How the this formulation be utilized in a real-world condition? One tremendous use of using the stopwatch is through creating a timeout. for instance you have bought a role that every now and then receives hung up and you wish to make certain the whole script does not cease if this occurs. they can use the stopwatch by together with it in a while loop. below is an instance of checking the overall seconds the stopwatch has been running after which executing some code. To steer clear of from attempting to execute the code every microsecond or much less I've introduced a snooze cmdlet in there to be able to most effective investigate each 2d.whereas ($timer.Elapsed.TotalSeconds -lt 10) ## Do some stuff here
## Wait a specific intervalStart-Sleep -Seconds 1
## check the time$totalSecs = [math]::round($timer.Elapsed.TotalSeconds,0)Write-Verbose -Message "still looking forward to action to complete after [$totalSecs] seconds..."
This illustration ensures that the code inner of the whereas loop doesn't execute for longer than ten seconds. the usage of a timeout is a good means to use this useful .internet object. What other applications do you see that might come in useful by using the stopwatch?
Adam Bertram is a 20-year veteran of IT. he is an automation engineer, blogger, consultant, freelance writer, Pluralsight direction creator and content material advertising advisor to assorted know-how organizations. Adam additionally situated the common TechSnips e-gaining knowledge of platform. He chiefly specializes in DevOps, device management and automation technologies, as well as various cloud structures in most cases in the Microsoft house. he's a Microsoft Cloud and Datacenter administration MVP who absorbs abilities from the IT container and explains it in a straightforward-to-be mindful style. catch up on Adam's articles at adamtheautomator.com, join on LinkedIn or comply with him on Twitter at @adbertram or the TechSnips Twitter account @techsnips_io.