simplify poplog example
Vladimir Bauer
5 years ago
| 1 | 1 | |
| 2 | 2 | import ( |
| 3 | 3 | "fmt" |
| 4 | "io" | |
| 5 | 4 | "math/rand" |
| 6 | "sync" | |
| 7 | 5 | "time" |
| 8 | 6 | |
| 9 | 7 | "github.com/vbauerster/mpb/v5" |
| 13 | 11 | func main() { |
| 14 | 12 | p := mpb.New(mpb.PopCompletedMode()) |
| 15 | 13 | |
| 16 | total, numBars := 100, 2 | |
| 14 | total, numBars := 100, 4 | |
| 17 | 15 | for i := 0; i < numBars; i++ { |
| 18 | 16 | name := fmt.Sprintf("Bar#%d:", i) |
| 19 | 17 | bar := p.AddBar(int64(total), |
| 20 | mpb.BarNoPop(), | |
| 18 | mpb.BarFillerOnComplete(fmt.Sprintf("%s has been completed", name)), | |
| 19 | mpb.BarFillerTrim(), | |
| 21 | 20 | mpb.PrependDecorators( |
| 22 | decor.Name(name), | |
| 23 | decor.Percentage(decor.WCSyncSpace), | |
| 21 | decor.OnComplete(decor.Name(name), ""), | |
| 22 | decor.OnComplete(decor.NewPercentage(" % d "), ""), | |
| 24 | 23 | ), |
| 25 | 24 | mpb.AppendDecorators( |
| 26 | decor.OnComplete( | |
| 27 | decor.EwmaETA(decor.ET_STYLE_GO, 60), "done!", | |
| 28 | ), | |
| 25 | decor.OnComplete(decor.Name(" "), ""), | |
| 26 | decor.OnComplete(decor.EwmaETA(decor.ET_STYLE_GO, 60), ""), | |
| 29 | 27 | ), |
| 30 | 28 | ) |
| 31 | 29 | // simulating some work |
| 32 | go func() { | |
| 33 | rng := rand.New(rand.NewSource(time.Now().UnixNano())) | |
| 34 | max := 100 * time.Millisecond | |
| 35 | for i := 0; i < total; i++ { | |
| 36 | // start variable is solely for EWMA calculation | |
| 37 | // EWMA's unit of measure is an iteration's duration | |
| 38 | start := time.Now() | |
| 39 | time.Sleep(time.Duration(rng.Intn(10)+1) * max / 10) | |
| 40 | bar.Increment() | |
| 41 | // we need to call DecoratorEwmaUpdate to fulfill ewma decorator's contract | |
| 42 | bar.DecoratorEwmaUpdate(time.Since(start)) | |
| 43 | } | |
| 44 | }() | |
| 30 | rng := rand.New(rand.NewSource(time.Now().UnixNano())) | |
| 31 | max := 100 * time.Millisecond | |
| 32 | for i := 0; i < total; i++ { | |
| 33 | // start variable is solely for EWMA calculation | |
| 34 | // EWMA's unit of measure is an iteration's duration | |
| 35 | start := time.Now() | |
| 36 | time.Sleep(time.Duration(rng.Intn(10)+1) * max / 10) | |
| 37 | bar.Increment() | |
| 38 | // we need to call DecoratorEwmaUpdate to fulfill ewma decorator's contract | |
| 39 | bar.DecoratorEwmaUpdate(time.Since(start)) | |
| 40 | } | |
| 45 | 41 | } |
| 46 | 42 | |
| 47 | var wg sync.WaitGroup | |
| 48 | wg.Add(1) | |
| 49 | go func() { | |
| 50 | defer wg.Done() | |
| 51 | rng := rand.New(rand.NewSource(time.Now().UnixNano())) | |
| 52 | max := 3000 * time.Millisecond | |
| 53 | for i := 0; i < 10; i++ { | |
| 54 | filler := makeLogBar(fmt.Sprintf("some log: %d", i)) | |
| 55 | p.Add(0, filler).SetTotal(0, true) | |
| 56 | time.Sleep(time.Duration(rng.Intn(10)+1) * max / 10) | |
| 57 | } | |
| 58 | }() | |
| 59 | ||
| 60 | wg.Wait() | |
| 61 | 43 | p.Wait() |
| 62 | 44 | } |
| 63 | ||
| 64 | func makeLogBar(msg string) mpb.BarFiller { | |
| 65 | limit := "%%.%ds" | |
| 66 | return mpb.BarFillerFunc(func(w io.Writer, _ int, st decor.Statistics) { | |
| 67 | fmt.Fprintf(w, fmt.Sprintf(limit, st.AvailableWidth), msg) | |
| 68 | }) | |
| 69 | } | |