Форматирование вывода System.Diagnostics.Stopwatch и Measure-Object с помощью PowerShell 5 - PullRequest
0 голосов
/ 16 марта 2020

Windows 10 64-бит. PowerShell 5.1

Формат вывода System.Diagnostics.Stopwatch и Measure-Object с использованием PowerShell 5.

Я зацикливаю команду System.Diagnostics.Stopwatch и выводю Elapsed.TotalSeconds в массив для измерения производительности набор команд. Я обрабатываю массив с помощью Measure-Object, удаляю пробелы с помощью $_.Trim(" `t"), строку с select-string -pattern и добавляю новую строку с if match regex, чтобы получить это и немного больше:

Count    : 10
Average  : 0.35949785
Sum      : 3.5949785
Maximum  : 1.485631
Minimum  : 0.1970544

Count    : 10
Average  : 0.33406021
Sum      : 3.3406021
Maximum  : 1.0060256
Minimum  : 0.2100706

Есть ли лучший способ?

$zcommandpath=$MyInvocation.MyCommand.Path
for ($zdoug1=0;$zdoug1 -lt 2; $zdoug1++){
Write-Output "`r`nThis is $zcommandpath`r`nHow long to archive 9 text files.`r`nStarted: "((get-date).ToString('MM/dd/yy hh:mm:ss.ffff tt') -replace " : ", ":") | out-file -append -nonewline logfile031822_030657692.txt
$times=@()
# https://stackoverflow.com/a/56687926/8826818  # stopwatch
$watch = New-Object System.Diagnostics.Stopwatch
for ($zdoug2=0;$zdoug2 -lt 2; $zdoug2++){
$watch.Start()
sleep 1 # commands here
$times += $watch.Elapsed.TotalSeconds 
$watch.reset()} 
$zLoopFinished=((get-date).ToString('MM/dd/yy hh:mm:ss.ffff tt') -replace " : ", ":") 
# https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/measure-object?view=powershell-7 
# https://stackoverflow.com/questions/7614695  # measure object
$times | Measure-Object -sum -ave -max -min | out-file -append logfile031822_030657692.txt 
Write-Output "Finished:  $zLoopFinished" | out-file -append logfile031822_030657692.txt    
} 
cls;Write-Host " 
Look at logfile031822_030657692.txt
Press any key to remove whitespace and line containing 'Property: ' ...
";cmd /c pause>nul 

# REMOVE WHITESPACE AND LINE CONTAINING "PROPERTY :"     
(gc logfile031822_030657692.txt) | Where { $_.Trim(" `t") }  | select-string -pattern "Property      :" -notmatch | out-file logfile031822_030657692.txt

cls;Write-Host " 
Press any key to add a new line to logfile.
";cmd /c pause>nul 

# ADD NEW LINE AFTER LINE THAT CONTAINS "FINISHED: ..."
$NewLine = gc logfile031822_030657692.txt | ForEach-Object {
$_ 
if ($_ -match ('^' + [regex]::Escape( 'Finished:' ))){ " " }}
$NewLine | Out-File logfile031822_030657692.txt -Force 

# Empty the logfile
cls;Write-Host " 
Finished logfile031822_030657692.txt
Press any key to empty the logfile.
";cmd /c pause>nul 

ni -ItemType File logfile031822_030657692.txt -Force > $null
# 

Format System.Diagnostics.Stopwatch and Measure-Object output using PowerShell 5. Remove whitespace with $_.Trim("`t"), $_.Trim("t"), remove line with select-string -pattern -notmatch, and add a newline with if match regex.
...