forked from vbauerster/mpb
/
main.go
51 lines (45 loc) · 1.01 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"fmt"
"math/rand"
"sync"
"time"
"github.com/vbauerster/mpb"
"github.com/vbauerster/mpb/decor"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func main() {
var wg sync.WaitGroup
p := mpb.New(mpb.WithWaitGroup(&wg))
total := 100
numBars := 3
wg.Add(numBars)
for i := 0; i < numBars; i++ {
name := fmt.Sprintf("Bar#%d:", i)
b := p.AddBar(int64(total), mpb.BarID(i),
mpb.OptionOnCondition(mpb.BarRemoveOnComplete(), func() bool { return i == 0 }),
mpb.PrependDecorators(
decor.Name(name),
decor.EwmaETA(decor.ET_STYLE_GO, 60, decor.WCSyncSpace),
),
mpb.AppendDecorators(decor.Percentage()),
)
go func() {
defer wg.Done()
max := 100 * time.Millisecond
for i := 0; i < total; i++ {
start := time.Now()
if b.ID() == 2 && i == 42 {
p.Abort(b, true)
return
}
time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
// ewma based decorators require work duration measurement
b.IncrBy(1, time.Since(start))
}
}()
}
p.Wait()
}