Skip to content

testing: benchmark performance misaligned for fast tests #30928

@kokes

Description

@kokes

What version of Go are you using (go version)?

$ go version
go version go1.12.1 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/ondrej.kokes/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/ondrej.kokes/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/Cellar/go/1.12.1/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.12.1/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/mk/dqryy0_947936yjsdw8jl6v00000gp/T/go-build420651873=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I ran some fast tests, where each iteration took less than a nanosecond. This led to the results table to be misaligned.

Here's a reproducible example:

package foo

import (
	"testing"
	"time"
)

func BenchmarkFoo(b *testing.B) {
	for j := 0; j < b.N; j++ {
		time.Sleep(time.Millisecond)
	}
}

func BenchmarkBar(b *testing.B) {
	for j := 0; j < b.N; j++ {

	}
}

What did you expect to see?

goos: darwin
goarch: amd64
BenchmarkFoo-4   	    1000	   1357941 ns/op
BenchmarkBar-4        2000000000	      0.32 ns/op

What did you see instead?

goos: darwin
goarch: amd64
BenchmarkFoo-4   	    1000	   1357941 ns/op
BenchmarkBar-4   	2000000000	         0.32 ns/op

Thoughts

I guess this has to do with performance numbers being printed as %8d, so anything larger than that will get shifted.

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.TestingAn issue that has been verified to require only test changes, not just a test failure.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions