package main import ( "net/http" "runtime/pprof" ) var quit chan struct{} = make(chan struct{}) func f() { <-quit } func handler(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/plain") p := pprof.Lookup("goroutine") p.WriteTo(w, 1) } func main() { for i := 0; i < 10000; i++ { go f() } http.HandleFunc("/", handler) http.ListenAndServe(":11181", nil) }
这上面的例子中,我们启动了10000个goroutine,并阻塞,然后通过访问http://localhost:11181/,我们就可以得到整个goroutine的信息,仅列出关键信息:
goroutine profile: total 10004 10000 @ 0x186f6 0x616b 0x6298 0x2033 0x188c0 #0x2033 main.f+0x33 /Users/siddontang/test/pprof.go:11
可以看到,在main.f这个函数中,有10000个goroutine正在执行
转自:http://www.tuicool.com/articles/VBFNNfA
时间: 2024-10-07 22:30:39