Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.