英文:
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 < b.N; i++ {
if i%100 == 0 {
fmt.Println("do something", i)
continue
}
fmt.Println("do other thing", i)
}
}
when i run command to benchmark it, it seem can not stop
go test -bench BenchmarkSimple
go env like this:
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"
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
).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论