Thursday, January 8, 2015

(PowerShell) Clock App Runtime

I wanted to figure out if there was a way in PowerShell to do something like the time command does in Linux (Calculates the amount of time in ms that an application takes to execute. It turns out that that it is built right in to PowerShell in the form of a cmdlet (Measure-Command).

I decided to start with something simple that would take a little time to complete.

Note: The {braces} define a statement block and in this case is used to combine the actions that you want to time. 

Measure-Command {Get-ChildItem -Path C:\ -Recurse -ErrorAction SilentlyContinue}

Output:












So that worked pretty much how I wanted. So I next I wanted to see if it would time something other than PowerShell cmdlets (like a regular Windows App). I wanted to do something that would take a bit of time so I copied a large file. I checked the PowerShell alias table to make sure robocopy was not an alias for a cmdlet  and gave this a try.

First I found a big file (did not care what it was, just that it was between 100-200MB)

Get-ChildItem -Path $PWD | Where-Object {$_.Length -gt 100MB -and $_.Length -lt 200MB}

Output:





Then ran the speed test on robocopy

Measure-Command {robocopy.exe $PWD $HOME MRT.exe}

Output:











So an executable without a GUI can be timed with no problem. What about a .exe that a user interacts with? It seemed to make sense that something like this would work.

Measure-Command {calc.exe}

Output:











Bad luck, it does not work, it only timed the launching of the app and then returned as soon as the window appeared. I need to do more research but I think that GUI apps disconnect themselves from the shell once they are launched and can not be measured this way :(.

No comments:

Post a Comment