go pprof 第一篇 環境配置
阿新 • • 發佈:2020-11-13
pprof 工具功能就不多說了
mac系統解說 :
看了網上方法進行一次小白步驟解說,網上很多講的太簡略了
1首先在你的main專案里加程式碼,這兩個庫為必須匯入的
import(
"net/http"
_ "net/http/pprof"
)
func main() {
//log 初始化
go func() {
http.ListenAndServe("0.0.0.0:6060", nil)
}()
//你的專案程式碼
}
2 控制檯輸入
go tool pprof http://localhost:6060/debug/pprof/profile
如果出現 go tool: no such tool "pprof"go env 檢視自己的 GOTOOL 路徑 一般問題出在該路徑下沒有pprof 當然執行不了
用find / -name "darwin_amd64" 查詢下自己電腦上的真實路徑
直接改gotooldir路徑是不行了,我估計go內部配置依賴了goroot, 所以直接開啟
vim ~/.bashrc
然後在裡面加一行 如果已經有了goroot 直接替換那個goroot
export GOROOT=/usr/local/Cellar/go/1.15/libexec
然後記得 soure~/.bashrc
再go env 看下gotool路徑
以上就是環境配置了
接下來go tool pprof http://localhost:6060/debug/pprof/profile
這個指令會等個30秒左右才出結果
top10是看前10個最耗cpu
web指令會生成一個svg檔案,儲存那個檔案,然後開啟方式選擇瀏覽器 就可以看到效果圖了
下期再說每個引數意義,基本到這兒算領大家進入pprof大門了