PoshCode Logo PowerShell Code Repository

PerformanceHistory 2.52 (modification of post by Joel Bennett view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/424"></script>download | new post

Another modification to Get-PerformanceHistory to allow it to work in PowerShell 1.0 and still show averages.

  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.52 - added regex-based iteration counting to calculate averages
  7. ## v2.51 - removed PsParser features to make it v1 compatible
  8. ## v2.5 - added "average" calculation if the first thing in your command line is a range: 1..x
  9. ## v2   - added measuring the scripts involved in the command, (uses Tokenizer)
  10. ##      - adds a ton of parsing to make the output pretty
  11. ##############################################################################################################
  12. # function Get-PerformanceHistory {
  13. param( [int]$count=1, [int[]]$id=@((Get-History -count 1| Select Id).Id) )
  14.  
  15. ## Removed to make this v1 compatible
  16. ## $Parser = [System.Management.Automation.PsParser]
  17. function FormatTimeSpan($ts) {
  18.    if($ts.Minutes) {
  19.       if($ts.Hours) {
  20.          if($ts.Days) {
  21.             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)))
  22.          }
  23.          return "{0:##}:{1:00}:{2:00}.{3:00000}" -f $ts.Hours, $ts.Minutes, $ts.Seconds, [int](100000 * ($ts.TotalSeconds - [Math]::Floor($ts.TotalSeconds)))
  24.       }
  25.       return "{0:##}:{1:00}.{2:00000}" -f $ts.Minutes, $ts.Seconds, [int](100000 * ($ts.TotalSeconds - [Math]::Floor($ts.TotalSeconds)))
  26.    }
  27.    return "{0:#0}.{1:00000}" -f $ts.Seconds, [int](100000 * ($ts.TotalSeconds - [Math]::Floor($ts.TotalSeconds)))
  28. }
  29.  
  30. # if there's only one id, then the count counts, otherwise we just use the ids
  31. # ... basically:    { 1..$count | % { $id += $id[-1]-1 }  }
  32. if($id.Count -eq 1) { $id = ($id[0])..($id[0]-($count-1)) }
  33.  
  34. # so we can call it with just the IDs
  35. Get-History -id $id |
  36. ForEach {
  37.    $msr = $null
  38.    $cmd = $_
  39.    # default formatting values
  40.    $avg = 7; $len = 8; $count = 1
  41.    
  42. ## Removed to make this v1 compatible
  43. ##   $tok = $Parser::Tokenize( $cmd.CommandLine, [ref]$null )
  44. ##   if( ($tok[0].Type -eq "Number") -and
  45. ##       ($tok[0].Content -le 1) -and
  46. ##       ($tok[2].Type -eq "Number") -and
  47. ##       ($tok[1].Content -eq "..") )
  48. ##   {
  49. ##      $count = ([int]$tok[2].Content) - ([int]$tok[0].Content) + 1
  50. ##   }
  51. ##   
  52. ##   $com = @( $tok | where {$_.Type -eq "Command"} |
  53. ##                     foreach {
  54. ##                        $Local:ErrorActionPreference = "SilentlyContinue"
  55. ##                        get-command $_.Content
  56. ##                        $Local:ErrorActionPreference = $Script:ErrorActionPreference
  57. ##                     } |
  58. ##                     where { $_.CommandType -eq "ExternalScript" } |
  59. ##                     foreach { $_.Path } )
  60. ##                     
  61. ##   # If we actually got a script, measure it out
  62. ##   if($com.Count -gt 0){
  63. ##      $msr = Get-Content -path $com | Measure-Object -Line -Word -Char
  64. ##   } else {
  65.       $msr = Measure-Object -in $cmd.CommandLine -Line -Word -Char
  66. ##   }
  67.  
  68. ## V1 Averages:
  69. $min, $max = ([regex]"^\s*(?:(?<min>\d+)\.\.(?<max>\d+)\s+\||for\s*\([^=]+=\s*(?<min>\d+)\s*;[^;]+\-lt\s*(?<max>\d+)\s*;[^;)]+\)\s*{)").match( $cmd.CommandLine ).Groups[1,2] | % { [int]$_.Value }
  70. $count = $max - $min
  71. if($count -le 0 ) { $count = 1 }
  72.    
  73.    "" | Select @{n="Id";        e={$cmd.Id}},
  74.                @{n="Duration";  e={FormatTimeSpan ($cmd.EndExecutionTime - $cmd.StartExecutionTime)}},
  75.                @{n="Average";   e={FormatTimeSpan ([TimeSpan]::FromTicks( (($cmd.EndExecutionTime - $cmd.StartExecutionTime).Ticks / $count) ))}},
  76.                # @{n="Lines";     e={$msr.Lines}},
  77.                # @{n="Words";     e={$msr.Words}},
  78.                # @{n="Chars";     e={$msr.Characters}},
  79.                # @{n="Type";      e={if($com.Count -gt 0){"Script"}else{"Command"}}},
  80.                @{n="Commmand";  e={$cmd.CommandLine}}
  81. } |
  82. # I have to figure out what the longest time string is to make it look its best
  83. Foreach {
  84. $avg = [Math]::Max($avg,$_.Average.Length);
  85. $len = [Math]::Max($len,$_.Duration.Length)
  86. $_ } | Sort Id |
  87. Format-Table @{l="Duration";e={"{0,$len}" -f $_.Duration}},@{l="Average";e={"{0,$avg}" -f $_.Average}},Commmand -auto #Lines,Words,Chars,Type,
  88. #}

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