問題我有一個帶有測試套件的 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=
:
net:網絡阻止配置文件
sync:同步阻塞配置文件
系統調用:系統調用阻塞配置文件
sched:調度程序延遲配置文件
- 1 回答
- 0 關注
- 155 瀏覽
添加回答
舉報
0/150
提交
取消