Codebase list golang-github-vbauerster-mpb / f659025
example_test upd Vladimir Bauer 6 years ago
1 changed file(s) with 14 addition(s) and 12 deletion(s). Raw diff Collapse all Expand all
00 package mpb_test
11
22 import (
3 crand "crypto/rand"
34 "io"
45 "io/ioutil"
56 "math/rand"
6 "net/http"
77 "time"
88
99 "github.com/vbauerster/mpb/v4"
3232 mpb.AppendDecorators(decor.Percentage()),
3333 )
3434 // simulating some work
35 rng := rand.New(rand.NewSource(time.Now().UnixNano()))
3536 max := 100 * time.Millisecond
3637 for i := 0; i < total; i++ {
3738 start := time.Now()
38 time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
39 time.Sleep(time.Duration(rng.Intn(10)+1) * max / 10)
3940 // since ewma decorator is used, we need to pass time.Since(start)
4041 bar.Increment(time.Since(start))
4142 }
4748 p := mpb.New()
4849 bar := p.AddBar(100)
4950
51 rng := rand.New(rand.NewSource(time.Now().UnixNano()))
5052 max := 100 * time.Millisecond
5153 for !bar.Completed() {
52 time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
54 time.Sleep(time.Duration(rng.Intn(10)+1) * max / 10)
5355 bar.Increment()
5456 }
5557
5759 }
5860
5961 func ExampleBar_ProxyReader() {
62 var total int64 = 1024 * 1024 * 500
63 // crand is "crypto/rand"
64 reader := io.LimitReader(crand.Reader, total)
65
6066 p := mpb.New()
61 // make http get request, ignoring errors
62 resp, _ := http.Get("https://homebrew.bintray.com/bottles/libtiff-4.0.7.sierra.bottle.tar.gz")
63 defer resp.Body.Close()
64
65 // Assuming ContentLength > 0
66 bar := p.AddBar(resp.ContentLength,
67 bar := p.AddBar(total,
6768 mpb.AppendDecorators(
68 decor.CountersKibiByte("%6.1f / %6.1f"),
69 decor.CountersKibiByte("% .2f / % .2f"),
6970 ),
7071 )
7172
7273 // create proxy reader
73 reader := bar.ProxyReader(resp.Body)
74 proxyReader := bar.ProxyReader(reader)
75 defer proxyReader.Close()
7476
7577 // and copy from reader, ignoring errors
76 io.Copy(ioutil.Discard, reader)
78 io.Copy(ioutil.Discard, proxyReader)
7779
7880 p.Wait()
7981 }