Golang FlameGraph(火焰圖)

Yew1168發表於2019-04-30

1.安裝元件

  1. 安裝go-torch
    go get github.com/uber/go-torch
  2. 安裝 FlameGraph
    cd $WORK_PATH && git clone https://github.com/brendangregg/FlameGraph.git
    export PATH=$PATH:$WORK_PATH/FlameGraph-master
  3. 安裝graphviz
    yum install graphviz(CentOS, Redhat)

2.程式碼修改

package main

import (
    "net/http"
    "net/http/pprof"
)

func main() {
    // 主函式中新增
    go func() {
        http.HandleFunc("/debug/pprof/block", pprof.Index)
        http.HandleFunc("/debug/pprof/goroutine", pprof.Index)
        http.HandleFunc("/debug/pprof/heap", pprof.Index)
        http.HandleFunc("/debug/pprof/threadcreate", pprof.Index)

        http.ListenAndServe("0.0.0.0:8888", nil)
    }()

    var finishWaiter chan int
    <-finishWaiter
}

3.檢視結果

執行上述程式後,使用如下命令生成CPU火焰圖:
go-torch -u http://localhost:8888/debug/pprof/ -p > profile-local.svg
效果圖如下:

profile-local.png

 

生成記憶體火焰圖:
go-torch -u http://localhost:8888/debug/pprof/heap -p > heap-local.svg
效果圖如下:

heap-local.png



作者:zr_hebo
連結:https://www.jianshu.com/p/1e784c387f45

相關文章