golang pprof 使用


一、概述

gopprof工具可以用來監測進程的運行數據,用於監控程序的性能,對內存使用和CPU使用的情況統信息進行分析。

官方提供了兩個包:runtime/pprofnet/http/pprof,前者用於普通代碼的性能分析,后者用於web服務器的性能分析。

官方文檔:

https://golang.org/pkg/runtime/pprof/

https://golang.org/pkg/net/http/pprof/#Index

https://github.com/google/pprof/blob/master/doc/pprof.md

二、runtime/pprof的使用

該包提供了一系列用於調試信息的方法,可以很方便的對堆棧進行調試。

通常用得多得是以下幾個:

  • StartCPUProfile:開始監控cpu。
  • StopCPUProfile:停止監控cpu,使用StartCPUProfile后一定要調用該函數停止監控。
  • WriteHeapProfile:把堆中的內存分配信息寫入分析文件中。

示例代碼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main

import (
"flag"
"runtime/pprof"
"log"
"runtime"
"math/rand"
"os"
"time"
)

var cpuprofile = flag.String("cpuprofile", "", "write cpu profile `file`")
var memprofile = flag.String("memprofile", "", "write memory profile to `file`")

const (
col = 10000
row = 10000
)

func main() {
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命令即可分析。

三、go tool pprof

生成數據文件后使用go tool pprof file進入交互式界面進行數據分析,輸入help可以查看命令。

用法詳見:pprof.md

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
> 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

2.tree

命令格式:tree [n],以樹狀圖形式顯示,默認顯示10個。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
(pprof) tree 5
8250ms of 8510ms total (96.94%)
Dropped 13 nodes (cum <= 42.55ms)
Showing top 5 nodes out of 15 (cum >= 190ms)
----------------------------------------------------------+-------------
flat flat% sum% cum cum% calls calls% + context
----------------------------------------------------------+-------------
6780ms 79.67% 79.67% 8510ms 100% | main.main
1600ms 100% | math/rand.(*Rand).Intn
----------------------------------------------------------+-------------
1250ms 100% | math/rand.(*Rand).Intn
670ms 7.87% 87.54% 1250ms 14.69% | math/rand.(*Rand).Int31n
580ms 100% | math/rand.(*Rand).Int31
----------------------------------------------------------+-------------
1600ms 100% | main.main
350ms 4.11% 91.66% 1600ms 18.80% | math/rand.(*Rand).Intn
1250ms 100% | math/rand.(*Rand).Int31n
----------------------------------------------------------+-------------
580ms 100% | math/rand.(*Rand).Int31n
260ms 3.06% 94.71% 580ms 6.82% | math/rand.(*Rand).Int31
190ms 100% | math/rand.(*rngSource).Int63
----------------------------------------------------------+-------------
190ms 100% | math/rand.(*Rand).Int31
190ms 2.23% 96.94% 190ms 2.23% | math/rand.(*rngSource).Int63
----------------------------------------------------------+-------------

3.web

以web形式查看,在web服務的時候經常被用到,需要安裝gv工具,官方網頁:http://www.graphviz.org/。

linux用戶使用yum install graphviz安裝即可,當然,純命令行界面是不能查看的。

windows用戶下載msi包安裝后需要把安裝目錄下的bin目錄添加到環境變量才行。

如果沒有安裝gv工具,使用會報錯:

Cannot find dot, have you installed Graphviz?

exec: “firefox”: executable file not found in $PATH

4.其他

其他的都是以不同形式展現出來,大同小異,以后有時間再測試。

四、web服務器監測

在web服務器中監測只需要在import部分加上監測包即可:

1
2
3
import(
_ "net/http/pprof"
)

當服務開啟后,在當前服務環境的http://ip:port/debug/pprof頁面可以看到當前的系統信息:

點擊查看具體的信息:

通常可以對服務器在一段時間內進行數據采樣,然后分析服務器的耗時和性能:

1
go tool pprof http://*:*/debug/pprof/profile

使用該命令后會對服務進行30s的采樣,這段時間內可以盡量多使用web服務,生成多一些統計數據。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
> go tool pprof http://127.0.0.1:8080/debug/pprof/profile
Fetching profile from http://127.0.0.1:8080/debug/pprof/profile
Please wait... (30s)
Saved profile in \pprof\pprof.127.0.0.1.samples.cpu.001.pb.gz
Entering interactive mode (type "help" for commands)
(pprof) top
3870ms of 4800ms total (80.62%)
Dropped 37 nodes (cum <= 24ms)
Showing top 10 nodes out of 66 (cum >= 110ms)
flat flat% sum% cum cum%
1230ms 25.62% 25.62% 1300ms 27.08% runtime.mapaccess1_faststr
860ms 17.92% 43.54% 860ms 17.92% runtime.memclrNoHeapPointers
810ms 16.88% 60.42% 1010ms 21.04% runtime.scanobject
190ms 3.96% 64.38% 190ms 3.96% runtime.heapBitsForObject
160ms 3.33% 67.71% 190ms 3.96% strconv.ParseInt
140ms 2.92% 70.62% 1720ms 35.83% business_sets/haoxingdai_qiangdan/server/handler.makeOrder4Replace
140ms 2.92% 73.54% 1990ms 41.46% runtime.mallocgc
120ms 2.50% 76.04% 120ms 2.50% runtime.heapBitsSetType
110ms 2.29% 78.33% 1680ms 35.00% runtime.mapassign
110ms 2.29% 80.62% 110ms 2.29% runtime.memhash

使用web命令后會生成采樣時間內每個系統調用的耗時分析,可以用來分析web服務的響應時間都用在哪了:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM