Go pprof使用之记录一次项目中的分析
对于一个后端开发者,性能分析及其重要,在Go语言中, 提供了一些优秀的诊断工具来帮助我们深入分析应用程序的执行情况,这些工具中最常用的当属pprof。 提出问题 Go性能分析要关注什么 pprof 如何使用 pprof分析报告解读 性能分析 CPU:程序对CPU的使用,时间都花在哪里; Heap:堆内存分配及占用内存的情况,是否存在内存泄漏; goroutine:goroutine 运行堆栈信息 Block:阻塞等待信息 Mutex: 锁竞争情况 pprof 使用 开启 pprof 方法一,引入 “net/http/pprof” import _ "net/http/pprof" 这样我们就可以通过访问http://localhost:6060/debug/pprof来访问pprof 方法2,自定义端口及地址 func RunProf(port int) { mux := http.NewServeMux() mux.HandleFunc("/debug/pprof/", pprof.Index) mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline) mux.HandleFunc("/debug/pprof/profile", pprof.Profile) mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol) mux.HandleFunc("/debug/pprof/trace", pprof.Trace) if port != 0 { ip := "localhost" go func() { addr := fmt.Sprintf("%v:%v", ip, port) tlog.Infof("pprof httpserver server listening on: %v", addr) err := http....