Codebase list golang-github-vbauerster-mpb / 38828e4
Some benchmarks and panic example Vladimir Bauer 8 years ago
2 changed file(s) with 83 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 package mpb
1
2 import (
3 "io/ioutil"
4 "testing"
5 )
6
7 func benchmarkSingleBar(total int) {
8 p := New(Output(ioutil.Discard))
9 bar := p.AddBar(int64(total))
10 for i := 0; i < total; i++ {
11 bar.Increment()
12 }
13 p.Stop()
14 }
15
16 func BenchmarkSingleBar100(b *testing.B) {
17 for i := 0; i < b.N; i++ {
18 benchmarkSingleBar(100)
19 }
20 }
21
22 func BenchmarkSingleBar1000(b *testing.B) {
23 for i := 0; i < b.N; i++ {
24 benchmarkSingleBar(1000)
25 }
26 }
27
28 func BenchmarkSingleBar10000(b *testing.B) {
29 for i := 0; i < b.N; i++ {
30 benchmarkSingleBar(10000)
31 }
32 }
33
34 func BenchmarkIncrSingleBar(b *testing.B) {
35 p := New(Output(ioutil.Discard))
36 bar := p.AddBar(int64(b.N))
37 for i := 0; i < b.N; i++ {
38 bar.Increment()
39 }
40 p.Stop()
41 }
0 package main
1
2 import (
3 "fmt"
4 "sync"
5 "time"
6
7 "github.com/vbauerster/mpb"
8 "github.com/vbauerster/mpb/decor"
9 )
10
11 func main() {
12 var wg sync.WaitGroup
13 p := mpb.New(mpb.WithWaitGroup(&wg))
14
15 wantPanic := "Upps!!!"
16 numBars := 3
17 wg.Add(numBars)
18
19 for i := 0; i < numBars; i++ {
20 name := fmt.Sprintf("b#%02d:", i)
21 bar := p.AddBar(100, mpb.BarID(i), mpb.PrependDecorators(
22 func(s *decor.Statistics, _ chan<- int, _ <-chan int) string {
23 if s.ID == 2 && s.Current >= 42 {
24 panic(wantPanic)
25 }
26 return name
27 },
28 ))
29
30 go func() {
31 defer wg.Done()
32 for i := 0; i < 100; i++ {
33 time.Sleep(10 * time.Millisecond)
34 bar.Increment()
35 }
36 }()
37 }
38
39 p.Stop()
40 }