为什么我的Go基准测试函数无法停止?

huangapple go评论72阅读模式
英文:

Why my Go Benchmark function can not stop?

问题

为什么我的Go基准测试函数无法停止?
在Go中限制基准测试次数的正确方法是什么?

我想限制基准测试的运行次数,所以我编写了以下代码:

func BenchmarkSimple(b *testing.B) {
    b.N = 200
    for i := 0; i < b.N; i++ {
        if i%100 == 0 {
            fmt.Println("do something", i)
            continue
        }

        fmt.Println("do other thing", i)
    }
}

当我运行基准测试命令时,它似乎无法停止:

go test -bench BenchmarkSimple

go env 的输出如下:

GO111MODULE="auto"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/sinksmell/Library/Caches/go-build"
GOENV="/Users/sinksmell/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/sinksmell/go/pkg/mod"
GONOPROXY=""
GONOSUMDB="gopkg.mihoyo.com"
GOOS="darwin"
GOPATH="/Users/sinksmell/go"
GOPRIVATE=""
GOPROXY="https://goproxy.cn"
GOROOT="/usr/local/Cellar/go/1.16.5/libexec"
GOSUMDB="sum.golang.google.cn"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.16.5/libexec/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.16.5"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/r9/6n1x2r_s5kq9xncqdt_dplk40000gp/T/go-build471996324=/tmp/go-build -gno-record-gcc-switches -fno-common"

谢谢您的阅读!

英文:

Why my Go Benchmark function can not stop ? <br>
What is the right way to limit benchmark times in Go?

I want to limit benchmark run times, so i write code like this:

func BenchmarkSimple(b *testing.B) {
	b.N = 200
	for i := 0; i &lt; b.N; i++ {
		if i%100 == 0 {
			fmt.Println(&quot;do something&quot;, i)
			continue
		}

		fmt.Println(&quot;do other thing&quot;, i)
	}
}


when i run command to benchmark it, it seem can not stop

 go test -bench BenchmarkSimple

go env like this:


GO111MODULE=&quot;auto&quot;
GOARCH=&quot;amd64&quot;
GOBIN=&quot;&quot;
GOCACHE=&quot;/Users/sinksmell/Library/Caches/go-build&quot;
GOENV=&quot;/Users/sinksmell/Library/Application Support/go/env&quot;
GOEXE=&quot;&quot;
GOFLAGS=&quot;&quot;
GOHOSTARCH=&quot;amd64&quot;
GOHOSTOS=&quot;darwin&quot;
GOINSECURE=&quot;&quot;
GOMODCACHE=&quot;/Users/sinksmell/go/pkg/mod&quot;
GONOPROXY=&quot;&quot;
GONOSUMDB=&quot;gopkg.mihoyo.com&quot;
GOOS=&quot;darwin&quot;
GOPATH=&quot;/Users/sinksmell/go&quot;
GOPRIVATE=&quot;&quot;
GOPROXY=&quot;https://goproxy.cn&quot;
GOROOT=&quot;/usr/local/Cellar/go/1.16.5/libexec&quot;
GOSUMDB=&quot;sum.golang.google.cn&quot;
GOTMPDIR=&quot;&quot;
GOTOOLDIR=&quot;/usr/local/Cellar/go/1.16.5/libexec/pkg/tool/darwin_amd64&quot;
GOVCS=&quot;&quot;
GOVERSION=&quot;go1.16.5&quot;
GCCGO=&quot;gccgo&quot;
AR=&quot;ar&quot;
CC=&quot;clang&quot;
CXX=&quot;clang++&quot;
CGO_ENABLED=&quot;1&quot;
CGO_CFLAGS=&quot;-g -O2&quot;
CGO_CPPFLAGS=&quot;&quot;
CGO_CXXFLAGS=&quot;-g -O2&quot;
CGO_FFLAGS=&quot;-g -O2&quot;
CGO_LDFLAGS=&quot;-g -O2&quot;
PKG_CONFIG=&quot;pkg-config&quot;
GOGCCFLAGS=&quot;-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/r9/6n1x2r_s5kq9xncqdt_dplk40000gp/T/go-build471996324=/tmp/go-build -gno-record-gcc-switches -fno-common&quot;

Thanks for you read !

答案1

得分: 2

如@icza在评论中指出的那样,testing.B.N只能被读取,不能被你的代码写入。测试框架设置了N,告诉你的代码要运行多少次迭代。它测量运行N次迭代所需的时间,然后将总时间除以N得到每次迭代的时间。测试框架可能会多次调用你的基准函数,根据函数的执行时间和结果的一致性来变化N的值。

如@Adrian在评论中指出的那样,你可以使用-benchtime命令行标志来控制基准测试的时间。它可以设置为一个持续时间(例如-benchtime=60x)或一个迭代次数(例如-benchtime=1000x)。

英文:

As @icza pointed out in the comments, testing.B.N should only be read, not written by your code. The testing framework sets N, telling your code how many iterations to run. It measures the time it takes to run N iterations, then divides by N to get the time per iteration. The testing framework may call your benchmark function multiple times, varying N based on how long the function takes and how consistent the results are.

As @Adrian pointed out in the comments, you can control the time spent benchmarking with the -benchtime command line flag. It can be set to a duration (like -benchtime=60x) or a number of iterations (like -benchtime=1000x).

huangapple
  • 本文由 发表于 2021年8月10日 23:21:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/68729486.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定