PoshCode Archive  Check-in [042654558b]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment: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
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:042654558bf16f06c1f37810f3645eeaa0f2d2a9a75a1499684036608931c7de
User & Date: Joel Bennett 2018-06-10 14:21:27
Context
2018-06-10
14:21
Shortcut functions for instantiating WPF or WinForm widgets check-in: d579344556 user: mario tags: trunk
14:21
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 check-in: 042654558b user: Joel Bennett tags: trunk
14:21
PowerShell script for displaying and picking Washington State lottery game winning numbers. check-in: 503b680e2b user: Craig Dayton tags: trunk
Changes

Changes to Get-PerformanceHistory-2.ps1.

1
2
3
4
5
6
7
8

9
10
11
12
13
14
15
16
17
18
19



20
21
22
23
24
25
26

27
28
29
30
31
32
33
34
35
36
37
38
39
40





41
42
43
44
45
46
47

48
49
50
51
52
53

54
55
56


57


58


59
60
61

62
63
64
65
66

67




68
69
70
71
72
73
74
..
77
78
79
80
81
82
83

# encoding: ascii
# api: powershell
# title: Get-PerformanceHistory 2
# description: 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
# version: 2.5
# type: script
# author: Joel Bennett
# license: CC0

# x-poshcode-id: 156
# x-derived-from-id: 683
# x-archived: 2016-04-20T06:09:50
# x-published: 2008-02-28T15:52:00
#
#
#requires -version 2.0
## Get-PerformanceHistory.ps1
##############################################################################################################
## Lets you see the amount of time recent commands in your history have taken
## History:



## v2.5 - added "average" calculation if the first thing in your command line is a range: 1..x
## v2   - added measuring the scripts involved in the command, (uses Tokenizer)
##      - adds a ton of parsing to make the output pretty
##############################################################################################################
#requires -version 2.0
param( [int]$count=1, [int]$id=((Get-History -count 1| Select Id).Id) )


$Parser = [System.Management.Automation.PsParser]
function FormatTimeSpan($ts) {
   if($ts.Minutes) {
      if($ts.Hours) {
         if($ts.Days) {
            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)))
         }
         return "{0:##}:{1:00}:{2:00}.{3:00000}" -f $ts.Hours, $ts.Minutes, $ts.Seconds, [int](100000 * ($ts.TotalSeconds - [Math]::Floor($ts.TotalSeconds)))
      }
      return "{0:##}:{1:00}.{2:00000}" -f $ts.Minutes, $ts.Seconds, [int](100000 * ($ts.TotalSeconds - [Math]::Floor($ts.TotalSeconds)))
   }
   return "{0:#0}.{1:00000}" -f $ts.Seconds, [int](100000 * ($ts.TotalSeconds - [Math]::Floor($ts.TotalSeconds)))
}






Get-History -count $count -id $id | 
ForEach {
   $msr = $null
   $cmd = $_
   # default formatting values
   $avg = 7; $len = 8; $count = 1
   

   $tok = $Parser::Tokenize( $cmd.CommandLine, [ref]$null )
   if( ($tok[0].Type -eq "Number") -and 
       ($tok[0].Content -le 1) -and 
       ($tok[2].Type -eq "Number") -and 
       ($tok[1].Content -eq "..") )
   {

      $count = ([int]$tok[2].Content) - ([int]$tok[0].Content) + 1
   }
   


   $com = @( $tok | where {$_.Type -eq "Command"} | 


                    foreach { get-command $_.Content } | 


                    where { $_.CommandType -eq "ExternalScript" } |
                    foreach { $_.Path } )


   # If we actually got a script, measure it out
   if($com.Count -gt 0){
      $msr = Get-Content -path $com | Measure-Object -L -W -C
   } else {
      $msr = Measure-Object -in $cmd.CommandLine -L -W -C

   }




   
   "" | Select @{n="Id";        e={$cmd.Id}},
               @{n="Duration";  e={FormatTimeSpan ($cmd.EndExecutionTime - $cmd.StartExecutionTime)}},
               @{n="Average";   e={FormatTimeSpan ([TimeSpan]::FromTicks( (($cmd.EndExecutionTime - $cmd.StartExecutionTime).Ticks / $count) ))}},
               @{n="Lines";     e={$msr.Lines}},
               @{n="Words";     e={$msr.Words}},
               @{n="Chars";     e={$msr.Characters}},
................................................................................
} | 
# I have to figure out what the longest time string is to make it look its best
Foreach { 
$avg = [Math]::Max($avg,$_.Average.Length);
$len = [Math]::Max($len,$_.Duration.Length);  
$_ } | Sort Id |
Format-Table @{l="Duration";e={"{0,$len}" -f $_.Duration}},@{l="Average";e={"{0,$avg}" -f $_.Average}},Lines,Words,Chars,Type,Commmand -auto





|



>
|
<
|
<


|




>
>
>




|
|

>
|













>
>
>
>
>
|






>
|
|
|
|
|
<
>
|
<
<
>
>
|
>
>
|
>
>
|
|
<
>
|
|
|
|
|
>
|
>
>
>
>







 







>
1
2
3
4
5
6
7
8
9
10

11

12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

62
63


64
65
66
67
68
69
70
71
72
73

74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
..
95
96
97
98
99
100
101
102
# encoding: ascii
# api: powershell
# title: Get-PerformanceHistory 2
# description: 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
# version: 2.53
# type: script
# author: Joel Bennett
# license: CC0
# function: Get-PerformanceHistory
# x-poshcode-id: 683

# x-archived: 2009-01-05T17:57:06

#
#
##requires -version 2.0
## Get-PerformanceHistory.ps1
##############################################################################################################
## Lets you see the amount of time recent commands in your history have taken
## History:
## v2.53 - added back the Lines, Words, and Characters ....
## v2.52 - added regex-based iteration counting to calculate averages
## v2.51 - removed PsParser features to make it v1 compatible
## v2.5 - added "average" calculation if the first thing in your command line is a range: 1..x
## v2   - added measuring the scripts involved in the command, (uses Tokenizer)
##      - adds a ton of parsing to make the output pretty
##############################################################################################################
function Get-PerformanceHistory {
param( [int]$count=1, [int[]]$id=@((Get-History -count 1| Select Id).Id) )

## Removed to make this v1 compatible
## $Parser = [System.Management.Automation.PsParser]
function FormatTimeSpan($ts) {
   if($ts.Minutes) {
      if($ts.Hours) {
         if($ts.Days) {
            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)))
         }
         return "{0:##}:{1:00}:{2:00}.{3:00000}" -f $ts.Hours, $ts.Minutes, $ts.Seconds, [int](100000 * ($ts.TotalSeconds - [Math]::Floor($ts.TotalSeconds)))
      }
      return "{0:##}:{1:00}.{2:00000}" -f $ts.Minutes, $ts.Seconds, [int](100000 * ($ts.TotalSeconds - [Math]::Floor($ts.TotalSeconds)))
   }
   return "{0:#0}.{1:00000}" -f $ts.Seconds, [int](100000 * ($ts.TotalSeconds - [Math]::Floor($ts.TotalSeconds)))
}

# if there's only one id, then the count counts, otherwise we just use the ids
# ... basically:    { 1..$count | % { $id += $id[-1]-1 }  }
if($id.Count -eq 1) { $id = ($id[0])..($id[0]-($count-1)) } 

# so we can call it with just the IDs
Get-History -id $id | 
ForEach {
   $msr = $null
   $cmd = $_
   # default formatting values
   $avg = 7; $len = 8; $count = 1
   
## Removed to make this v1 compatible
##   $tok = $Parser::Tokenize( $cmd.CommandLine, [ref]$null )
##   if( ($tok[0].Type -eq "Number") -and 
##       ($tok[0].Content -le 1) -and 
##       ($tok[2].Type -eq "Number") -and 
##       ($tok[1].Content -eq "..") )

##   {
##      $count = ([int]$tok[2].Content) - ([int]$tok[0].Content) + 1


##   }
##   
##   $com = @( $tok | where {$_.Type -eq "Command"} | 
##                     foreach { 
##                        $Local:ErrorActionPreference = "SilentlyContinue"
##                        get-command $_.Content 
##                        $Local:ErrorActionPreference = $Script:ErrorActionPreference
##                     } | 
##                     where { $_.CommandType -eq "ExternalScript" } |
##                     foreach { $_.Path } )

##                     
##   # If we actually got a script, measure it out
##   if($com.Count -gt 0){
##      $msr = Get-Content -path $com | Measure-Object -Line -Word -Char
##   } else {
      $msr = Measure-Object -in $cmd.CommandLine -Line -Word -Char
##   }

## V1 Averages:
$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 }
$count = $max - $min
if($count -le 0 ) { $count = 1 }
   
   "" | Select @{n="Id";        e={$cmd.Id}},
               @{n="Duration";  e={FormatTimeSpan ($cmd.EndExecutionTime - $cmd.StartExecutionTime)}},
               @{n="Average";   e={FormatTimeSpan ([TimeSpan]::FromTicks( (($cmd.EndExecutionTime - $cmd.StartExecutionTime).Ticks / $count) ))}},
               @{n="Lines";     e={$msr.Lines}},
               @{n="Words";     e={$msr.Words}},
               @{n="Chars";     e={$msr.Characters}},
................................................................................
} | 
# I have to figure out what the longest time string is to make it look its best
Foreach { 
$avg = [Math]::Max($avg,$_.Average.Length);
$len = [Math]::Max($len,$_.Duration.Length);  
$_ } | Sort Id |
Format-Table @{l="Duration";e={"{0,$len}" -f $_.Duration}},@{l="Average";e={"{0,$avg}" -f $_.Average}},Lines,Words,Chars,Type,Commmand -auto
}