var cpuprofile = flag.String("cpuprofile", "", "write cpu profile `file`") var memprofile = flag.String("memprofile", "", "write memory profile to `file`")
const ( col = 10000 row = 10000 )
funcmain() { flag.Parse() if *cpuprofile != "" { f, err := os.Create(*cpuprofile) if err != nil { log.Fatal("could not create CPU profile: ", err) } if err := pprof.StartCPUProfile(f); err != nil { //监控cpu log.Fatal("could not start CPU profile: ", err) } defer pprof.StopCPUProfile() }
// 主逻辑区,进行一些简单的代码运算 x := [row][col]int{} s := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < row; i++{ for j := 0; j < col; j++ { x[i][j] = s.Intn(100000) } }
for i := 0; i < row; i++{ tmp := 0 for j := 0; j < col; j++ { tmp += x[i][j] } }
if *memprofile != "" { f, err := os.Create(*memprofile) if err != nil { log.Fatal("could not create memory profile: ", err) } runtime.GC() // GC,获取最新的数据信息 if err := pprof.WriteHeapProfile(f); err != nil { // 写入内存信息 log.Fatal("could not write memory profile: ", err) } f.Close() } }
编译运行,会根据入参生成两个对应的数据文件:
1 2 3 4 5 6 7 8
> go build > ./pprof -cpuprofile cpu.prof -memprofile mem.prof > ll total 2656 -rw-r--r--. 1 ma root 1183 Jan 9 21:39 cpu.prof # cpu运行数据 -rw-r--r--. 1 ma root 280 Jan 9 21:39 mem.prof # 内存数据 -rwxr-xr-x. 1 ma root 2707088 Jan 9 21:39 pprof # 编译后的可执行文件 -rw-r--r--. 1 ma root 1296 Jan 9 21:38 pprof.go
> go tool pprof cpu.prof Entering interactive mode (type "help" for commands) (pprof) help
Commands: cmd [n] [--cum] [focus_regex]* [-ignore_regex]* Produce a text report with the top n entries. Include samples matching focus_regex, and exclude ignore_regex. Add --cum to sort using cumulative data. Available commands: callgrind Outputs a graph in callgrind format disasm Output annotated assembly for functions matching regexp or address dot Outputs a graph in DOT format eog Visualize graph through eog evince Visualize graph through evince gif Outputs a graph image in GIF format gv Visualize graph through gv list Output annotated source for functions matching regexp pdf Outputs a graph in PDF format peek Output callers/callees of functions matching regexp png Outputs a graph image in PNG format proto Outputs the profile in compressed protobuf format ps Outputs a graph in PS format raw Outputs a text representation of the raw profile svg Outputs a graph in SVG format tags Outputs all tags in the profile text Outputs top entries in text form top Outputs top entries in text form tree Outputs a text rendering of call graph web Visualize graph through web browser weblist Output annotated source in HTML for functions matching regexp or address peek func_regex Display callers and callees of functions matching func_regex. ...
1.top
命令格式:top [n],查看排名前n个数据,默认为10。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
(pprof) top 8490ms of 8510ms total (99.76%) Dropped 13 nodes (cum <= 42.55ms) Showing top 10 nodes out of 15 (cum >= 110ms) flat flat% sum% cum cum% 6780ms 79.67% 79.67% 8510ms 100% main.main 670ms 7.87% 87.54% 1250ms 14.69% math/rand.(*Rand).Int31n 350ms 4.11% 91.66% 1600ms 18.80% math/rand.(*Rand).Intn 260ms 3.06% 94.71% 580ms 6.82% math/rand.(*Rand).Int31 190ms 2.23% 96.94% 190ms 2.23% math/rand.(*rngSource).Int63 130ms 1.53% 98.47% 320ms 3.76% math/rand.(*Rand).Int63 110ms 1.29% 99.76% 110ms 1.29% runtime.memclrNoHeapPointers 0 0% 99.76% 8510ms 100% runtime.goexit 0 0% 99.76% 110ms 1.29% runtime.heapBits.initSpan 0 0% 99.76% 110ms 1.29% runtime.largeAlloc
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile `file`") var memprofile = flag.String("memprofile", "", "write memory profile to `file`")
const ( col = 10000 row = 10000 )
funcmain() { flag.Parse() if *cpuprofile != "" { f, err := os.Create(*cpuprofile) if err != nil { log.Fatal("could not create CPU profile: ", err) } if err := pprof.StartCPUProfile(f); err != nil { //监控cpu log.Fatal("could not start CPU profile: ", err) } defer pprof.StopCPUProfile() }
// 主逻辑区,进行一些简单的代码运算 x := [row][col]int{} s := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < row; i++{ for j := 0; j < col; j++ { x[i][j] = s.Intn(100000) } }
for i := 0; i < row; i++{ tmp := 0 for j := 0; j < col; j++ { tmp += x[i][j] } }
if *memprofile != "" { f, err := os.Create(*memprofile) if err != nil { log.Fatal("could not create memory profile: ", err) } runtime.GC() // GC,获取最新的数据信息 if err := pprof.WriteHeapProfile(f); err != nil { // 写入内存信息 log.Fatal("could not write memory profile: ", err) } f.Close() } }
编译运行,会根据入参生成两个对应的数据文件:
1 2 3 4 5 6 7 8
> go build > ./pprof -cpuprofile cpu.prof -memprofile mem.prof > ll total 2656 -rw-r--r--. 1 ma root 1183 Jan 9 21:39 cpu.prof # cpu运行数据 -rw-r--r--. 1 ma root 280 Jan 9 21:39 mem.prof # 内存数据 -rwxr-xr-x. 1 ma root 2707088 Jan 9 21:39 pprof # 编译后的可执行文件 -rw-r--r--. 1 ma root 1296 Jan 9 21:38 pprof.go
> go tool pprof cpu.prof Entering interactive mode (type "help" for commands) (pprof) help
Commands: cmd [n] [--cum] [focus_regex]* [-ignore_regex]* Produce a text report with the top n entries. Include samples matching focus_regex, and exclude ignore_regex. Add --cum to sort using cumulative data. Available commands: callgrind Outputs a graph in callgrind format disasm Output annotated assembly for functions matching regexp or address dot Outputs a graph in DOT format eog Visualize graph through eog evince Visualize graph through evince gif Outputs a graph image in GIF format gv Visualize graph through gv list Output annotated source for functions matching regexp pdf Outputs a graph in PDF format peek Output callers/callees of functions matching regexp png Outputs a graph image in PNG format proto Outputs the profile in compressed protobuf format ps Outputs a graph in PS format raw Outputs a text representation of the raw profile svg Outputs a graph in SVG format tags Outputs all tags in the profile text Outputs top entries in text form top Outputs top entries in text form tree Outputs a text rendering of call graph web Visualize graph through web browser weblist Output annotated source in HTML for functions matching regexp or address peek func_regex Display callers and callees of functions matching func_regex. ...
1.top
命令格式:top [n],查看排名前n个数据,默认为10。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
(pprof) top 8490ms of 8510ms total (99.76%) Dropped 13 nodes (cum <= 42.55ms) Showing top 10 nodes out of 15 (cum >= 110ms) flat flat% sum% cum cum% 6780ms 79.67% 79.67% 8510ms 100% main.main 670ms 7.87% 87.54% 1250ms 14.69% math/rand.(*Rand).Int31n 350ms 4.11% 91.66% 1600ms 18.80% math/rand.(*Rand).Intn 260ms 3.06% 94.71% 580ms 6.82% math/rand.(*Rand).Int31 190ms 2.23% 96.94% 190ms 2.23% math/rand.(*rngSource).Int63 130ms 1.53% 98.47% 320ms 3.76% math/rand.(*Rand).Int63 110ms 1.29% 99.76% 110ms 1.29% runtime.memclrNoHeapPointers 0 0% 99.76% 8510ms 100% runtime.goexit 0 0% 99.76% 110ms 1.29% runtime.heapBits.initSpan 0 0% 99.76% 110ms 1.29% runtime.largeAlloc