go性能优化
Go性能優(yōu)化
- CPU profile:報(bào)告程序CPU使用情況,按照一定頻率去采集應(yīng)用程序CPU和寄存器上面的數(shù)據(jù)
- Memory Profile (Heap Profile):報(bào)告內(nèi)存使用情況
- Block Profiling:報(bào)告goroutines不在運(yùn)行狀況情況下,可以用來分析和查找死鎖等性能瓶頸
- Goroutine Profiling:報(bào)告goroutine的使用情況,有哪些goroutine它們調(diào)用關(guān)系是怎樣的
編譯:
andrew@andrew-G3-3590:~/go/src/basic/pprof_demo$ go build pprofile_demo.go andrew@andrew-G3-3590:~/go/src/basic/pprof_demo$ ./pprofile_demo -cpu=true andrew@andrew-G3-3590:~/go/src/basic/pprof_demo$ go tool pprof cpu.pprof File: pprofile_demo Type: cpu Time: Dec 21, 2020 at 11:08pm (CST) Duration: 20.17s, Total samples = 58.35s (289.35%) Entering interactive mode (type "help" for commands, "o" for options) (pprof) top 5 Showing nodes accounting for 58.25s, 99.83% of 58.35s total Dropped 14 nodes (cum <= 0.29s)flat flat% sum% cum cum%27.16s 46.55% 46.55% 46.38s 79.49% runtime.selectnbrecv19.22s 32.94% 79.49% 19.22s 32.94% runtime.chanrecv11.87s 20.34% 99.83% 58.25s 99.83% main.logicCode (pprof) quit- flat當(dāng)前函數(shù)占用CPU的耗時(shí)
- flat%當(dāng)前函數(shù)占用CPU的耗時(shí)百分比
- sum%函數(shù)占用CPU耗時(shí)累計(jì)百分比
- cum當(dāng)前函數(shù)加上調(diào)用當(dāng)前函數(shù)的函數(shù)占用CPU的總耗時(shí)
- cum%當(dāng)前函數(shù)加上調(diào)用當(dāng)前函數(shù)的函數(shù)占用CPU的總耗時(shí)百分比
- 最后一列:函數(shù)名稱
使用命令查看,具體哪個(gè)函數(shù)浪費(fèi)了比較多的時(shí)間
andrew@andrew-G3-3590:~/go/src/basic/pprof_demo$ go build pprofile_demo.go andrew@andrew-G3-3590:~/go/src/basic/pprof_demo$ ./pprofile_demo -cpu=true andrew@andrew-G3-3590:~/go/src/basic/pprof_demo$ go tool pprof cpu.pprof File: pprofile_demo Type: cpu Time: Dec 21, 2020 at 11:23pm (CST) Duration: 6.15s, Total samples = 17.10s (277.95%) Entering interactive mode (type "help" for commands, "o" for options) (pprof) top 3 Showing nodes accounting for 17.10s, 100% of 17.10s totalflat flat% sum% cum cum%7.79s 45.56% 45.56% 13.18s 77.08% runtime.selectnbrecv5.39s 31.52% 77.08% 5.39s 31.52% runtime.chanrecv3.92s 22.92% 100% 17.10s 100% main.logicCode (pprof) list logicCode Total: 17.10s ROUTINE ======================== main.logicCode in /home/andrew/go/src/basic/pprof_demo/pprofile_demo.go3.92s 17.10s (flat, cum) 100% of Total. . 11:// 一段有問題的代碼. . 12:func logicCode() {. . 13: var c chan int. . 14: for {. . 15: select {3.92s 17.10s 16: case v := <- c:. . 17: fmt.Printf("recv from chan, value:%v", v). . 18: default:. . 19: //time.Sleep(time.Second). . 20:. . 21: } (pprof) web # 如果裝的有g(shù)raphviz工具,這里會(huì)在瀏覽器打開一個(gè)svg圖片如下:使用go繪制火焰圖
講到這里就不得不說下,使用go繪制火焰圖分析代碼性能:
要是遇到提示沒有權(quán)限,參考: go get提示沒有 權(quán)限解決
直接使用pprof分析:
pprof -http=:8080 cpu.prof執(zhí)行之后會(huì)在瀏覽器中打開,并顯示對(duì)程序的分析圖
go-torch 工具的使用非常簡(jiǎn)單,沒有任何參數(shù)的話,它會(huì)嘗試從 http://localhost:8080/debug/pprof/profile 獲取 profiling 數(shù)據(jù)。它有三個(gè)常用的參數(shù)可以調(diào)整:
-u --url:要訪問的 URL,這里只是主機(jī)和端口部分
-s --suffix:pprof profile 的路徑,默認(rèn)為 /debug/pprof/profile
-t --seconds:要執(zhí)行 profiling 的時(shí)間長(zhǎng)度,默認(rèn)為 30s
火焰圖的調(diào)用順序從下到上,每個(gè)方塊代表一個(gè)函數(shù),它上面一層表示這個(gè)函數(shù)會(huì)調(diào)用哪些函數(shù),方塊的大小代表了占用 CPU 使用的長(zhǎng)短,火焰圖的配色并沒有特殊的意義。
總結(jié)
- 上一篇: go get 命令提示没有权限问题解决
- 下一篇: 【软件质量】ISO-9126质量模型