web服务器
import _"net/http/pprof" go func() { log.Println(http.ListenAndServe("localhost:6666", nil)) }()
应用程序
import "runtime/pprof" func main() { ... f, err := os.Create("cpu.prof") if err != nil { log.Fatal(err) } defer f.close() pprof.StartCPUProfile(f) defer pprof.StopCPUProfile()
go tool pprof XXX cpu.prof
为golang程序使用pprof远程查看httpserver运行堆栈,cpu耗时等信息
go tool pprof http://localhost:6666/debug/pprof/profile
fm, err := os.Create("mem.prof")
if err != nil {
log.Fatal(err)
}
pprof.WriteHeapProfile(fm)
fm.Close()
func saveHeapProfile() {
runtime.GC()
f, err := os.Create(fmt.Sprintf("prof/heap_%s_%d_%s.prof", progname, pid, time.Now().Format("2006_01_02_03_04_05")))
if err != nil {
return
}
defer f.Close()
pprof.Lookup("heap").WriteTo(f, 1)
}
保存堆信息之前先GC了一下,以进行垃圾回收,之后保存下来的堆信息将更精确地告诉我们哪些地方可能会造成内存泄露,无法被垃圾回收的。
提及 pkg/profile 包
Debugging performance issues in Go programs
Golang Slices And The Case Of The Missing Memory
golang profiling — Yandong Yao
graphviz
官网:
http://www.graphviz.org/Download..php
SuSE:
http://download.opensuse.org/repositories/graphics/SLE_12/x86_64/graphviz-2.38.0-137.1.x86_64.rpm