golang開發(fā)安裝go-torch火焰圖操作步驟
安裝
1、 安裝go-torch
go get github.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)文章!
相關(guān)文章
Go語言創(chuàng)建、初始化數(shù)組的常見方式匯總
這篇文章主要介紹了Go語言創(chuàng)建、初始化數(shù)組的常見方式,實例匯總了Go語言操作數(shù)組的常見技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-02-02goland?-sync/atomic原子操作小結(jié)
這篇文章主要介紹了goland?-sync/atomic原子操作,原子操作能夠保證執(zhí)行期間是連續(xù)且不會被中斷(變量不會被其他修改,mutex可能存在被其他修改的情況),本文給大家介紹的非常詳細,需要的朋友參考下2022-08-08Go語言開發(fā)環(huán)境搭建與初探(Windows平臺下)
Go是Google開發(fā)的一種編譯型,並發(fā)型,并具有垃圾回收功能的編程語言,可能很多人想學習go語言,那么首先就要了解go語言的環(huán)境配置方法2014-10-10