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 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿