Tracker.go 2.6 KB

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