increment bar with random value
Vladimir Bauer
4 years ago
| 27 | 27 | ), |
| 28 | 28 | mpb.AppendDecorators(decor.Percentage(decor.WC{W: 5})), |
| 29 | 29 | ) |
| 30 | go complete(bars[i], i+1) | |
| 30 | go complete(bars[i]) | |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | for i := 0; i < numBars; i++ { |
| 34 | 34 | afterBar := bars[i] |
| 35 | 35 | task := fmt.Sprintf("Task#%02d:", i) |
| 36 | incrBy := numBars - i | |
| 37 | 36 | go func() { |
| 38 | 37 | job := "\x1b[31minstalling\x1b[0m" |
| 39 | 38 | // preparing queued bars |
| 49 | 48 | decor.OnComplete(decor.Percentage(decor.WC{W: 5}), ""), |
| 50 | 49 | ), |
| 51 | 50 | ) |
| 52 | complete(b, incrBy) // blocks until afterBar completes | |
| 51 | complete(b) // blocks until afterBar completes | |
| 53 | 52 | }() |
| 54 | 53 | } |
| 55 | 54 | |
| 56 | 55 | p.Wait() |
| 57 | 56 | } |
| 58 | 57 | |
| 59 | func complete(bar *mpb.Bar, incrBy int) { | |
| 58 | func complete(bar *mpb.Bar) { | |
| 60 | 59 | max := 100 * time.Millisecond |
| 61 | 60 | for !bar.Completed() { |
| 62 | 61 | // start variable is solely for EWMA calculation |
| 63 | 62 | // EWMA's unit of measure is an iteration's duration |
| 64 | 63 | start := time.Now() |
| 65 | 64 | time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10) |
| 66 | bar.IncrBy(incrBy) | |
| 65 | bar.IncrInt64(rand.Int63n(5) + 1) | |
| 67 | 66 | // we need to call DecoratorEwmaUpdate to fulfill ewma decorator's contract |
| 68 | 67 | bar.DecoratorEwmaUpdate(time.Since(start)) |
| 69 | 68 | } |