golang開發(fā)安裝go-torch火焰圖操作步驟
安裝
1、安裝go-torch
go getgithub.com/uber/go-torch
2、安裝FlameGraph
cd $GOPATH && git clone https://github.com/brendangregg/FlameGraph.git
export PATH=$PATH:$GOPATH/FlameGraph
【這步一定要設(shè)置,生成火焰圖時會用到】
3、安裝graphviz(CentOS, Redhat)
yum install graphviz
在程序的包含mian函數(shù)的文件中添加相應(yīng)代碼
使用
package main import ( "net/http" "net/http/pprof" ) func main() { // 主函數(shù)中添加 go func(){ http.HandleFunc("/debug/pprof/block", pprof.Index) http.HandleFunc("/debug/pprof/goroutine", pprof.Index) http.HandleFunc("/debug/pprof/heap", pprof.Index) http.ListenAndServe("0.0.0.0:8888", nil) //注意此處,遇到錯誤 }() //你的代碼 }
然后壓測的時候,在go 的bin目錄下找到go-torch,去運行,會把緩存的數(shù)據(jù)輸出到文件中
./go-torch -u http://localhost:8080/debug/pprof/ -p > profile-local.svg ./go-torch -u http://localhost:8080/debug/pprof/heap -p > heap-local.svg
另一種自定義顯示方式
代碼修改
import "net/http" import _ "net/http/pprof" func main() { // 主函數(shù)中添加 go func() { http.HandleFunc("/program/html", htmlHandler) // 用來查看自定義的內(nèi)容 log.Println(http.ListenAndServe("0.0.0.0:8080", nil)) }() }
使用
# 用 -u 分析CPU使用情況 ./go-torch -u http://127.0.0.1:8080 # 用 -alloc_space 來分析內(nèi)存的臨時分配情況 ./go-torch -alloc_space http://127.0.0.1:8080/debug/pprof/heap --colors=mem # 用 -inuse_space 來分析程序常駐內(nèi)存的占用情況; ./go-torch -inuse_space http://127.0.0.1:8080/debug/pprof/heap --colors=mem # 畫出內(nèi)存分配圖 go tool pprof -alloc_space -cum -svg http://127.0.0.1:8080/debug/pprof/heap > heap.svg
查看
使用瀏覽器查看svg文件,程序運行中,可以登錄http://127.0.0.1:10086/debug/pprof/查看程序?qū)崟r狀態(tài) 在此基礎(chǔ)上,可以通過配置handle
來實現(xiàn)自定義的內(nèi)容查看,可以添加Html格式的輸出,優(yōu)化顯示效果
func writeBuf(buffer *bytes.Buffer, format string, a ...interface{}) { (*buffer).WriteString(fmt.Sprintf(format, a...)) } func htmlHandler(w http.ResponseWriter, req *http.Request) { io.WriteString(w, statusHtml()) } // 訪問 localhost:8080/program/html 可以看到一個表格,一秒鐘刷新一次 func statusHtml() string { var buf bytes.Buffer buf.WriteString("<html><meta http-equiv=\"refresh\" content=\"1\">" + "<body><h2>netflow-decoder status count</h2>" + "<table width=\"500px\" border=\"1\" cellpadding=\"5\" cellspacing=\"1\">" + "<tr><th>NAME</th><th>TOTAL</th><th>SPEED</th></tr>") writeBuf(&buf, "<tr><td>UDP</td><td>%d</td><td>%d</td></tr>", lastRecord.RecvUDP, currSpeed.RecvUDP) writeBuf(&buf, "</table><p>Count time: %s</p><p>Time now: %s</p>", countTime.Format("2006-01-02 15:04:05"), time.Now().Format("2006-01-02 15:04:05")) buf.WriteString("</body></html>") return buf.String() }
以上就是golang開發(fā)安裝go-torch火焰圖操作步驟的詳細內(nèi)容,火焰圖的效果網(wǎng)上很多,更多關(guān)于golang的資料請關(guān)注本站其它相關(guān)文章!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。