Skip to content

Instantly share code, notes, and snippets.

@mavaddat
Last active March 22, 2024 04:24
Show Gist options
  • Save mavaddat/4dfe9038be7119a46f8113a9e29901b5 to your computer and use it in GitHub Desktop.
Save mavaddat/4dfe9038be7119a46f8113a9e29901b5 to your computer and use it in GitHub Desktop.
Demo for effective time prediction in progress in PowerShell
$queries = 0..25
Set-Variable -Name SecondsPerQuery -Value 11 -Option ReadOnly -Force
Set-Variable -Name Variability -Value 0.07 -Option ReadOnly -Force
$queries | ForEach-Object -Begin {
$averageSecondsPerQuery = $SecondsPerQuery # Average seconds per query will be used to predict the time per iteration
} -Process {
$index = $_
$timing = [timespan]::FromSeconds((Get-Random -Minimum ((1.00 - $Variability) * $SecondsPerQuery) -Maximum ((1.00 + $Variability) * $SecondsPerQuery)))
$averageSecondsPerQuery = $averageSecondsPerQuery * ($index / $queries.Length) + $timing.TotalSeconds * ($queries.Length - $index) / $queries.Length
[pscustomobject]@{
Index = $index
SecondsForIteration = $timing.TotalSeconds
AverageSecondsPerQuery = $averageSecondsPerQuery
SecondsRemaining = (($queries.Length - $index) * $averageSecondsPerQuery) # A moving weighted average
}
Write-Progress -Activity 'Predicting timing' -SecondsRemaining (($queries.Length - $index) * $averageSecondsPerQuery) -Status "$index / $($queries.Length)" -PercentComplete (($index / $queries.Length) * 100) -CurrentOperation "Query $index of $($queries.Length)" -Id 0
Start-Sleep -Seconds $timing.TotalSeconds # Simulate query
} | Format-Table -AutoSize -Wrap
Write-Progress -Activity 'Predicting timing' -Status 'Complete' -Id 0 -Completed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment