Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@pezhore
Created March 27, 2018 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pezhore/e8fef050b5d426dc34902088944f9a3c to your computer and use it in GitHub Desktop.
Save pezhore/e8fef050b5d426dc34902088944f9a3c to your computer and use it in GitHub Desktop.
function Get-Benchmark {
[CmdletBinding()]
param(
$ScriptBlock,
[Int32] $Iterations
)
begin {}
process {
$AllTimespans = [System.Collections.ArrayList]@()
1..$Iterations | Foreach-Object{
$ProgressSplat = @{
Activity = "Iterating Over ScriptBlock"
Status = "Current Iteration ($( $_ ) of $Iterations)"
PercentComplete = ( $_ / $Iterations ) * 100
}
Write-Progress @ProgressSplat
Remove-Variable -Name Start -ErrorAction SilentlyContinue
$Start = Get-Date
Invoke-Command -ScriptBlock $ScriptBlock | Out-Null
$AllTimespans.Add((New-TimeSpan $Start)) | Out-Null
}
}
end {
Write-Debug "Anything Else?"
$Average = [String][Timespan]::FromTicks(([Math]::Round(($AllTimespans.ticks | Measure-Object -average | Select-Object -Expand Average))))
$Min = [String][Timespan]::FromTicks(([Math]::Round(($AllTimespans.ticks | Measure-Object -Minimum | Select-Object -Expand Minimum))))
$Max = [String][Timespan]::FromTicks(([Math]::Round(($AllTimespans.ticks | Measure-Object -Maximum | Select-Object -Expand Maximum))))
Write-Output "Min: $Min"
Write-Output "Avg: $Average"
Write-output "Max: $Max"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment