pprof采集不到数据?先确认三件事:服务是否有真实请求、pprof是否在独立goroutine运行且主goroutine未退出、cpu profile采样是否满30秒,否则改用heap或goroutine接口即时查看。

pprof 采集不到数据?先确认服务真正在跑请求
压测流量没打到目标服务,pprof 就是空的——这不是配置问题,是链路断了。常见原因有三:access log 没日志、netstat -an | grep :6060 看不到连接、http.ListenAndServe("0.0.0.0:6060", nil) 没放开监听地址(默认只绑 localhost)。Go 1.20+ 还得确认是否显式导入了 import _ "net/http/pprof",否则 /debug/pprof/goroutine?debug=2 这类 endpoint 会被禁用。
CPU profile 火焰图一片平?换 heap 或 goroutine 接口看即时状态
运行 go tool pprof http://localhost:6060/debug/pprof/profile?seconds=30 却只看到 runtime.mcall 或 runtime.park,说明不是 CPU 密集型瓶颈,而是调度或阻塞问题。这时应立刻切到:/debug/pprof/goroutine?debug=2 查堆积数量与状态;/debug/pprof/heap?alloc_space 看累计分配字节数,定位高频 json.Marshal 或 bytes.Buffer 创建点;/debug/pprof/block 查 runtime.chansend 是否超 30%,暴露 channel 同步瓶颈。
HTTP 客户端没设超时?延迟会直接传染整条链路
Go 的 http.Client 默认零超时,一次下游卡住 5 秒,本服务所有 goroutine 都可能挂起。必须显式配置 Transport:DialContext.Timeout(连接)、TLSHandshakeTimeout(握手)、ResponseHeaderTimeout(响应头),再配 Timeout 控总耗时。高并发下不设 MaxIdleConns 和 MaxIdleConnsPerHost,连接复用就失效,goroutine 会在 net/http.(*persistConn).roundTrip 上排队等空闲连接。
goroutine 数量飙到 10k+?大概率是泄漏,不是并发需求
/debug/pprof/goroutine?debug=2 显示活跃协程持续增长,90% 是泄漏:http.HandleFunc 里裸写 go func() { ... }() 却没配 context.WithTimeout;日志模块无节制往 channel 写;或者数据库 QueryRow 卡在 database/sql.(*DB).QueryRow,实为连接池耗尽或慢查询未加索引。验证方式很简单:重启服务后压测 5 分钟,runtime.NumGoroutine() 应稳定在初始值 ±20% 范围内。
runtime.gopark 调用栈比盯着业务函数更有效。golang免费学习笔记(深入):立即使用
在学习笔记中,你将探索golang的核心概念和高级技巧!











