dongluanban3536 2018-11-27 05:34
浏览 109
已采纳

从项目根目录运行子文件夹基准

I have a project with a binary which entrypoint is located in ./cmd/<my-project>/main.go. I've added a benchmark in ./cmd/<my-project>/main_test.go.

Now I want to execute this benchmark from the repository root. I've tried to execute something like go test -bench=./cmd/<my-project>/main in various alternations (like for example go test -bench=./cmd/<my-project>/main_test.go or go test -bench=./cmd/<my-project>) but I couldn't get it to work.

The error I always get is:

can't load package: package github.com/<username>/<my-project>: no Go files in /home/<user>/go/src/github.com/<username>/<my-project>

and it's true, I have no *.go files in my project root.

All I came up with is changing into the directory first and run the benchmark with `go test -bench=.

However, as my program depends on the current working directory and the result is heavily depending on that it would be great to be able to execute it in another directory than the cmd/<my-project> one. (If this is good or not isn't part of the question :))

Update after the first comment:

I get it to run with go test -bench=. /cmd/<my-project> but the benchmark get's executed as if it would be run in the ./cmd/<my-project> directory. I've noticed similiar behaviour in 'normal tests' - but that wasn't a problem for me at any time.

So, my program does scan the current directory for files directories depending on the current working directory, this results in only two files found (main.go and main_test.go) where the benchmark isn't really helpful.

  • 写回答

1条回答 默认 最新

  • doucheng2210 2018-11-27 07:07
    关注

    So to run the benchmarks:

    go test -bench=. ./cmd/<my-project>
    

    However, you are wanting to control the working directory, so really you want to run it from somewhere else. What you are looking for is the -c flag. It will create a binary instead of running the tests. You can then run the binary (and therefore your tests and benchmarks) where you want.

    go test -c ./cmd/my_proj
    my_proj.test -test.bench=.
    

    NOTE: Flags are prefixed with test. when you compile the tests.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳