如何运行包括子文件夹在内的所有 Golang 基准测试?

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

How to run all Golang benchmark tests including subfolders

问题

我有一个Go应用程序,其中包含一些单元测试和基准测试,分别位于根目录和名为"message"的子文件夹中。

我使用以下命令来运行根目录中的所有单元测试,包括"message"和其他子文件夹中的测试:

go test ./...

我希望能够对基准测试做同样的操作,即运行所有基准测试。以下命令可以运行根目录中的基准测试:

go test -bench .

但是,位于"/message"文件夹中的基准测试被忽略了,这是预期的。所以我尝试从根目录运行以下命令:

go test -bench ./...

但是,这个命令完全没有被识别,Go似乎执行的是位于根目录中的单元测试。我甚至尝试在命令中指定"message"文件夹,如下所示:

go test -bench ./message

...但是也失败了。目前,如果我想运行"message"文件夹中的基准测试,我必须进入该文件夹并执行以下命令:

go test -bench .

那么正确的方法是什么呢?如何告诉Go在根目录和子文件夹中查找基准测试?在"-bench"标志的正则表达式参数中如何工作?显然,它与单元测试运行器的正则表达式不同。

英文:

I have a Go application with a number of unit and benchmark tests both in the root and in a subfolder called "message".

I execute the following command to run all unit tests from the root including the ones in the messages and any other subfolder:

go test ./...

I want to achieve the same for the benchmark tests, i.e. run them all. The following works for the ones in the root directory:

go test -bench .

The benchmark tests in the /messages folder are ignored which is expected. So I run the following from the root:

go test -bench ./...

That's not recognised at all, Go seems to execute the unit tests that are located in the root dir. I even tried to specify the message folder in the command as follows:

go test -bench ./message

...but it also failed. Currently if I want to run the benchmark tests in the message folder I have to cd into that folder and execute

go test -bench .

like above.

So what's the correct way then? How can I tell Go to find the benchmark tests both in the root and the subfolders? How does the regexp arg work in the case of the -bench flag? Apparently it's different from the regexp for the unit test runner.

答案1

得分: 8

你应该使用./...来对当前工作目录及其所有子目录中的所有文件进行基准测试。如果你希望获得更详细的输出,可以使用-v标志。此外,使用-benchmem可以列出内存分配情况。

go test -v ./... -bench=. -run=xxx -benchmem
英文:

You should use ./... to bench all the files from the current working directory and all of its subdirectories. If you wish to get a more verbose output you can use the -v flag. Also it's good to list the memory allocation by using -benchmem.

go test -v ./... -bench=. -run=xxx -benchmem

答案2

得分: 2

-bench标志接受正则表达式,因此要在所有包中运行所有基准测试(-bench .),可以使用以下命令:go test -bench=. ./...

英文:

-bench flag takes regex so to run all benchmarks (-bench .) in all packages: go test -bench=. ./...

huangapple
  • 本文由 发表于 2022年1月27日 20:17:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/70878439.html
匿名

发表评论

匿名网友

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

确定