golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.67k stars 17.62k forks source link

testing: Benchmark prints nothing on failure #32066

Open rogpeppe opened 5 years ago

rogpeppe commented 5 years ago

Benchmark functions can call Log, Error and Fatal, but if the benchmark is run with the Benchmark function, all messages are discarded.

https://play.golang.org/p/fd5Ed-il70V

Perhaps the messages should be printed to standard error?

fodil-a commented 5 years ago

Hi, I can confirm that the output is discarded, here is the writer used in the context of the benchmark https://github.com/golang/go/blob/fbc6a972226f889d2ab1150468755615098ee80f/src/testing/benchmark.go#L772

Calls from Log, Error, or Fatal will print nothing, because the Write call of the discard writer does nothing. https://github.com/golang/go/blob/fbc6a972226f889d2ab1150468755615098ee80f/src/testing/benchmark.go#L783-L785

lahsivjar commented 1 year ago

I stumbled upon this today, any updates on this? It feels like a pretty valid case and I was hoping it could do one of the following:

  1. testing.Benchmark takes io.Writer as an input.
  2. Allow users to create a custom testing.B and call Benchmark on top of it.
fodil-a commented 1 year ago

If anyone from the team agrees the the approach of creating a new func that take a io.Writer and uses it here https://github.com/golang/go/blob/fbc6a972226f889d2ab1150468755615098ee80f/src/testing/benchmark.go#L772 I can do the code, should be pretty easy to add and test 😊