Tracker.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package solver
  2. import (
  3. "strconv"
  4. "time"
  5. )
  6. // Keep track and output progress.
  7. // Calculate rates, display percentages, estimate the ETA till completion.
  8. func (solver *Solver) Tracker() {
  9. // Add time tracking
  10. defer solver.timeTrack(time.Now(), "Validated solutions")
  11. solver.Outp.Println("Validating solutions")
  12. // Determine if the main-loop is done
  13. var done bool
  14. // Tracking progress in percentages
  15. var percentage float32
  16. // Tracking progress in validated solutions
  17. var track int
  18. // Tracking the rate, starting point
  19. var rateStart uint64
  20. // Tracking the rate, difference between previous iterations
  21. var rateDiff uint64
  22. // Tracking duration
  23. var timerStart = time.Now()
  24. // Estimation how long it will take
  25. var est_fin string
  26. // While not needed for rateDiff anymore, it makes estimation calculations more accurate. ☹️
  27. time.Sleep(time.Second)
  28. // for solver.Iter != solver.counter { // Start for-loop
  29. for !done {
  30. // Determine how far we are.
  31. percentage = (float32(solver.counter.Load()) / (float32(solver.Iter) / 100))
  32. // Reset the loop
  33. rateDiff = solver.counter.Load() - rateStart
  34. if track <= int(percentage) || rateDiff == 0 { // Start if-statement
  35. // Make sure something happened, making rateStart the only reliable variable
  36. if solver.Iter == solver.counter.Load() {
  37. percentage = 100
  38. solver.counter.Store(solver.Iter)
  39. done = true
  40. }
  41. timer_elapsed := time.Since(timerStart)
  42. solver.rates = append(solver.rates, rateDiff)
  43. rate_avg := solver.calcAVG()
  44. // Estimate when this is finished
  45. if rateDiff == 0 {
  46. est_fin = "N/A"
  47. } else {
  48. duration_int := (solver.Iter - solver.counter.Load()) / rate_avg
  49. duration_string := strconv.Itoa(int(duration_int)) + "s"
  50. est, err := time.ParseDuration(duration_string)
  51. if err != nil {
  52. est_fin = "parse error"
  53. } else {
  54. est_fin = est.String()
  55. }
  56. }
  57. // Printing the progress
  58. solver.Outp.Println("Processing: " + strconv.Itoa(int(percentage)) + "% (" + strconv.FormatUint(solver.counter.Load(), 10) + "/" + strconv.Itoa(int(solver.Iter)) + "); Rate: " + strconv.FormatUint(rateDiff, 10) + "/sec for " + timer_elapsed.String() + "; Time left (est.): " + est_fin)
  59. // After we are done printing, exit this for-loop
  60. if percentage == 100 {
  61. break
  62. }
  63. // Wrap up the loop or break
  64. if int(percentage) > track {
  65. track = int(percentage)
  66. } else {
  67. track = track + 1
  68. }
  69. timerStart = time.Now()
  70. }
  71. // Resert the rate counter
  72. rateStart = solver.counter.Load()
  73. // Sleep for a second
  74. if solver.Iter != solver.counter.Load() {
  75. time.Sleep(1 * time.Second)
  76. }
  77. } // End for-loop
  78. }