programing

PowerShell에서 명령 실행 타이밍

new-time 2020. 5. 12. 21:52
반응형

PowerShell에서 명령 실행 타이밍


Linux의 'time'명령과 같이 PowerShell에서 명령을 실행하는 간단한 방법이 있습니까?

 

나는 이것을 생각해 냈다.

$s=Get-Date; .\do_something.ps1 ; $e=Get-Date; ($e - $s).TotalSeconds

하지만 더 간단한 것을 원합니다

time .\do_something.ps1

예.

Measure-Command { .\do_something.ps1 }

Measure-Command의 단점 중 하나는 stdout 출력이 표시되지 않는다는 것입니다. 출력을 보려면 .NET Stopwatch 객체를 사용할 수 있습니다.

$sw = [Diagnostics.Stopwatch]::StartNew()
.\do_something.ps1
$sw.Stop()
$sw.Elapsed

또한 역사에서 마지막 명령을 취득하고 뺄 수

EndExecutionTime

의에서

StartExecutionTime

.

.\do_something.ps1  
$command = Get-History -Count 1  
$command.EndExecutionTime - $command.StartExecutionTime

사용하다

Measure-Command

Measure-Command { <your command here> | Out-Host }

파이프를

Out-Host

사용하면 명령의 출력을 볼 수 있습니다

Measure-Command

. 그렇지 않으면에 의해 소비됩니다 .


단순

function time($block) {
    $sw = [Diagnostics.Stopwatch]::StartNew()
    &$block
    $sw.Stop()
    $sw.Elapsed
}

다음으로 사용할 수 있습니다

time { .\some_command }

당신은 출력을 조정할 수 있습니다


유닉스

time

명령 과 비슷하게 작성한 함수는 다음과 같습니다 .

function time {
    Param(
        [Parameter(Mandatory=$true)]
        [string]$command,
        [switch]$quiet = $false
    )
    $start = Get-Date
    try {
        if ( -not $quiet ) {
            iex $command | Write-Host
        } else {
            iex $command > $null
        }
    } finally {
        $(Get-Date) - $start
    }
}

출처 :

https://gist.github.com/bender-the-greatest/741f696d965ed9728dc6287bdd336874


스톱워치 사용 및 경과 시간 포맷 :

Function FormatElapsedTime($ts) 
{
    $elapsedTime = ""

    if ( $ts.Minutes -gt 0 )
    {
        $elapsedTime = [string]::Format( "{0:00} min. {1:00}.{2:00} sec.", $ts.Minutes, $ts.Seconds, $ts.Milliseconds / 10 );
    }
    else
    {
        $elapsedTime = [string]::Format( "{0:00}.{1:00} sec.", $ts.Seconds, $ts.Milliseconds / 10 );
    }

    if ($ts.Hours -eq 0 -and $ts.Minutes -eq 0 -and $ts.Seconds -eq 0)
    {
        $elapsedTime = [string]::Format("{0:00} ms.", $ts.Milliseconds);
    }

    if ($ts.Milliseconds -eq 0)
    {
        $elapsedTime = [string]::Format("{0} ms", $ts.TotalMilliseconds);
    }

    return $elapsedTime
}

Function StepTimeBlock($step, $block) 
{
    Write-Host "`r`n*****"
    Write-Host $step
    Write-Host "`r`n*****"

    $sw = [Diagnostics.Stopwatch]::StartNew()
    &$block
    $sw.Stop()
    $time = $sw.Elapsed

    $formatTime = FormatElapsedTime $time
    Write-Host "`r`n`t=====> $step took $formatTime"
}

사용 샘플

StepTimeBlock ("Publish {0} Reports" -f $Script:ArrayReportsList.Count)  { 
    $Script:ArrayReportsList | % { Publish-Report $WebServiceSSRSRDL $_ $CarpetaReports $CarpetaDataSources $Script:datasourceReport };
}

StepTimeBlock ("My Process")  {  .\do_something.ps1 }

Measure-Command {echo "Good morning World!" | Write-Host}

소스-https:

//github.com/PowerShell/PowerShell/issues/2289#issuecomment-247793839

참고 URL :

https://stackoverflow.com/questions/3513650/timing-a-commands-execution-in-powershell

반응형