Codebase list golang-github-vbauerster-mpb / f2d12d2
stress test example Vladimir Bauer 9 years ago
1 changed file(s) with 49 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 package main
1
2 import (
3 "fmt"
4 "math/rand"
5 "sync"
6 "time"
7
8 "github.com/vbauerster/mpb"
9 )
10
11 const (
12 totalBars = 32
13 maxBlockSize = 12
14 )
15
16 func main() {
17
18 p := mpb.New(nil)
19 var wg sync.WaitGroup
20 wg.Add(totalBars)
21
22 for i := 0; i < totalBars; i++ {
23 name := fmt.Sprintf("Bar#%02d: ", i)
24 total := rand.Intn(120) + 10
25 bar := p.AddBar(int64(total)).
26 PrependName(name, len(name)).PrependETA(4).
27 AppendPercentage().TrimRightSpace()
28
29 go func() {
30 defer wg.Done()
31 blockSize := rand.Intn(maxBlockSize) + 1
32 for i := 0; i < total; i++ {
33 sleep(blockSize)
34 bar.Incr(1)
35 blockSize = rand.Intn(maxBlockSize) + 1
36 }
37 }()
38 }
39
40 wg.Wait()
41 p.Stop()
42 // p.AddBar(1) // panic: you cannot reuse p, create new one!
43 fmt.Println("stop")
44 }
45
46 func sleep(blockSize int) {
47 time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond)))))
48 }