分享
Golang FlameGraph(火焰图)
城寒 · · 6741 次点击 · · 开始浏览这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。
简介
安装
go get github.com/uber/go-torch
# 再安装 brendangregg/FlameGraph
export PATH=$PATH:/absolute/path/FlameGraph-master
# 还需要安装一个graphviz用来画内存图
yum install graphviz
代码修改
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()
}
火焰图的效果网上很多,下一篇介绍基本的调优过程
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信6741 次点击
上一篇:[转载]Go垃圾回收机制剖析
下一篇:Go语言性能测试
添加一条新回复
(您需要 后才能回复 没有账号 ?)
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
简介
安装
go get github.com/uber/go-torch
# 再安装 brendangregg/FlameGraph
export PATH=$PATH:/absolute/path/FlameGraph-master
# 还需要安装一个graphviz用来画内存图
yum install graphviz
代码修改
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()
}
火焰图的效果网上很多,下一篇介绍基本的调优过程