vbauerster / mpb

multi progress bar for Go cli applications
The Unlicense
2.29k stars 123 forks source link

[bytes counter] when use EwmaSpeed ,speed and remain time calculate is wrong? #120

Closed lixd closed 1 year ago

lixd commented 1 year ago

the bar like this:

1.89 GiB / 2.00 GiB 95 % [======================================================>---]  ] 0s8.06 GiB/s

time is 0s,speed is 8.06 GiB/s,but really speed is less than 600MiB/s

when use AverageSpeed,is ok,bar like this:

1.94 GiB / 2.00 GiB 97 % [=======================================================>--]  ] 548.14 MiB/s

and test bytes counter example ,is ok.

my code like this

        // create a tmp file to read
    file, err := os.Create("./tmp.txt")
    if err != nil {
        return
    }
    data := make([]byte, 1024*1024*1024*2)
    file.Write(data)
    file.Close()
    defer func() {
        os.Remove("./tmp.txt")
        os.Remove("./tmp2.txt")
    }()

    p := mpb.New(
        mpb.WithWidth(60),
        mpb.WithRefreshRate(180*time.Millisecond),
    )

    bar := p.New(0,
        mpb.BarStyle(),
        mpb.PrependDecorators(
            decor.CountersKibiByte("% .2f / % .2f"),
            decor.Percentage(decor.WCSyncSpace),
        ),
        mpb.AppendDecorators(
            decor.Name(" ] "),
            decor.EwmaETA(decor.ET_STYLE_GO, 90),
            decor.EwmaSpeed(decor.UnitKiB, "% .2f", decor.DSyncSpace),
            //decor.AverageSpeed(decor.UnitKiB, "% .2f", decor.WCSyncWidth),
        ),
    )

    // file to reader
    open, err := os.Open("./tmp.txt")
    if err != nil {
        panic(err)
    }
    stat, err := open.Stat()
    if err != nil {
        panic(err)
    }
    // set total as file size
    fmt.Println("size: ", stat.Size())
    bar.SetTotal(stat.Size(), false)

    // create proxy reader
    proxyReader := bar.ProxyReader(open)
    //defer proxyReader.Close()

    // copy from proxyReader, ignoring errors
    create, err := os.Create("./tmp2.txt")
    if err != nil {
        panic(err)
    }
    defer create.Close()

    n, _ := io.Copy(create, proxyReader)

    // triggering complete event now
    bar.SetTotal(-1, true)
vbauerster commented 1 year ago

Please refer to documentation of ewma lib.

TLDR You have to pick correct weight parameter. If you choose anything than 30, ewma lib will need to collect 10 samples before any result. In your code sample you've chosen 90 and got 0 result, which means there were less than 10 samples.

lixd commented 1 year ago

@vbauerster thanks for your replay,but why this example bytes counter example it ok, params in my code is same with this. what need i do to fix my code? help~

vbauerster commented 1 year ago

use 0 as arg to ewma, that will default to implementation that doesn't require prerequisite samples.

https://github.com/vbauerster/mpb/blob/314158faaf7557a31a22ab69bd491799d46f2e3f/decor/eta.go#L30

lixd commented 1 year ago

Thanks,but unfortunately,i tried use 0 as arg to ewma,,but not work too.