例子 gist 来自之前的 Trie 的实现, Ruby vs Go.

main
os.Create("cpu_profile")pprof.StartCPUProfiledefer pprof.StopCPUProfile()

When CPU profiling is enabled, the Go program stops about 100 times per second and records a sample consisting of the program counters on the currently executing goroutine’s stack.

CPU Profiling
help
toptop15
main.NewNode35.09% + 12.28% = 47.37%
main.NewNode
list
Memory Profiling
pprof.WriteHeapProfile(memProfile)

然后是跟上面一样的生成图片:

Type: inuse_space 是监控内存的默认选项, 还可以选 -alloc_space, -inuse_objects, -alloc_objects

_objects
net/http/pprof
net/http/pprof
_ "net/http/pprof"/debug/pprof/

用 wrk (brew install wrk) 模拟测试

wrk -c 200 -t 4 -d 3m http://localhost:8080/hello
go tool pprof