商丘哪里做网站,手机大全及价格,北京网站建设文章,网站建设大约需要多少钱pprof是Go语言内置的性能分析工具#xff0c;它可以帮助我们分析程序的CPU使用情况、内存分配等。
pprof 包含两部分#xff1a; Go语言内置的两个包 net/http/pprof 对 runtime/pprof 的二次封装#xff0c;一般是服务型应用。比如 web server #xff0c;它一直运行。这…pprof是Go语言内置的性能分析工具它可以帮助我们分析程序的CPU使用情况、内存分配等。
pprof 包含两部分 Go语言内置的两个包 net/http/pprof 对 runtime/pprof 的二次封装一般是服务型应用。比如 web server 它一直运行。这个包对提供的 http 服务进行数据采集分析。 runtime/pprof 采集程序运行数据进行性能分析一般用于后台工具型应用这种应用运行一段时间就结束。 性能剖析工具 go tool pprof
性能分析指标
CPU性能分析内存(Memory)性能分析阻塞(block)性能分析锁(mutex)性能分析
内置包应用
runtime/pprof
cpu分析
关键
// 开启 cpu 采集分析
pprof.StartCPUProfile(w io.Writer)// 停止 cpu 采集分析
pprof.StopCPUProfile()demo
package mainimport (logosruntime/pprof
)func main() {f, err : os.OpenFile(cpu.pprof, os.O_CREATE|os.O_RDWR, 0644)if err ! nil {log.Fatalf(Failed to open profile file: %v, err)return}if err pprof.StartCPUProfile(f); err ! nil {log.Fatalf(Failed to start CPU profiling: %v, err)return}defer pprof.StopCPUProfile()// do something
}内存分析
关键
pprof.WriteHeapProfile(w io.Writer)demo
func captureHeapProfile(filename string) error {// 打开文件用于存储堆内存信息file, err : os.Create(filename)if err ! nil {return err}defer file.Close()// 写入堆内存信息if err pprof.WriteHeapProfile(file); err ! nil {return err}return nil
}net/http/pprof
对于服务类型的应用主要在服务内部匿名引入net/http/pprof包然后通过HTTP访问pprof页面。 匿名引入方式为import _ net/http/pprof
package mainimport (fmtnet/http_ net/http/pprof
)func main() {http.HandleFunc(/, hello)err : http.ListenAndServe(:8080, nil)if err ! nil {fmt.Println(ListenAndServe Err:, err.Error())return}
}func hello(resp http.ResponseWriter, req *http.Request) {fmt.Fprintln(resp, Hello World, Are You OK?)
}浏览器打开 http://localhost:8080/debug/pprof/
如果HTTP服务不是通过http.ListenAndServe(:8080, nil)启动的需要自己注册pprof路由
net/http/pprof包中有init函数
func init() {http.HandleFunc(/debug/pprof/, Index)http.HandleFunc(/debug/pprof/cmdline, Cmdline)http.HandleFunc(/debug/pprof/profile, Profile)http.HandleFunc(/debug/pprof/symbol, Symbol)http.HandleFunc(/debug/pprof/trace, Trace)
}如果使用自定义的ServeMux则需要增加注册后才能获取到pprof
// 自己注册这几个函数
r.HandleFunc(/debug/pprof/, pprof.Index)
r.HandleFunc(/debug/pprof/cmdline, pprof.Cmdline)
r.HandleFunc(/debug/pprof/profile, pprof.Profile)
r.HandleFunc(/debug/pprof/symbol, pprof.Symbol)
r.HandleFunc(/debug/pprof/trace, pprof.Trace)go tool pprof
命令格式
pprof format [options] [binary] source
pprof [options] [binary] source
pprof -http [host]:[port] [options] [binary] source想在浏览器查看需要安装graphviz
web页面模式
go tool pprof -http:6666 [file_pprof|url_pprof]
go tool pprof -http:9999 cpu.pprof
go tool pprof -http:9999 mem.pprof命令行模式
go tool pprof [file_pprof|url_pprof]
常用
go tool pprof -http:9999 cpu.pprof
go tool pprof -http:9999 /tmp/profile215959616/mem.pprof
go test -benchFib$ -cpuprofilecpu.pprof .
go tool pprof -text cpu.pprofhttps://blog.csdn.net/sinat_24985411/article/details/128816228
https://www.cnblogs.com/jiujuan/p/14588185.html
https://github.com/google/pprof/blob/main/doc/README.md
https://pkg.go.dev/runtime/pprof#hdr-Profiling_a_Go_program
https://www.cnblogs.com/jiujuan/p/14588185.html