diff --git a/README.md b/README.md index ef35437..41cef5b 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,7 @@ ) for i := 0; i < total; i++ { - time.Sleep(time.Duration(rand.Intn(10)+1) * time.Second / 100) + time.Sleep(time.Duration(rand.Intn(10)+1) * (200 * time.Millisecond) / 10) bar.Increment() } // Gracefully shutdown mpb's monitor goroutine @@ -86,13 +86,13 @@ decor.Percentage(3, decor.DSyncSpace), ), mpb.AppendDecorators( - decor.ETA(2, 0), + decor.ETA(3, 0), ), ) go func() { defer wg.Done() for i := 0; i < total; i++ { - time.Sleep(time.Duration(rand.Intn(10)+1) * time.Second / 100) + time.Sleep(time.Duration(rand.Intn(10)+1) * (200 * time.Millisecond) / 10) bar.Increment() } }() diff --git a/example_test.go b/example_test.go index 97e4346..7f0f9e8 100644 --- a/example_test.go +++ b/example_test.go @@ -39,7 +39,7 @@ ) for i := 0; i < total; i++ { - time.Sleep(time.Duration(rand.Intn(10)+1) * time.Second / 100) + time.Sleep(time.Duration(rand.Intn(10)+1) * (200 * time.Millisecond) / 10) bar.Increment() } @@ -51,7 +51,7 @@ bar := p.AddBar(100, mpb.AppendDecorators(decor.Percentage(5, 0))) for !bar.Completed() { - time.Sleep(time.Duration(rand.Intn(10)+1) * time.Second / 100) + time.Sleep(time.Duration(rand.Intn(10)+1) * (200 * time.Millisecond) / 10) bar.Increment() } } diff --git a/examples/cancel/main.go b/examples/cancel/main.go index 3cb28c2..b782a58 100644 --- a/examples/cancel/main.go +++ b/examples/cancel/main.go @@ -13,12 +13,11 @@ "github.com/vbauerster/mpb/decor" ) -const ( - maxBlockSize = 12 -) +func init() { + rand.Seed(time.Now().UnixNano()) +} func main() { - ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) defer cancel() @@ -27,13 +26,13 @@ mpb.WithWaitGroup(&wg), mpb.WithContext(ctx), ) - total := 100 + total := 300 numBars := 3 wg.Add(numBars) for i := 0; i < numBars; i++ { name := fmt.Sprintf("Bar#%d:", i) - bar := p.AddBar(int64(total), mpb.BarID(i), + bar := p.AddBar(int64(total), mpb.PrependDecorators( decor.StaticName(name, 0, decor.DwidthSync|decor.DidentRight), decor.ETA(4, decor.DSyncSpace), @@ -42,18 +41,12 @@ decor.Percentage(5, 0), ), ) + go func() { defer wg.Done() - blockSize := rand.Intn(maxBlockSize) + 1 - for i := 0; i < total; i++ { - select { - case <-ctx.Done(): - return - default: - } - sleep(blockSize) + for !bar.Completed() { + time.Sleep(randomDuration(200 * time.Millisecond)) bar.Increment() - blockSize = rand.Intn(maxBlockSize) + 1 } }() } @@ -62,6 +55,6 @@ fmt.Println("stop") } -func sleep(blockSize int) { - time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond))))) +func randomDuration(max time.Duration) time.Duration { + return time.Duration(rand.Intn(10)+1) * max / 10 } diff --git a/examples/dynTotal/main.go b/examples/dynTotal/main.go index 6f38a17..d4e13b2 100644 --- a/examples/dynTotal/main.go +++ b/examples/dynTotal/main.go @@ -7,6 +7,10 @@ "github.com/vbauerster/mpb" "github.com/vbauerster/mpb/decor" ) + +func init() { + rand.Seed(time.Now().UnixNano()) +} func main() { p := mpb.New() @@ -43,7 +47,7 @@ if i == 250 { close(totalUpd2) } - time.Sleep(time.Duration(rand.Intn(10)+1) * time.Second / 100) + time.Sleep(time.Duration(rand.Intn(10)+1) * (200 * time.Millisecond) / 10) bar.Increment() } diff --git a/examples/prependETA/main.go b/examples/prependETA/main.go index 0589662..3a2c5d7 100644 --- a/examples/prependETA/main.go +++ b/examples/prependETA/main.go @@ -10,12 +10,11 @@ "github.com/vbauerster/mpb/decor" ) -const ( - maxBlockSize = 12 -) +func init() { + rand.Seed(time.Now().UnixNano()) +} func main() { - var wg sync.WaitGroup p := mpb.New(mpb.WithWaitGroup(&wg)) total := 100 @@ -38,11 +37,9 @@ ) go func() { defer wg.Done() - blockSize := rand.Intn(maxBlockSize) + 1 for i := 0; i < total; i++ { - sleep(blockSize) + time.Sleep(time.Duration(rand.Intn(10)+1) * (200 * time.Millisecond) / 10) b.Increment() - blockSize = rand.Intn(maxBlockSize) + 1 } }() } @@ -50,7 +47,3 @@ p.Stop() fmt.Println("stop") } - -func sleep(blockSize int) { - time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond))))) -} diff --git a/examples/prependElapsed/main.go b/examples/prependElapsed/main.go index 2f3af50..e0a336a 100644 --- a/examples/prependElapsed/main.go +++ b/examples/prependElapsed/main.go @@ -10,9 +10,9 @@ "github.com/vbauerster/mpb/decor" ) -const ( - maxBlockSize = 12 -) +func init() { + rand.Seed(time.Now().UnixNano()) +} func main() { @@ -38,11 +38,9 @@ ) go func() { defer wg.Done() - blockSize := rand.Intn(maxBlockSize) + 1 for i := 0; i < total; i++ { - sleep(blockSize) + time.Sleep(time.Duration(rand.Intn(10)+1) * (200 * time.Millisecond) / 10) b.Increment() - blockSize = rand.Intn(maxBlockSize) + 1 } }() } @@ -50,7 +48,3 @@ p.Stop() fmt.Println("stop") } - -func sleep(blockSize int) { - time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond))))) -} diff --git a/examples/remove/main.go b/examples/remove/main.go index f3c4456..a7a375c 100644 --- a/examples/remove/main.go +++ b/examples/remove/main.go @@ -10,12 +10,11 @@ "github.com/vbauerster/mpb/decor" ) -const ( - maxBlockSize = 12 -) +func init() { + rand.Seed(time.Now().UnixNano()) +} func main() { - var wg sync.WaitGroup p := mpb.New(mpb.WithWaitGroup(&wg)) total := 100 @@ -38,11 +37,9 @@ ) go func() { defer wg.Done() - blockSize := rand.Intn(maxBlockSize) + 1 for i := 0; i < total; i++ { - sleep(blockSize) + time.Sleep(time.Duration(rand.Intn(10)+1) * (200 * time.Millisecond) / 10) b.Increment() - blockSize = rand.Intn(maxBlockSize) + 1 if b.ID() == 1 && i >= 42 { p.RemoveBar(b) return @@ -54,7 +51,3 @@ p.Stop() fmt.Println("stop") } - -func sleep(blockSize int) { - time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond))))) -} diff --git a/examples/simple/main.go b/examples/simple/main.go index c53ef9f..ccb99a8 100644 --- a/examples/simple/main.go +++ b/examples/simple/main.go @@ -9,6 +9,10 @@ "github.com/vbauerster/mpb" "github.com/vbauerster/mpb/decor" ) + +func init() { + rand.Seed(time.Now().UnixNano()) +} func main() { var wg sync.WaitGroup @@ -28,13 +32,13 @@ decor.Percentage(3, decor.DSyncSpace), ), mpb.AppendDecorators( - decor.ETA(2, 0), + decor.ETA(3, 0), ), ) go func() { defer wg.Done() for i := 0; i < total; i++ { - time.Sleep(time.Duration(rand.Intn(10)+1) * time.Second / 100) + time.Sleep(time.Duration(rand.Intn(10)+1) * (200 * time.Millisecond) / 10) bar.Increment() } }() diff --git a/examples/singleBar/main.go b/examples/singleBar/main.go index 44b9388..8305d7b 100644 --- a/examples/singleBar/main.go +++ b/examples/singleBar/main.go @@ -39,7 +39,7 @@ ) for i := 0; i < total; i++ { - time.Sleep(time.Duration(rand.Intn(10)+1) * time.Second / 100) + time.Sleep(time.Duration(rand.Intn(10)+1) * (200 * time.Millisecond) / 10) bar.Increment() } // Gracefully shutdown mpb's monitor goroutine diff --git a/examples/sort/main.go b/examples/sort/main.go index 3bdda2e..03a54c5 100644 --- a/examples/sort/main.go +++ b/examples/sort/main.go @@ -10,9 +10,9 @@ "github.com/vbauerster/mpb/decor" ) -const ( - maxBlockSize = 12 -) +func init() { + rand.Seed(time.Now().UnixNano()) +} func main() { var wg sync.WaitGroup @@ -37,14 +37,13 @@ ) go func() { defer wg.Done() - for blockSize, i := 0, 0; i < total; i++ { - blockSize = rand.Intn(maxBlockSize) + 1 + for i := 0; i < total; i++ { + time.Sleep(time.Duration(rand.Intn(10)+1) * (200 * time.Millisecond) / 10) if i&1 == 1 { priority := total - int(b.Current()) p.UpdateBarPriority(b, priority) } b.Increment() - sleep(blockSize) } }() } @@ -52,7 +51,3 @@ p.Stop() fmt.Println("stop") } - -func sleep(blockSize int) { - time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond))))) -} diff --git a/examples/stress/main.go b/examples/stress/main.go index 3ef9e6d..330d45d 100644 --- a/examples/stress/main.go +++ b/examples/stress/main.go @@ -11,9 +11,12 @@ ) const ( - totalBars = 32 - maxBlockSize = 8 + totalBars = 32 ) + +func init() { + rand.Seed(time.Now().UnixNano()) +} func main() { @@ -36,11 +39,9 @@ go func() { defer wg.Done() - blockSize := rand.Intn(maxBlockSize) + 1 for i := 0; i < total; i++ { - sleep(blockSize) + time.Sleep(time.Duration(rand.Intn(10)+1) * (200 * time.Millisecond) / 10) bar.Increment() - blockSize = rand.Intn(maxBlockSize) + 1 } }() } @@ -48,7 +49,3 @@ p.Stop() fmt.Println("stop") } - -func sleep(blockSize int) { - time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond))))) -}