Codebase list golang-github-vbauerster-mpb / f7a332b
refactoring examples Vladimir Bauer 6 years ago
17 changed file(s) with 30 addition(s) and 119 deletion(s). Raw diff Collapse all Expand all
9090 // simulating some work
9191 go func() {
9292 defer wg.Done()
93 rng := rand.New(rand.NewSource(time.Now().UnixNano()))
9394 max := 100 * time.Millisecond
9495 for i := 0; i < total; i++ {
9596 start := time.Now()
96 time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
97 time.Sleep(time.Duration(rng.Intn(10)+1) * max / 10)
9798 // since ewma decorator is used, we need to pass time.Since(start)
9899 bar.Increment(time.Since(start))
99100 }
99 "github.com/vbauerster/mpb/v4"
1010 "github.com/vbauerster/mpb/v4/decor"
1111 )
12
13 func init() {
14 rand.Seed(time.Now().UnixNano())
15 }
1612
1713 func main() {
1814 var wg sync.WaitGroup
4541 // simulating some work
4642 go func() {
4743 defer wg.Done()
44 rng := rand.New(rand.NewSource(time.Now().UnixNano()))
4845 max := 100 * time.Millisecond
4946 for i := 0; i < total; i++ {
5047 start := time.Now()
51 time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
48 time.Sleep(time.Duration(rng.Intn(10)+1) * max / 10)
5249 // since ewma decorator is used, we need to pass time.Since(start)
5350 bar.Increment(time.Since(start))
5451 }
99 "github.com/vbauerster/mpb/v4"
1010 "github.com/vbauerster/mpb/v4/decor"
1111 )
12
13 func init() {
14 rand.Seed(time.Now().UnixNano())
15 }
1612
1713 func main() {
1814 ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
3935
4036 go func() {
4137 defer wg.Done()
38 rng := rand.New(rand.NewSource(time.Now().UnixNano()))
4239 max := 100 * time.Millisecond
4340 for !bar.Completed() {
4441 start := time.Now()
45 time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
42 time.Sleep(time.Duration(rng.Intn(10)+1) * max / 10)
4643 // since ewma decorator is used, we need to pass time.Since(start)
4744 bar.Increment(time.Since(start))
4845 }
88 "github.com/vbauerster/mpb/v4"
99 "github.com/vbauerster/mpb/v4/decor"
1010 )
11
12 func init() {
13 rand.Seed(time.Now().UnixNano())
14 }
1511
1612 func main() {
1713 var wg sync.WaitGroup
4541 // simulating some work
4642 go func() {
4743 defer wg.Done()
44 rng := rand.New(rand.NewSource(time.Now().UnixNano()))
4845 max := 100 * time.Millisecond
4946 for i := 0; i < total; i++ {
5047 start := time.Now()
51 time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
48 time.Sleep(time.Duration(rng.Intn(10)+1) * max / 10)
5249 // since ewma decorator is used, we need to pass time.Since(start)
5350 bar.Increment(time.Since(start))
5451 }
88 "github.com/vbauerster/mpb/v4"
99 "github.com/vbauerster/mpb/v4/decor"
1010 )
11
12 func init() {
13 rand.Seed(time.Now().UnixNano())
14 }
1511
1612 func main() {
1713 var wg sync.WaitGroup
4642 // simulating some work
4743 go func() {
4844 defer wg.Done()
45 rng := rand.New(rand.NewSource(time.Now().UnixNano()))
4946 max := 100 * time.Millisecond
5047 for i := 0; i < total; i++ {
5148 start := time.Now()
52 time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
49 time.Sleep(time.Duration(rng.Intn(10)+1) * max / 10)
5350 // since ewma decorator is used, we need to pass time.Since(start)
5451 bar.Increment(time.Since(start))
5552 }
88 "github.com/vbauerster/mpb/v4"
99 "github.com/vbauerster/mpb/v4/decor"
1010 )
11
12 func init() {
13 rand.Seed(time.Now().UnixNano())
14 }
1511
1612 func main() {
1713 var wg sync.WaitGroup
4036 // simulating some work
4137 go func() {
4238 defer wg.Done()
39 rng := rand.New(rand.NewSource(time.Now().UnixNano()))
4340 max := 100 * time.Millisecond
4441 for i := 0; i < total; i++ {
4542 start := time.Now()
46 time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
43 time.Sleep(time.Duration(rng.Intn(10)+1) * max / 10)
4744 // since ewma decorator is used, we need to pass time.Since(start)
4845 bar.Increment(time.Since(start))
4946 }
1111 )
1212
1313 func main() {
14 rand.Seed(time.Now().UnixNano())
1514 p := mpb.New(mpb.PopCompletedMode())
1615
1716 total, numBars := 100, 2
3130 )
3231 // simulating some work
3332 go func() {
33 rng := rand.New(rand.NewSource(time.Now().UnixNano()))
3434 max := 100 * time.Millisecond
3535 for i := 0; i < total; i++ {
3636 start := time.Now()
37 time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
37 time.Sleep(time.Duration(rng.Intn(10)+1) * max / 10)
3838 bar.Increment(time.Since(start))
3939 }
4040 }()
4444 wg.Add(1)
4545 go func() {
4646 defer wg.Done()
47 rng := rand.New(rand.NewSource(time.Now().UnixNano()))
4748 max := 3000 * time.Millisecond
4849 for i := 0; i < 10; i++ {
4950 p.Add(0, makeLogBar(fmt.Sprintf("some log: %d", i))).SetTotal(0, true)
50 time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
51 time.Sleep(time.Duration(rng.Intn(10)+1) * max / 10)
5152 }
5253 }()
5354
1313 var quietMode bool
1414
1515 func init() {
16 rand.Seed(time.Now().UnixNano())
1716 flag.BoolVar(&quietMode, "q", false, "quiet mode")
1817 }
1918
5554 // simulating some work
5655 go func() {
5756 defer wg.Done()
57 rng := rand.New(rand.NewSource(time.Now().UnixNano()))
5858 max := 100 * time.Millisecond
5959 for i := 0; i < total; i++ {
6060 start := time.Now()
61 time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
61 time.Sleep(time.Duration(rng.Intn(10)+1) * max / 10)
6262 // since ewma decorator is used, we need to pass time.Since(start)
6363 bar.Increment(time.Since(start))
6464 }
88 "github.com/vbauerster/mpb/v4"
99 "github.com/vbauerster/mpb/v4/decor"
1010 )
11
12 func init() {
13 rand.Seed(time.Now().UnixNano())
14 }
1511
1612 func main() {
1713 var wg sync.WaitGroup
3228 )
3329 go func() {
3430 defer wg.Done()
31 rng := rand.New(rand.NewSource(time.Now().UnixNano()))
3532 max := 100 * time.Millisecond
3633 for i := 0; !b.Completed(); i++ {
3734 start := time.Now()
3936 // aborting and removing while bar is running
4037 b.Abort(true)
4138 }
42 time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
39 time.Sleep(time.Duration(rng.Intn(10)+1) * max / 10)
4340 // since ewma decorator is used, we need to pass time.Since(start)
4441 b.Increment(time.Since(start))
4542 }
88 "github.com/vbauerster/mpb/v4"
99 "github.com/vbauerster/mpb/v4/decor"
1010 )
11
12 func init() {
13 rand.Seed(time.Now().UnixNano())
14 }
1511
1612 func main() {
1713 var wg sync.WaitGroup
4339 // simulating some work
4440 go func() {
4541 defer wg.Done()
42 rng := rand.New(rand.NewSource(time.Now().UnixNano()))
4643 max := 100 * time.Millisecond
4744 for i := 0; i < total; i++ {
4845 start := time.Now()
49 time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
46 time.Sleep(time.Duration(rng.Intn(10)+1) * max / 10)
5047 // since ewma decorator is used, we need to pass time.Since(start)
5148 bar.Increment(time.Since(start))
5249 }
3838 }
3939 // wait for our bar to complete and flush
4040 p.Wait()
41 p.Wait()
4241 }
+0
-5
_examples/sortBars/go.mod less more
0 module github.com/vbauerster/mpb/_examples/sortBars
1
2 go 1.13
3
4 require github.com/vbauerster/mpb/v4 v4.9.3
+0
-53
_examples/sortBars/main.go less more
0 package main
1
2 import (
3 "fmt"
4 "math/rand"
5 "sync"
6 "time"
7
8 "github.com/vbauerster/mpb/v4"
9 "github.com/vbauerster/mpb/v4/decor"
10 )
11
12 func init() {
13 rand.Seed(time.Now().UnixNano())
14 }
15
16 func main() {
17 var wg sync.WaitGroup
18 p := mpb.New(mpb.WithWaitGroup(&wg))
19 total := 100
20 numBars := 3
21 wg.Add(numBars)
22
23 for i := 0; i < numBars; i++ {
24 name := fmt.Sprintf("Bar#%d:", i)
25 b := p.AddBar(int64(total),
26 mpb.BarOptOnCond(mpb.BarWidth(40), func() bool { return i > 0 }),
27 mpb.PrependDecorators(
28 decor.Name(name, decor.WCSyncWidth),
29 decor.CountersNoUnit("%d / %d", decor.WCSyncSpace),
30 ),
31 mpb.AppendDecorators(
32 decor.EwmaETA(decor.ET_STYLE_GO, 60, decor.WC{W: 3}),
33 ),
34 )
35 go func() {
36 defer wg.Done()
37 max := 100 * time.Millisecond
38 for i := 0; i < total; i++ {
39 start := time.Now()
40 time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
41 if i&1 == 1 {
42 priority := total - int(b.Current())
43 b.SetPriority(priority)
44 }
45 // since ewma decorator is used, we need to pass time.Since(start)
46 b.Increment(time.Since(start))
47 }
48 }()
49 }
50
51 p.Wait()
52 }
88 "github.com/vbauerster/mpb/v4"
99 "github.com/vbauerster/mpb/v4/decor"
1010 )
11
12 func init() {
13 rand.Seed(time.Now().UnixNano())
14 }
1511
1612 func main() {
1713 var wg sync.WaitGroup
6258 // simulating some work
6359 go func() {
6460 defer wg.Done()
61 rng := rand.New(rand.NewSource(time.Now().UnixNano()))
6562 max := 100 * time.Millisecond
6663 for i := 0; i < total; i++ {
6764 start := time.Now()
68 time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
65 time.Sleep(time.Duration(rng.Intn(10)+1) * max / 10)
6966 // since ewma decorator is used, we need to pass time.Since(start)
7067 bar.Increment(time.Since(start))
7168 }
88 "github.com/vbauerster/mpb/v4"
99 "github.com/vbauerster/mpb/v4/decor"
1010 )
11
12 func init() {
13 rand.Seed(time.Now().UnixNano())
14 }
1511
1612 func main() {
1713 var wg sync.WaitGroup
3935 // simulating some work
4036 go func() {
4137 defer wg.Done()
38 rng := rand.New(rand.NewSource(time.Now().UnixNano()))
4239 max := 100 * time.Millisecond
4340 for i := 0; i < total; i++ {
44 time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
41 time.Sleep(time.Duration(rng.Intn(10)+1) * max / 10)
4542 bar.Increment()
4643 }
4744 }()
1212 const (
1313 totalBars = 32
1414 )
15
16 func init() {
17 rand.Seed(time.Now().UnixNano())
18 }
1915
2016 func main() {
2117 var wg sync.WaitGroup
4238
4339 go func() {
4440 defer wg.Done()
41 rng := rand.New(rand.NewSource(time.Now().UnixNano()))
4542 max := 100 * time.Millisecond
4643 for !bar.Completed() {
47 time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
44 time.Sleep(time.Duration(rng.Intn(10)+1) * max / 10)
4845 bar.Increment()
4946 }
5047 }()
3232 mpb.AppendDecorators(decor.Percentage()),
3333 )
3434 // simulating some work
35 rng := rand.New(rand.NewSource(time.Now().UnixNano()))
3635 max := 100 * time.Millisecond
3736 for i := 0; i < total; i++ {
3837 start := time.Now()
39 time.Sleep(time.Duration(rng.Intn(10)+1) * max / 10)
38 time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
4039 // since ewma decorator is used, we need to pass time.Since(start)
4140 bar.Increment(time.Since(start))
4241 }
4847 p := mpb.New()
4948 bar := p.AddBar(100)
5049
51 rng := rand.New(rand.NewSource(time.Now().UnixNano()))
5250 max := 100 * time.Millisecond
5351 for !bar.Completed() {
54 time.Sleep(time.Duration(rng.Intn(10)+1) * max / 10)
52 time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
5553 bar.Increment()
5654 }
5755