为了账号安全,请及时绑定邮箱和手机立即绑定

“go test -cpuprofile”不会生成完整的跟踪

“go test -cpuprofile”不会生成完整的跟踪

Go
喵喔喔 2022-06-21 16:31:15
问题我有一个带有测试套件的 go 包。当我为此包运行测试套件时,总运行时间约为 7 秒:$ go test ./mydbpackage/ -count 1ok      mymodule/mydbpackage    7.253s但是,当我添加一个-cpuprofile=cpu.out选项时,采样不会覆盖整个运行:$ go test ./mydbpackage/ -count 1 -cpuprofile=cpu.outok      mymodule/mydbpackage    7.029s$ go tool pprof -text -cum cpu.outFile: mydbpackage.testType: cpuTime: Aug 6, 2020 at 9:42am (CEST)Duration: 5.22s, Total samples = 780ms (14.95%)     # <--- depending on the runs, I get 400ms to 1sShowing nodes accounting for 780ms, 100% of 780ms total      flat  flat%   sum%        cum   cum%         0     0%     0%      440ms 56.41%  testing.tRunner      10ms  1.28%  1.28%      220ms 28.21%  database/sql.withLock      10ms  1.28%  2.56%      180ms 23.08%  runtime.findrunnable         0     0%  2.56%      180ms 23.08%  runtime.mcall      ...查看收集的样本:# sample from another run :$ go tool pprof -traces cpu.out | grep "ms "  # get the first line of each sample      10ms   runtime.nanotime      10ms   fmt.(*readRune).ReadRune      30ms   syscall.Syscall      10ms   runtime.scanobject      10ms   runtime.gentraceback      ...# 98 samples collected, for a total sum of 1.12s我看到的问题是:由于某种原因,采样分析器停止收集样本,或者在某个时候被阻塞/减速。语境go版本是1.14.6,平台是linux/amd64$ go versiongo version go1.14.6 linux/amd64这个包包含与数据库交互的代码,测试是在一个实时的 postgresql 服务器上运行的。我尝试过的一件事:t.Skip()内部调用runtime.Goexit(),所以我用t.Skip简单的return;替换了对和变体的调用。但这并没有改变结果。问题为什么不收集更多样本?我有一些已知的模式会阻止/减慢采样器,或者提前终止采样器?
查看完整描述

1 回答

?
猛跑小猪

TA贡献1858条经验 获得超8个赞

-cpuprofile创建一个配置文件,其中仅对积极使用 CPU 的 goroutine 进行采样。

在我的用例中:我的 go 代码花费大量时间等待 postgresql 服务器的答案。

使用 生成跟踪go test -trace=trace.out,然后使用 提取网络阻塞配置文件go tool trace -pprof=net trace.out > network.out会产生更多相关信息。

作为参考,除了使用 打开完整跟踪go tool trace trace.out之外,您还可以传递以下值-pprof=

来自go tool trace文档

  • net:网络阻止配置文件

  • sync:同步阻塞配置文件

  • 系统调用:系统调用阻塞配置文件

  • sched:调度程序延迟配置文件


查看完整回答
反对 回复 2022-06-21
  • 1 回答
  • 0 关注
  • 160 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号