golang开发安装go-torch火焰图操作步骤
程序员文章站
2022-06-17 10:26:20
目录安装使用另一种自定义显示方式代码修改使用查看安装 1、安装go-torchgo getgithub.com/uber/go-torch 2、安装flamegraph cd $gopath &a...
安装
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
【这步一定要设置,生成火焰图时会用到】
3、安装graphviz (centos, redhat)
yum install graphviz
在程序的包含mian函数的文件中添加相应代码
使用
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.listenandserve("0.0.0.0:8888", nil) //注意此处,遇到错误 }() //你的代码 }
然后压测的时候,在go 的bin目录下找到go-torch,去运行,会把缓存的数据输出到文件中
./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() { // 主函数中添加 go func() { http.handlefunc("/program/html", htmlhandler) // 用来查看自定义的内容 log.println(http.listenandserve("0.0.0.0:8080", nil)) }() }
使用
# 用 -u 分析cpu使用情况 ./go-torch -u http://127.0.0.1:8080 # 用 -alloc_space 来分析内存的临时分配情况 ./go-torch -alloc_space http://127.0.0.1:8080/debug/pprof/heap --colors=mem # 用 -inuse_space 来分析程序常驻内存的占用情况; ./go-torch -inuse_space http://127.0.0.1:8080/debug/pprof/heap --colors=mem # 画出内存分配图 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/ 查看程序实时状态 在此基础上,可以通过配置handle
来实现自定义的内容查看,可以添加html格式的输出,优化显示效果
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开发安装go-torch火焰图操作步骤的详细内容,火焰图的效果网上很多,更多关于golang的资料请关注其它相关文章!