日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

go性能优化

發(fā)布時(shí)間:2025/3/15 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 go性能优化 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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)系是怎樣的
package mainimport ("flag""fmt""os""runtime/pprof""time" )// 一段有問題的代碼 func logicCode() {var c chan intfor {select {case v := <- c:fmt.Printf("recv from chan, value:%v", v)default:time.Sleep(time.Second) // 耗費(fèi)CPU的時(shí)候注釋這行}} }func main() {var isCPUPprof boolvar isMemPprof boolflag.BoolVar(&isCPUPprof, "cpu", false, "turn cpu pprof on")flag.BoolVar(&isMemPprof, "mem", false, "turn mem pprof on")flag.Parse()if isCPUPprof {file, err := os.Create("./cpu.pprof") // 在當(dāng)前文件夾下創(chuàng)建一個(gè)文件if err != nil {fmt.Printf("creat cpu pprof failed, err : %v", err)return}_ = pprof.StartCPUProfile(file)defer pprof.StopCPUProfile()}for i:=0; i < 8; i++ {go logicCode()}time.Sleep(6 * time.Second)if isMemPprof {file, err := os.Create("./mem.pprof")if err != nil {fmt.Printf("create mem pprof failed, err : %v", err)return}_ = pprof.WriteHeapProfile(file)file.Close()}}

編譯:

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繪制火焰圖分析代碼性能:

  • 第一步安裝pprof
  • go get github.com/google/pprof

    要是遇到提示沒有權(quán)限,參考: go get提示沒有 權(quán)限解決

    直接使用pprof分析:

    pprof -http=:8080 cpu.prof

    執(zhí)行之后會(huì)在瀏覽器中打開,并顯示對(duì)程序的分析圖

  • 安裝FlameGraph
  • git clone https://github.com/brendangregg/FlameGraph.git export PATH=$PATH:home/FlameGraph #換成自己的路徑
  • 安裝graphviz
  • sudo apt-get install graphviz
  • 安裝go-torch
  • go get github.com/uber/go-torch

    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性能优化的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。