Last active
March 22, 2024 04:24
-
-
Save mavaddat/4dfe9038be7119a46f8113a9e29901b5 to your computer and use it in GitHub Desktop.
Demo for effective time prediction in progress in PowerShell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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