PerformanceBenchmark.psm1

Function Measure-Performance
{
    param
    (
        [Parameter(Mandatory=$true)]
        [ScriptBlock] $Expression,
        
         [Parameter(Mandatory=$false)]
        [Object[]] $Arguments,

        [Parameter(Mandatory=$false)]
        [int] $Times,
        
        [Parameter(Mandatory=$false)]
        [string] $Title
    )

    if($Times -eq $null -or $Times -le 0)
    {
        $Count = 1
    }

    $resultsList = New-Object System.Collections.Generic.LinkedList[long]

    for($i=0;$i -lt $Times;$i=$i+1)
    {
        if($Arguments -ne $null)
        {
            foreach($argument in $Arguments)
            {
               $stopwatch = [System.Diagnostics.Stopwatch]::StartNew()

               Invoke-Command $Expression -ArgumentList $argument
           
               $stopwatch.Stop()
                      
               $resultsList.AddFirst($($stopwatch.Elapsed.TotalMilliseconds)) | Out-Null
            }
        }
        else
        {
            $stopwatch = [System.Diagnostics.Stopwatch]::StartNew()

            Invoke-Command $Expression -ArgumentList $argument
           
            $stopwatch.Stop()
                      
            $resultsList.AddFirst($($stopwatch.Elapsed.TotalMilliseconds)) | Out-Null
        }
    }

    $result = $($resultsList | Measure-Object -Maximum -Minimum -Average -Sum) 

    if($Title -ne $null)
    {
         $result.Property = $Title
    }

    return $result
}

Export-ModuleMember -Function Measure-Performance