This is a creation in Article, where the information may have evolved or changed.
Brief introduction
Installation
go get github.com/uber/go-torch# 再安装 brendangregg/FlameGraph export PATH=$PATH:/absolute/path/FlameGraph-master# 还需要安装一个graphviz用来画内存图yum install graphviz
Code modification
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))}()}
Use
# 用 -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
View
Use the browser to view the SVG file, the program runs, you can log in to http://127.0.0.1:10086/debug/pprof/
view the real-time status of the program on this basis, you can configure handle
to achieve a customized content view, you can add HTML format output, optimize the display effect
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 ())}//access localhost:8080/program/html can see a table that refreshes the Func statushtml () string {var buf bytes once a second. Bufferbuf.writestring ("
The effect of the flame map is many online, the next article describes the basic tuning process