溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

golang開發(fā)如何安裝go-torch火焰圖

發(fā)布時(shí)間:2021-11-16 13:28:45 來源:億速云 閱讀:254 作者:小新 欄目:開發(fā)技術(shù)

小編給大家分享一下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è)置,生成火焰圖時(shí)會(huì)用到】

    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) //注意此處,遇到錯(cuò)誤 
}() 
//你的代碼 
}

然后壓測(cè)的時(shí)候,在go 的bin目錄下找到go-torch,去運(yùn)行,會(huì)把緩存的數(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

golang開發(fā)如何安裝go-torch火焰圖

另一種自定義顯示方式

代碼修改

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)存的臨時(shí)分配情況
./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文件,程序運(yùn)行中,可以登錄 http://127.0.0.1:10086/debug/pprof/ 查看程序?qū)崟r(shí)狀態(tài) 在此基礎(chǔ)上,可以通過配置handle來實(shí)現(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 可以看到一個(gè)表格,一秒鐘刷新一次
func statusHtml() string {
	var buf bytes.Buffer
	buf.WriteString("<html><meta http-equiv=\"refresh\" content=\"1\">" +
		"<body><h3>netflow-decoder status count</h3>" +
		"<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()
}

看完了這篇文章,相信你對(duì)“golang開發(fā)如何安裝go-torch火焰圖”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)
推薦閱讀:
  1. golang安裝
  2. mac 安裝golang

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI