go 调试, pprof, go tool trace
go 调试, pprof, go tool trace 做 Profiling 第一步就是怎么获取应用程序的运行情况数据。go 语言提供了 runtime/pprof 和 net/http/pprof 两个库 http api // pprof 的init函数会将pprof里的一些handler注册到http.DefaultServeMux上 // 当不使用http.DefaultServeMux来提供http api时,可以查阅其init函数,自己注册handler import _ "net/http/pprof" go func() { http.ListenAndServe("0.0.0.0:8080", nil) }() http://localhost:8080/debug/pprof/ cpu go tool pprof http://localhost:8080/debug/pprof/profile?seconds=60 mem go tool pprof http://localhost:6060/debug/pprof/heap block go tool pprof http://localhost:8080/debug/pprof/block mutex go tool pprof http://localhost:6060/debug/pprof/mutex runtime/pprof // CPUProfile enables cpu profiling. Note: Default is CPU defer profile.Start(profile.CPUProfile).Stop() // GoroutineProfile enables goroutine profiling. // It returns all Goroutines alive when defer occurs. defer profile.Start(profile.GoroutineProfile).Stop() // BlockProfile enables block (contention) profiling. defer profile.Start(profile.BlockProfile).Stop() // ThreadcreationProfile enables thread creation profiling. defer profile.Start(profile.ThreadcreationProfile).Stop() // MemProfile changes which type of memory profiling to // profile the heap. defer profile.Start(profile.MemProfile).Stop() // MutexProfile enables mutex profiling. defer profile.Start(profile.MutexProfile).Stop() golang CPU profiling CPU 性能分析(CPU profiling) 是最常见的性能分析类型。 ...