Codebase list golang-github-vbauerster-mpb / 010b124
better bar bench test Vladimir Bauer 4 years ago
1 changed file(s) with 43 addition(s) and 28 deletion(s). Raw diff Collapse all Expand all
00 package mpb
11
22 import (
3 "io/ioutil"
3 "sync"
44 "testing"
5
6 "github.com/vbauerster/mpb/v7/decor"
75 )
86
9 func BenchmarkIncrSingleBar(b *testing.B) {
10 p := New(WithOutput(ioutil.Discard), WithWidth(80))
11 bar := p.AddBar(int64(b.N))
12 for i := 0; i < b.N; i++ {
13 bar.Increment()
14 }
7 const total = 1000
8
9 func BenchmarkIncrementOneBar(b *testing.B) {
10 benchBody(1, b)
1511 }
1612
17 func BenchmarkIncrSingleBarWhileIsNotCompleted(b *testing.B) {
18 p := New(WithOutput(ioutil.Discard), WithWidth(80))
19 bar := p.AddBar(int64(b.N))
20 for !bar.Completed() {
21 bar.Increment()
22 }
13 func BenchmarkIncrementTwoBars(b *testing.B) {
14 benchBody(2, b)
2315 }
2416
25 func BenchmarkIncrSingleBarWithNameDecorator(b *testing.B) {
26 p := New(WithOutput(ioutil.Discard), WithWidth(80))
27 bar := p.AddBar(int64(b.N), PrependDecorators(decor.Name("test")))
28 for i := 0; i < b.N; i++ {
29 bar.Increment()
30 }
17 func BenchmarkIncrementThreeBars(b *testing.B) {
18 benchBody(3, b)
3119 }
3220
33 func BenchmarkIncrSingleBarWithNameAndEwmaETADecorator(b *testing.B) {
34 p := New(WithOutput(ioutil.Discard), WithWidth(80))
35 bar := p.AddBar(int64(b.N),
36 PrependDecorators(decor.Name("test")),
37 AppendDecorators(decor.EwmaETA(decor.ET_STYLE_GO, 60)),
38 )
21 func BenchmarkIncrementFourBars(b *testing.B) {
22 benchBody(4, b)
23 }
24
25 func benchBody(n int, b *testing.B) {
26 p := New(WithOutput(nil), WithWidth(80))
27 wg := new(sync.WaitGroup)
28 b.ResetTimer()
3929 for i := 0; i < b.N; i++ {
40 bar.Increment()
30 for j := 0; j < n; j++ {
31 switch j {
32 case n - 1:
33 bar := p.AddBar(total)
34 for c := 0; c < total; c++ {
35 bar.Increment()
36 }
37 if !bar.Completed() {
38 b.Fail()
39 }
40 default:
41 wg.Add(1)
42 go func() {
43 bar := p.AddBar(total)
44 for c := 0; c < total; c++ {
45 bar.Increment()
46 }
47 if !bar.Completed() {
48 b.Fail()
49 }
50 wg.Done()
51 }()
52 }
53 }
54 wg.Wait()
4155 }
56 p.Wait()
4257 }