nats-io / natscli

The NATS Command Line Interface
Apache License 2.0
472 stars 95 forks source link

Uiprogress runtime error during "nats bench" when the number of publishers exceeds the number of messages #1134

Closed giacomoquinalia closed 1 week ago

giacomoquinalia commented 2 weeks ago

Observed behavior

Running a command like nats bench foo.bar --pub 2 --msgs 1 I receive the output:

10:51:24 Starting Core NATS pub/sub benchmark [subject=foo.bar, multisubject=false, multisubjectmax=100000, msgs=1, msgsize=128 B, pubs=2, subs=0, pubsleep=0s, subsleep=0s]
10:51:24 Starting publisher, publishing 1 messages
10:51:24 Starting publisher, publishing 0 messages
panic: runtime error: index out of range [0] with length 0

goroutine 39 [running]:
github.com/gosuri/uiprogress.(*Bar).Bytes(0xc000254000)
    /home/giacomo/go/pkg/mod/github.com/gosuri/uiprogress@v0.0.1/bar.go:195 +0x40b
github.com/gosuri/uiprogress.(*Bar).String(...)
    /home/giacomo/go/pkg/mod/github.com/gosuri/uiprogress@v0.0.1/bar.go:214
github.com/gosuri/uiprogress.(*Progress).print(0xc0000a2480)
    /home/giacomo/go/pkg/mod/github.com/gosuri/uiprogress@v0.0.1/progress.go:127 +0x8f
github.com/gosuri/uiprogress.(*Progress).Listen(0xc0000a2480)
    /home/giacomo/go/pkg/mod/github.com/gosuri/uiprogress@v0.0.1/progress.go:116 +0xb3
created by github.com/gosuri/uiprogress.(*Progress).Start in goroutine 1
    /home/giacomo/go/pkg/mod/github.com/gosuri/uiprogress@v0.0.1/progress.go:134 +0x4f

Expected behavior

To prevent a runtime error when the number of messages for a publisher is zero, the progress bar could be shown as already completed instead of attempting to update it. A possible solution:

File: https://github.com/nats-io/natscli/blob/main/cli/bench_command.go#L1718

    if c.progressBar {
        barCount := numMsg
        if barCount == 0 {
            barCount = 1
        }

        progress = uiprogress.AddBar(barCount).AppendCompleted().PrependElapsed()
        progress.Width = progressWidth()

        if numMsg == 0 {
            progress.PrependFunc(func(b *uiprogress.Bar) string {
                return "Finished  "
            })
            progress.Incr()
        }
    }

    if numMsg == 0 {
        donewg.Done()
        errChan <- nil
        return
    }

Server and client version

NATS server: 2.10.20 CLI client: v0.1.5

Host environment

No response

Steps to reproduce

No response

ripienaar commented 2 weeks ago

Thanks @giacomoquinalia, feel like sending a PR with that in?

giacomoquinalia commented 2 weeks ago

Sure, here it is: https://github.com/nats-io/natscli/pull/1138