better bar bench test
Vladimir Bauer
4 years ago
| 0 | 0 | package mpb |
| 1 | 1 | |
| 2 | 2 | import ( |
| 3 | "io/ioutil" | |
| 3 | "sync" | |
| 4 | 4 | "testing" |
| 5 | ||
| 6 | "github.com/vbauerster/mpb/v7/decor" | |
| 7 | 5 | ) |
| 8 | 6 | |
| 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) | |
| 15 | 11 | } |
| 16 | 12 | |
| 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) | |
| 23 | 15 | } |
| 24 | 16 | |
| 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) | |
| 31 | 19 | } |
| 32 | 20 | |
| 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() | |
| 39 | 29 | 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() | |
| 41 | 55 | } |
| 56 | p.Wait() | |
| 42 | 57 | } |