# 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"
                }
            }
        }
}
