golang pprof简易玩法

golang的应用诊断可以依靠pprof工具, 这里总结一个快速上手的步骤. step0: 假设 我们现在的应用跑在远端 我们本地的go为1.11版本(webui支持了火焰图) 诊断的应用是caddy step1: 开启 对我们的应用开启profiling. 这个方法有很多, 可以直接参考runtime/pprof和net/http/pprof. 因为我们要调试的是caddy, 直接通过改配置文件并重新加载就可以开启了: ## 正常服务 http://test.com:8888 { root /static log stdout } ## 只允许本地去访问该服务, 安全起见 http://xx.yy:4869 { bind 127.0.0.1 pprof } step2: 触发 在服务器上请求对应的url, 获取对应prof文件: ## 获取应用当前的内存情况 curl '127.0.0.1:4869/debug/pprof/heap' -H 'Host: xx.yy' -o mem.prof ## 采集应用60s内的cpu使用情况 curl '127.0.0.1:4869/debug/pprof/profile?seconds=60' -H 'Host: xx.yy' -o cpu.prof ## 采集应用10s内的goroutine调度与执行情况 curl '127.0.0.1:4869/debug/pprof/trace?seconds=10' -H 'Host: xx.yy' -o r.trace 因为配置文件指定了仅允许本地访问, 因此我们需要将对应的文件传到本地. step3: 分析 这块分析可以用命令行, 当然更方便的是使用浏览器. go tool pprof -http :6666 ....

January 28, 2019 · akawhy