脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Golang - golang开发安装go-torch火焰图操作步骤

golang开发安装go-torch火焰图操作步骤

2021-12-03 13:16秋天的春 Golang

这篇文章主要为大家介绍了golang开发安装go-torch火焰图操作步骤

安装

    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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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,去运行,会把缓存的数据输出到文件中

?
1
2
./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

golang开发安装go-torch火焰图操作步骤

另一种自定义显示方式

代码修改

?
1
2
3
4
5
6
7
8
9
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))
    }()
}

使用

?
1
2
3
4
5
6
7
8
# 用 -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格式的输出,优化显示效果

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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的资料请关注服务器之家其它相关文章!

原文链接:https://blog.csdn.net/ffzhihua/article/details/82862933

延伸 · 阅读

精彩推荐
  • Golanggolang如何使用struct的tag属性的详细介绍

    golang如何使用struct的tag属性的详细介绍

    这篇文章主要介绍了golang如何使用struct的tag属性的详细介绍,从例子说起,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看...

    Go语言中文网11352020-05-21
  • Golanggo日志系统logrus显示文件和行号的操作

    go日志系统logrus显示文件和行号的操作

    这篇文章主要介绍了go日志系统logrus显示文件和行号的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    SmallQinYan12302021-02-02
  • GolangGolang中Bit数组的实现方式

    Golang中Bit数组的实现方式

    这篇文章主要介绍了Golang中Bit数组的实现方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    天易独尊11682021-06-09
  • Golanggolang 通过ssh代理连接mysql的操作

    golang 通过ssh代理连接mysql的操作

    这篇文章主要介绍了golang 通过ssh代理连接mysql的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    a165861639710342021-03-08
  • Golanggolang json.Marshal 特殊html字符被转义的解决方法

    golang json.Marshal 特殊html字符被转义的解决方法

    今天小编就为大家分享一篇golang json.Marshal 特殊html字符被转义的解决方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧 ...

    李浩的life12792020-05-27
  • Golanggolang的httpserver优雅重启方法详解

    golang的httpserver优雅重启方法详解

    这篇文章主要给大家介绍了关于golang的httpserver优雅重启的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,...

    helight2992020-05-14
  • Golanggo语言制作端口扫描器

    go语言制作端口扫描器

    本文给大家分享的是使用go语言编写的TCP端口扫描器,可以选择IP范围,扫描的端口,以及多线程,有需要的小伙伴可以参考下。 ...

    脚本之家3642020-04-25
  • GolangGolang通脉之数据类型详情

    Golang通脉之数据类型详情

    这篇文章主要介绍了Golang通脉之数据类型,在编程语言中标识符就是定义的具有某种意义的词,比如变量名、常量名、函数名等等,Go语言中标识符允许由...

    4272021-11-24