首页后端开发其他后端知识go-torch如何进行安装和使用,什么情况用到

go-torch如何进行安装和使用,什么情况用到

时间2024-03-26 17:54:03发布访客分类其他后端知识浏览735
导读:这篇文章主要为大家详细介绍了go-torch如何进行安装和使用,什么情况用到,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望对大家学习或工作能有帮助。 安装 1、安装go-torc...
这篇文章主要为大家详细介绍了go-torch如何进行安装和使用,什么情况用到,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望对大家学习或工作能有帮助。


安装

1、安装go-torch

go getgithub.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()
}
     



以上就是关于go-torch如何进行安装和使用,什么情况用到的介绍啦,需要的朋友可以参考上述内容,希望对大家有帮助,欢迎关注网络,小编将为大家输出更多高质量的实用文章!

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: go-torch如何进行安装和使用,什么情况用到
本文地址: https://pptw.com/jishu/653662.html
PHP如何屏蔽错误?方法有哪些? PHP中怎样读取大文件?有什么方法?

游客 回复需填写必要信息