這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
轉載自:http://www.jianshu.com/p/162f44022eb7
概要
profile就是定時採樣,收集cpu,記憶體等資訊,進而給出效能最佳化指導,golang 官方提供了golang自己的效能分析工具的用法,也給出了指導,官方的介紹
環境
golang環境, graphviz
產生profile方法
golang目前提供了3中profile,分別是 cpu profile, memery profile, blocking profile, 對於如何產生這些profile有兩種辦法,一種是使用 net/http/pprof 包,一種是需要自己手寫代碼,下面分別介紹一下
1. net/http/pprof 方法
這種方法是非常非常簡單的,只需要引入 net/http/pprof 包就可以了,網頁上可以查看
package mainimport ( "fmt" "log" "net/http" _ "net/http/pprof")func main() { go func() { for { fmt.Println("hello world") } }() log.Fatal(http.ListenAndServe("0.0.0.0:8080", nil))}
http://127.0.0.1:8080/debug/pprof 查看整體資訊
http://127.0.0.1:8080/debug/pprof/profile 可以將cpu profile下載下來觀察分析
從terminal進入profile,進行細緻分析
go tool pprof http://localhost:6060/debug/pprof/profile
go tool pprof http://localhost:6060/debug/pprof/heap
go tool pprof http://localhost:6060/debug/pprof/block
- 寫代碼的方法
func main() { cpuf, err := os.Create("cpu_profile") if err != nil { log.Fatal(err) } pprof.StartCPUProfile(cpuf) defer pprof.StopCPUProfile() ctx, _ := context.WithTimeout(context.Background(), time.Second*5) test(ctx) time.Sleep(time.Second * 3) memf, err := os.Create("mem_profile") if err != nil { log.Fatal("could not create memory profile: ", err) } if err := pprof.WriteHeapProfile(memf); err != nil { log.Fatal("could not write memory profile: ", err) } memf.Close()}func test(c context.Context) { i := 0 j := 0 go func() { m := map[int]int{} for { i++ m[i] = i } }() go func() { m := map[int]int{} for { j++ m[i] = i } }() select { case <-c.Done(): fmt.Println("done, i", i, "j", j) return }}
會產生兩個profile,一個是cpu的,一個是記憶體的。進入proflie 方法
go tool pprof main profile
main 代表的是二進位檔案,也就是編譯出來的可執行檔
profile 就是上文中產生的profile,可以是cpu_profile, 也可以是mem_profile
對於cpu_profile 來說,代碼開始的時候就可以開始統計了
mem_profile 部分代碼如果寫在代碼開始的位置是統計不出來的,需要找到一個比較好的位置
如何分析 profile
1.範例程式碼如下
package mainimport ( "flag" "fmt" "log" "math/rand" "os" "runtime/pprof" "sort" "time")var num = 100000000var findNum = 10000000var t = flag.String("t", "map", "use map")func main() { cpuf, err := os.Create("cpu_profile") if err != nil { log.Fatal(err) } pprof.StartCPUProfile(cpuf) defer pprof.StopCPUProfile() flag.Parse() if *t == "map" { fmt.Println("map") findMapMax() } else { fmt.Println("slice") findSliceMax() }}func findMapMax() { m := map[int]int{} for i := 0; i < num; i++ { m[i] = i } for i := 0; i < findNum; i++ { toFind := rand.Int31n(int32(num)) _ = m[int(toFind)] }}func findSliceMax() { m := make([]int, num) for i := 0; i < num; i++ { m[i] = i } for i := 0; i < findNum; i++ { toFind := rand.Int31n(int32(num)) v := sort.SearchInts(m, int(toFind)) fmt.Println(v) }}
代碼很簡單,主要是為了介紹如何分析profile,達到效果即可,不要在意細節。
這段代碼就是分別用 map, slice 兩種資料結構, 先產生 num 個元素,在從map, slice 中 隨機找到 findNum 個元素, 選用map 還是 slice 可以通過 -t 來指定,本demo採用 非 net/http/pprof 方式
2.準備工作
需要產生 profile 檔案 和 二進位檔案
產生二進位檔案: go build main.go (執行命令後會產生 main 二進位檔案)
產生 profile: ./main (不指定-t ,預設使用map資料結構,會產生 cpu_profile, 這個檔案就是我們要分析的profile)
3.分析profile
- 現在準備工作做好了,我們目前產生了 main 二進位可執行檔,cpu_profile 效能分析需要的profile, 接下來我們要正式進入profile進行分析了
- go tool pprof main cpu_profile 執行這個命令就進入了profile 檔案了,這時候我們已經可以開始分析代碼了
- 輸入help,可以查看都支援哪些操作,有很多命令可以根據需要進行選擇,我們只介紹4個我自己比較喜歡用的命令 web, top, peek, list
*web----- 在profile輸入 web, 會產生網頁版的調用分析圖(需要安裝 graphviz)如:
web
輸入web命令後,會自動開啟瀏覽器出現如下內容:
web2
這樣就可以看到每個步驟佔用多少時間了,可以對效能進行大致的分析,但是很多時候可能出現的並不是我們關心的,比如這個demo中看到的都是不認識的函數(其實都是map的runtime操作)
因為效能統計都是採樣操作,所以不是每次統計出來的都一樣, 最重要的是經常統計出來的都是底層操作,並不是我們關心的,而且也不是每個人都能看得懂,我們更需要一種直觀的辦法,很直觀的能把自己寫的代碼耗時都看出來,下面就介紹一種我個人覺得非常好的方法。
peek findMapMax (因為根據1可以看出來消耗都在 findMapMax)
web2
3)list main.findMapMax (根據2可以看出來名字是 main.findMapMax)
web2
妙用 peek list指令可以很直觀的看出來,我們的代碼問題在 m[i] = i, 這就說明了就是map的寫操作耗費了38.75s, 而44行的讀操作只用了2.35s, 針對這個demo,我們要最佳化的就是 m[i] = i ,因為這句操作已經語言層級的,我們是沒有能力對他進行最佳化的,所以這時候如果需求只能用map,那麼這個程式幾乎沒有最佳化空間了,如果需求可以使用其他的資料結構,那我們肯定會把map修改為slice,眾所周知 map 每次存一個函數都要進行hash計算,而且存的多了到達一定的數量,還要重新對map進行重新分配空間的操作,所以肯定是耗時的。
總結
- go run main.go -t=slice 會使用slice的資料結構,同學們可以自行按照文章的方法進行分析一下,檢驗一下自己掌握的如何。
- 在profile中執行help命令,會列出所有的命令,有興趣可以去研究
- memory 也是同樣的分析方法,demo 中 mem_profile 產生的位置可能需要調整,我沒有進行驗證,降低memory 使用也會大幅提升效能。
- 還有一種 blocking profile 在手寫方式中沒有給出,有興趣的可以自己google一下