PoshCode Logo PowerShell Code Repository

Get-PerformanceHistory 2 (modification of post by Joel Bennett view diff)
View followups from Joel Bennett | diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/156"></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.5 - added "average" calculation if the first thing in your command line is a range: 1..x
  7. ## v2   - added measuring the scripts involved in the command, (uses Tokenizer)
  8. ##      - adds a ton of parsing to make the output pretty
  9. ##############################################################################################################
  10. #requires -version 2.0
  11. param( [int]$count=1, [int]$id=((Get-History -count 1| Select Id).Id) )
  12.  
  13. $Parser = [System.Management.Automation.PsParser]
  14. function FormatTimeSpan($ts) {
  15.    if($ts.Minutes) {
  16.       if($ts.Hours) {
  17.          if($ts.Days) {
  18.             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)))
  19.          }
  20.          return "{0:##}:{1:00}:{2:00}.{3:00000}" -f $ts.Hours, $ts.Minutes, $ts.Seconds, [int](100000 * ($ts.TotalSeconds - [Math]::Floor($ts.TotalSeconds)))
  21.       }
  22.       return "{0:##}:{1:00}.{2:00000}" -f $ts.Minutes, $ts.Seconds, [int](100000 * ($ts.TotalSeconds - [Math]::Floor($ts.TotalSeconds)))
  23.    }
  24.    return "{0:#0}.{1:00000}" -f $ts.Seconds, [int](100000 * ($ts.TotalSeconds - [Math]::Floor($ts.TotalSeconds)))
  25. }
  26.  
  27. Get-History -count $count -id $id |
  28. ForEach {
  29.    $msr = $null
  30.    $cmd = $_
  31.    # default formatting values
  32.    $avg = 7; $len = 8; $count = 1
  33.    
  34.    $tok = $Parser::Tokenize( $cmd.CommandLine, [ref]$null )
  35.    if( ($tok[0].Type -eq "Number") -and
  36.        ($tok[0].Content -le 1) -and
  37.        ($tok[2].Type -eq "Number") -and
  38.        ($tok[1].Content -eq "..") )
  39.    {
  40.       $count = ([int]$tok[2].Content) - ([int]$tok[0].Content) + 1
  41.    }
  42.    
  43.    $com = @( $tok | where {$_.Type -eq "Command"} |
  44.                     foreach { get-command $_.Content } |
  45.                     where { $_.CommandType -eq "ExternalScript" } |
  46.                     foreach { $_.Path } )
  47.  
  48.    # If we actually got a script, measure it out
  49.    if($com.Count -gt 0){
  50.       $msr = Get-Content -path $com | Measure-Object -L -W -C
  51.    } else {
  52.       $msr = Measure-Object -in $cmd.CommandLine -L -W -C
  53.    }
  54.    
  55.    "" | Select @{n="Id";        e={$cmd.Id}},
  56.                @{n="Duration";  e={FormatTimeSpan ($cmd.EndExecutionTime - $cmd.StartExecutionTime)}},
  57.                @{n="Average";   e={FormatTimeSpan ([TimeSpan]::FromTicks( (($cmd.EndExecutionTime - $cmd.StartExecutionTime).Ticks / $count) ))}},
  58.                @{n="Lines";     e={$msr.Lines}},
  59.                @{n="Words";     e={$msr.Words}},
  60.                @{n="Chars";     e={$msr.Characters}},
  61.                @{n="Type";      e={if($com.Count -gt 0){"Script"}else{"Command"}}},
  62.                @{n="Commmand";  e={$cmd.CommandLine}}
  63. } |
  64. # I have to figure out what the longest time string is to make it look its best
  65. Foreach {
  66. $avg = [Math]::Max($avg,$_.Average.Length);
  67. $len = [Math]::Max($len,$_.Duration.Length);  
  68. $_ } | Sort Id |
  69. Format-Table @{l="Duration";e={"{0,$len}" -f $_.Duration}},@{l="Average";e={"{0,$avg}" -f $_.Average}},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