PoshCode Archive  Artifact [ab1e87fe4e]

Artifact ab1e87fe4e69a0300d15da0b64f72ceb07eec72b760ba5da304721b8fb5c589e:

  • File Measure-Command.ps1 — part of check-in [9ad273a2a3] at 2018-06-10 14:02:53 on branch trunk — Measure-Command, but with iterations and stuff (user: Joel Bennett size: 1349)

# encoding: ascii
# api: powershell
# title: Measure-Command
# description: Measure-Command, but with iterations and stuff
# version: 0.1
# type: function
# author: Joel Bennett
# license: CC0
# function: Measure-Command
# x-poshcode-id: 5861
# x-archived: 2015-05-26T00:32:46
# x-published: 2015-05-15T22:17:00
#
#
function Measure-Command {
    [CmdletBinding()]
    param(
        [ScriptBlock]$Command,
        [Int]$Iterations = 10,
        [Switch]$NoBuffer
    )
    
    if(!$NoBuffer) { $Null = & $Command }       

    $watch = [System.Diagnostics.Stopwatch]::new()
    $watch.Start()
    $Measurements = for($i=0; $i -lt $Iterations; $i++) {
            $1 = $watch.ElapsedTicks
            $Null = &$Command
            $2 = $watch.ElapsedTicks
            $2 - $1
    }
    $Measurements | 
        Measure-Object -Average -Minimum -Maximum | 
        &{
            process{
                [PSCustomObject]@{
                    PSTypeName = "ExecutionPerformance"
                    Average = [Timespan]::FromTicks($_.Average)
                    Maximum = [Timespan]::FromTicks($_.Maximum)
                    Minimum = [Timespan]::FromTicks($_.Minimum)
                    Iterations = $Iterations
                    Command = "$Command"
                }
            }
        }
}