PoshCode Logo PowerShell Code Repository

Get-PerformanceHistory 2 (modification of post by view diff)
View followups from Joel Bennett and Joel Bennett | diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/155"></script>download | new post

This much more complicated version of Get-PerformanceHistory shows the approximate length of the command or script, as well as how long it took to run. Great for those “my script is shorter/faster/cooler” than yours bragging sessions on IRC ... or whatever. Lets you compare several commands by just running each of them and then calling Get-PerformanceHistory -Count 4

  1. #requires -version 2.0
  2. ## Get-PerformanceHistory.ps1
  3. ##############################################################################################################
  4. ## Lets you see the amount of time recent commands in your history have taken
  5. ## History:
  6. ## v2 - adds a ton of parsing to make the output pretty
  7. ##    - added measuring the scripts involved in the command, (uses Tokenizer)
  8. ##############################################################################################################
  9. param( [int]$count=1, [int]$id=((Get-History -count 1| Select Id).Id) )
  10.  
  11. $Parser = [System.Management.Automation.PsParser]
  12. function FormatTimeSpan($ts) {
  13.    if($ts.Minutes) {
  14.       if($ts.Hours) {
  15.          if($ts.Days) {
  16.             return "{0:##}d {1:00}:{2:00}:{3:00}.{4:00000}" -f $ts.Days, $ts.Hours, $ts.Minutes, $ts.Seconds, [int](100000 * ($ts.TotalSeconds - [Math]::Floor($ts.TotalSeconds)))
  17.          }
  18.          return "{0:##}:{1:00}:{2:00}.{3:00000}" -f $ts.Hours, $ts.Minutes, $ts.Seconds, [int](100000 * ($ts.TotalSeconds - [Math]::Floor($ts.TotalSeconds)))
  19.       }
  20.       return "{0:##}:{1:00}.{2:00000}" -f $ts.Minutes, $ts.Seconds, [int](100000 * ($ts.TotalSeconds - [Math]::Floor($ts.TotalSeconds)))
  21.    }
  22.    return "{0:#0}.{1:00000}" -f $ts.Seconds, [int](100000 * ($ts.TotalSeconds - [Math]::Floor($ts.TotalSeconds)))
  23. }
  24.  
  25. Get-History -count $count -id $id |
  26. ForEach {
  27.    $msr = $null
  28.    $cmd = $_
  29.    $len = 8
  30.    $com = @( $Parser::Tokenize( $cmd.CommandLine, [ref]$null ) |
  31.                   where {$_.Type -eq "Command"} |
  32.                   foreach { get-command $_.Content } |
  33.                   where { $_.CommandType -eq "ExternalScript" } |
  34.                   foreach { $_.Path } )
  35.  
  36.    # If we actually got a script, measure it out
  37.    if($com.Count -gt 0){
  38.       $msr = Get-Content -path $com | Measure-Object -L -W -C
  39.    } else {
  40.       $msr = Measure-Object -in $cmd.CommandLine -L -W -C
  41.    }
  42.    
  43.    "" | Select @{n="Id";        e={$cmd.Id}},
  44.                @{n="Duration";  e={FormatTimeSpan ($cmd.EndExecutionTime - $cmd.StartExecutionTime)}},
  45.                @{n="Lines";     e={$msr.Lines}},
  46.                @{n="Words";     e={$msr.Words}},
  47.                @{n="Chars";     e={$msr.Characters}},
  48.                @{n="Type";      e={if($com.Count -gt 0){"Script"}else{"Command"}}},
  49.                @{n="Commmand";  e={$cmd.CommandLine}}
  50. } |
  51. # I have to figure out what the longest time string is to make it look its best
  52. Foreach { $len = [Math]::Max($len,$_.Duration.Length); $_ } | Sort Id |
  53. Format-Table @{l="Duration";e={"{0,$len}" -f $_.Duration}},Lines,Words,Chars,Type,Commmand -auto

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me