Profiler labels in Go
在Go 1.9中,加入了Profiler labels部分。
CPU profilers collect and output hot spots where the CPU spent most time in when executing
A typical CPU profiler output is primarily reports the location of these spots as function name, source file/line, etc
在以往的Go的调试过程中,性能问题的debug往往是最困难的。
With 1.9, Go is introducing a new feature that allows you to record additional information to provide more context about the execution path.
The runtime/pprof
package will export several new APIs to let users add labels
关于打label部分:
1 2 3 4 5 6 labels := pprof.Labels("worker" , "purge" ) pprof.Do(ctx, labels, func (ctx context.Context) { go update(ctx) })
采用net/http/pprof
来分析相关的性能,
1 2 3 4 5 6 7 8 9 package mainimport _ "net/http/pprof" func main () { log.Fatal(http.ListenAndServe("localhost:5555" , nil )) }
通过
首先通过tags
区分出相关的path部分
1 2 3 4 5 6 7 (pprof) tags http-path: Total 80 70 (87.50 %): /messages 10 (12.50 %): /user worker: Total 158 158 ( 100 %): purge
通过相关的filter部分,我们可以仅关注到/user
部分
1 2 3 4 5 6 7 8 9 10 11 12 13 14 (pprof) tagfocus="http-path:/user" (pprof) top10 -cum Showing nodes accounting for 0.10 s, 3.05 % of 3.28 s total flat flat% sum% cum cum% 0 0 % 0 % 0.10 s 3.05 % main.generateID.func1 /Users/jbd/src/hello/main.go 0.01 s 0.3 % 0.3 % 0.08 s 2.44 % runtime.concatstring2 /Users/jbd/go /src/runtime/string .go 0.06 s 1.83 % 2.13 % 0.07 s 2.13 % runtime.concatstrings /Users/jbd/go /src/runtime/string .go 0.01 s 0.3 % 2.44 % 0.02 s 0.61 % runtime.mallocgc /Users/jbd/go /src/runtime/malloc.go 0 0 % 2.44 % 0.02 s 0.61 % runtime.slicebytetostring /Users/jbd/go /src/runtime/string .go 0 0 % 2.44 % 0.02 s 0.61 % strconv.FormatInt /Users/jbd/go /src/strconv/itoa.go 0 0 % 2.44 % 0.02 s 0.61 % strconv.Itoa /Users/jbd/go /src/strconv/itoa.go 0 0 % 2.44 % 0.02 s 0.61 % strconv.formatBits /Users/jbd/go /src/strconv/itoa.go 0.01 s 0.3 % 2.74 % 0.01 s 0.3 % runtime.memmove /Users/jbd/go /src/runtime/memmove_amd64.s 0.01 s 0.3 % 3.05 % 0.01 s 0.3 % runtime.nextFreeFast /Users/jbd/go /src/runtime/malloc.go
当然还有其他的命令
You can also use tagshow
, taghide
, and tagignore
commands as other filtering options. For example, tagignore allows you to match anything but the given regex.
同时,注意pprofutil
这个包