Skip to content

Commit

Permalink
refactoring: barbench_test
Browse files Browse the repository at this point in the history
  • Loading branch information
vbauerster committed Mar 26, 2024
1 parent 81dfad3 commit e94c4bc
Showing 1 changed file with 21 additions and 25 deletions.
46 changes: 21 additions & 25 deletions barbench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,92 +2,88 @@ package mpb_test

import (
"io"
"sync"
"testing"

"github.com/vbauerster/mpb/v8"
)

const total = 1000

func BenchmarkNopStyle1Bar(b *testing.B) {
func BenchmarkNopStyleB1(b *testing.B) {
bench(b, mpb.NopStyle(), false, 1)
}

func BenchmarkNopStyle1BarWithAutoRefresh(b *testing.B) {
func BenchmarkNopStyleWithAutoRefreshB1(b *testing.B) {
bench(b, mpb.NopStyle(), true, 1)
}

func BenchmarkNopStyle2Bars(b *testing.B) {
func BenchmarkNopStylesB2(b *testing.B) {
bench(b, mpb.NopStyle(), false, 2)
}

func BenchmarkNopStyle2BarsWithAutoRefresh(b *testing.B) {
func BenchmarkNopStylesWithAutoRefreshB2(b *testing.B) {
bench(b, mpb.NopStyle(), true, 2)
}

func BenchmarkNopStyle3Bars(b *testing.B) {
func BenchmarkNopStylesB3(b *testing.B) {
bench(b, mpb.NopStyle(), false, 3)
}

func BenchmarkNopStyle3BarsWithAutoRefresh(b *testing.B) {
func BenchmarkNopStylesWithAutoRefreshB3(b *testing.B) {
bench(b, mpb.NopStyle(), true, 3)
}

func BenchmarkBarStyle1Bar(b *testing.B) {
func BenchmarkBarStyleB1(b *testing.B) {
bench(b, mpb.BarStyle(), false, 1)
}

func BenchmarkBarStyle1BarWithAutoRefresh(b *testing.B) {
func BenchmarkBarStyleWithAutoRefreshB1(b *testing.B) {
bench(b, mpb.BarStyle(), true, 1)
}

func BenchmarkBarStyle2Bars(b *testing.B) {
func BenchmarkBarStylesB2(b *testing.B) {
bench(b, mpb.BarStyle(), false, 2)
}

func BenchmarkBarStyle2BarsWithAutoRefresh(b *testing.B) {
func BenchmarkBarStylesWithAutoRefreshB2(b *testing.B) {
bench(b, mpb.BarStyle(), true, 2)
}

func BenchmarkBarStyle3Bars(b *testing.B) {
func BenchmarkBarStylesB3(b *testing.B) {
bench(b, mpb.BarStyle(), false, 3)
}

func BenchmarkBarStyle3BarsWithAutoRefresh(b *testing.B) {
func BenchmarkBarStylesWithAutoRefreshB3(b *testing.B) {
bench(b, mpb.BarStyle(), true, 3)
}

func bench(b *testing.B, builder mpb.BarFillerBuilder, autoRefresh bool, n int) {
var wg sync.WaitGroup
p := mpb.New(
mpb.WithWidth(100),
mpb.WithOutput(io.Discard),
mpb.ContainerOptional(mpb.WithAutoRefresh(), autoRefresh),
)
defer p.Wait()
b.ResetTimer()
for i := 0; i < b.N; i++ {
var bars []*mpb.Bar
for j := 0; j < n; j++ {
bar := p.New(total, builder)
bars = append(bars, p.New(total, builder))
switch j {
case n - 1:
complete(b, bar)
complete(bars[j])
default:
wg.Add(1)
go func() {
complete(b, bar)
wg.Done()
}()
go complete(bars[j])
}
}
wg.Wait()
for _, bar := range bars {
bar.Wait()
}
}
p.Wait()
}

func complete(b *testing.B, bar *mpb.Bar) {
func complete(bar *mpb.Bar) {
for i := 0; i < total; i++ {
bar.Increment()
}
bar.Wait()
}

0 comments on commit e94c4bc

Please sign in to comment.