1. graphviz 2.38

官网下载[https://graphviz.org/download/](https://graphviz.org/download/)
安装完成后,环境变量中配置安装目录,命令行输出dot -v,输出如下,则表明安装成功:
image.png

2. pprof配合graphviz使用

web服务中使用原生路由,引入_ "net/http/pprof"

  1. package main
  2. import (
  3. "net/http"
  4. _ "net/http/pprof"
  5. )
  6. func main() {
  7. http.ListenAndServe(":8080", nil)
  8. }

web服务中使用gin,引入"github.com/gin-contrib/pprof"

  1. package main
  2. import (
  3. "net/http"
  4. "github.com/gin-contrib/pprof"
  5. "github.com/gin-gonic/gin"
  6. )
  7. func main() {
  8. g := gin.Default()
  9. pprof.Register(g)
  10. g.Run(":8080")
  11. }

运行程序,命令行输入命令进行采集

  1. # 采集30s,执行完稍稍等待,会在浏览器打开web界面,点击View-> Flame Graph 查看火焰图
  2. go tool pprof -http=:1234 http://localhost:8888/debug/pprof/profile
  3. ###########################
  4. # 或者采样后直接输出文件,默认位置在$HOME/pprof/pprof.samples.cpu.xxx.gb.gz
  5. go tool pprof http://localhost:8888/debug/pprof/profile
  6. # 再使用命令打开
  7. go tool pprof -http=:1234 [输出的采样文件]

3. go-torch

3.1 安装

Uber开源的一款更方便生成火焰图的工具,参考文章

  1. # 下载工具
  2. go get github.com/uber-archive/go-torch
  3. # 进入go-torch目录,下载FlameGraph
  4. cd $GOPATH/src/github.com/uber/go-torch
  5. git clone https://github.com/brendangregg/FlameGraph.git

将FlameGraph目录配置到环境变量,windows下可能会因为无法编译.pl文件而无法生成火焰图,解决办法

3.2 使用

运行程序,执行一下命令,会在程序目录下生成svg文件,双击打开即可
image.png
image.png