go中提供了pprof包來作代碼的性能監控,在兩個地方有包:html
其實net/http/pprof中只是使用runtime/pprof包來進行封裝了一下,並在http端口上暴露出來。node
若是你的go程序是用http包啓動的web服務器,想要查看本身的web服務器的狀態。這個時候就能夠選擇net/http/pprof。
git
import _ "net/http/pprof"
而後就能夠在瀏覽器中使用http://localhost:port/debug/pprof/ 直接看到當前web服務的狀態,包括CPU佔用狀況和內存使用狀況等。
固然,非WEB的也能夠用下面方式啓動WEB。
在 main 方法中增長
func main() {
go func() {
http.ListenAndServe("localhost:6060", nil)
}()
下圖就是訪問該網址的一次截圖:
關鍵代碼:github
import "runtime/pprof"golang
func main() {
f, err := os.OpenFile("./tmp/cpu.prof", os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
log.Fatal(err)
}
defer f.Close()
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()web
注意,有時候 defer f.Close(), defer pprof.StopCPUProfile() 會執行不到,這時候咱們就會看到 prof 文件是空的, 咱們須要在本身代碼退出的地方,增長上下面兩行,確保寫文件內容了。瀏覽器
pprof.StopCPUProfile() 服務器
f.Close()性能
咱們可使用 go tool pprof (應用程序) (應用程序的prof文件) 方式來對這個 prof 文件進行分析。debug
$ go tool pprof HuaRongDao ./tmp/cpu.prof
Entering interactive mode (type "help" for commands)
(pprof)
一些經常使用 pprof 的命令:
在默認狀況下,top命令會輸出以本地取樣計數爲順序的列表。咱們能夠把這個列表叫作本地取樣計數排名列表。
(pprof) top
2700ms of 3200ms total (84.38%)
Dropped 58 nodes (cum <= 16ms)
Showing top 10 nodes out of 111 (cum >= 80ms)
flat flat% sum% cum cum%
670ms 20.94% 20.94% 670ms 20.94% runtime.mach_semaphore_signal
580ms 18.12% 39.06% 590ms 18.44% runtime.cgocall
370ms 11.56% 50.62% 370ms 11.56% runtime.mach_semaphore_wait
360ms 11.25% 61.88% 360ms 11.25% runtime.memmove
210ms 6.56% 68.44% 580ms 18.12% golang.org/x/mobile/gl.(*context).DoWork
120ms 3.75% 72.19% 120ms 3.75% runtime.usleep
110ms 3.44% 75.62% 110ms 3.44% image/png.filterPaeth
100ms 3.12% 78.75% 160ms 5.00% compress/flate.(*decompressor).huffSym
100ms 3.12% 81.88% 100ms 3.12% image/draw.drawNRGBASrc
80ms 2.50% 84.38% 80ms 2.50% runtime.memclr
(pprof)
參考: https://github.com/hyper-carrot/go_command_tutorial/blob/master/0.12.md
默認狀況下top命令會列出前10項內容。可是若是在top命令後面緊跟一個數字,那麼其列出的項數就會與這個數字相同。
與gv命令相似,web命令也會用圖形化的方式來顯示概要文件。但不一樣的是,web命令是在一個Web瀏覽器中顯示它。若是你的Web瀏覽器已經啓動,那麼它的顯示速度會很是快。若是想改變所使用的Web瀏覽器,能夠在Linux下設置符號連接/etc/alternatives/gnome-www-browser或/etc/alternatives/x-www-browser,或在OS X下改變SVG文件的關聯Finder。
mac 下 修改默認打開方式: 右鍵一個想處理的文件,按alt 鍵(lion)出現always open with,而後打開,整個過程當中, 先右鍵,而後一直按 alt, 一直到打開爲止。
參考資料:
go tool pprof
https://github.com/hyper-carrot/go_command_tutorial/blob/master/0.12.md
Go的pprof使用
http://www.cnblogs.com/yjf512/archive/2012/12/27/2835331.html
Profiling Go Programs
https://blog.golang.org/profiling-go-programs
關鍵代碼
fm, err := os.OpenFile("./tmp/mem.out", os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
log.Fatal(err)
}
pprof.WriteHeapProfile(fm)
fm.Close()
在咱們須要生成當時的內存狀況時,只須要執行上面代碼便可。
觸發的條件能夠經過 http 的一個接口,或者退出時,或者接收到某個特殊信號,這些邏輯就須要本身實現了。
分析方法,跟之上CPU的分析方法一致。
參考資料:
[golang]內存不斷增加bytes.makeSlice
http://studygolang.com/articles/2763