经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Go语言 » 查看文章
golang开发安装go-torch火焰图操作步骤
来源:jb51  时间:2021/11/16 15:13:52  对本文有异议

安装

    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函数的文件中添加相应代码

使用

  1. package main
  2. import (
  3. "net/http"
  4. "net/http/pprof"
  5. )
  6. func main() {
  7. // 主函数中添加
  8. go func(){
  9. http.HandleFunc("/debug/pprof/block", pprof.Index)
  10. http.HandleFunc("/debug/pprof/goroutine", pprof.Index)
  11. http.HandleFunc("/debug/pprof/heap", pprof.Index)
  12. http.ListenAndServe("0.0.0.0:8888", nil) //注意此处,遇到错误
  13. }()
  14. //你的代码
  15. }

然后压测的时候,在go 的bin目录下找到go-torch,去运行,会把缓存的数据输出到文件中

  1. ./go-torch -u http://localhost:8080/debug/pprof/ -p > profile-local.svg
  2. ./go-torch -u http://localhost:8080/debug/pprof/heap -p > heap-local.svg

另一种自定义显示方式

代码修改

  1. import "net/http"
  2. import _ "net/http/pprof"
  3. func main() {
  4. // 主函数中添加
  5. go func() {
  6. http.HandleFunc("/program/html", htmlHandler) // 用来查看自定义的内容
  7. log.Println(http.ListenAndServe("0.0.0.0:8080", nil))
  8. }()
  9. }

使用

  1. # 用 -u 分析CPU使用情况
  2. ./go-torch -u http://127.0.0.1:8080
  3. # 用 -alloc_space 来分析内存的临时分配情况
  4. ./go-torch -alloc_space http://127.0.0.1:8080/debug/pprof/heap --colors=mem
  5. # 用 -inuse_space 来分析程序常驻内存的占用情况;
  6. ./go-torch -inuse_space http://127.0.0.1:8080/debug/pprof/heap --colors=mem
  7. # 画出内存分配图
  8. 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格式的输出,优化显示效果

  1. func writeBuf(buffer *bytes.Buffer, format string, a ...interface{}) {
  2. (*buffer).WriteString(fmt.Sprintf(format, a...))
  3. }
  4. func htmlHandler(w http.ResponseWriter, req *http.Request) {
  5. io.WriteString(w, statusHtml())
  6. }
  7. // 访问 localhost:8080/program/html 可以看到一个表格,一秒钟刷新一次
  8. func statusHtml() string {
  9. var buf bytes.Buffer
  10. buf.WriteString("<html><meta http-equiv=\"refresh\" content=\"1\">" +
  11. "<body><h2>netflow-decoder status count</h2>" +
  12. "<table width=\"500px\" border=\"1\" cellpadding=\"5\" cellspacing=\"1\">" +
  13. "<tr><th>NAME</th><th>TOTAL</th><th>SPEED</th></tr>")
  14. writeBuf(&buf, "<tr><td>UDP</td><td>%d</td><td>%d</td></tr>",
  15. lastRecord.RecvUDP, currSpeed.RecvUDP)
  16. writeBuf(&buf, "</table><p>Count time: %s</p><p>Time now: %s</p>",
  17. countTime.Format("2006-01-02 15:04:05"), time.Now().Format("2006-01-02 15:04:05"))
  18. buf.WriteString("</body></html>")
  19. return buf.String()
  20. }

以上就是golang开发安装go-torch火焰图操作步骤的详细内容,火焰图的效果网上很多,更多关于golang的资料请关注w3xue其它相关文章!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号