Go: pprof与性能分析

tech2022-08-06  146

pprof是非常好用的性能分析工具


非 服 务 类 程 序 \red{非服务类程序}

源代码:

package main import ( "fmt" "time" ) func dfs(ans *[][]int,path []int,sum int,k int){ if sum > k { return } if sum == k { *ans = append(*ans, path) return } for i:=1;i<10;i++{ t := i path = append(path, t) sum += t dfs(ans, path, sum, k) sum -= t path = path[:len(path)-1:len(path)-1] } } func solve(k int) [][]int{ ans := make([][]int,0) path := make([]int,0) dfs(&ans,path,0,k) return ans } func main(){ start := time.Now() fmt.Println(len(solve(24))) fmt.Println(time.Since(start)) }

c p u 性 能 分 析 : \red{cpu性能分析:} cpu

package main import ( "flag" "fmt" "log" "os" "runtime/pprof" "time" ) var cpuProfile = flag.String("cpuprofile","","write cpu profile to file") func dfs(ans *[][]int,path []int,sum int,k int){ if sum > k { return } if sum == k { *ans = append(*ans, path) return } for i:=1;i<10;i++{ t := i path = append(path, t) sum += t dfs(ans, path, sum, k) sum -= t path = path[:len(path)-1:len(path)-1] } } func solve(k int) [][]int{ ans := make([][]int,0) path := make([]int,0) dfs(&ans,path,0,k) return ans } func main(){ flag.Parse() if *cpuProfile != "" { f, err := os.Create(*cpuProfile) if err != nil { log.Fatal(err) } _ = pprof.StartCPUProfile(f) defer pprof.StopCPUProfile() } start := time.Now() fmt.Println(len(solve(24))) fmt.Println(time.Since(start)) }

程序运行完毕后:

go tool pprof algorithm cpuProfile.prof top10 # 前10个 top5 -cum # 按cum排序 web # 输出图文件 web growslice # 查看特定函数的图文件

m e m 性 能 分 析 : \red{mem性能分析:} mem

package main import ( "flag" "fmt" "log" "os" "runtime/pprof" "time" ) var cpuProfile = flag.String("cpuprofile","","write cpu profile to file") var memprofile = flag.String("memprofile","","write memory profile to this file") func dfs(ans *[][]int,path []int,sum int,k int){ if sum > k { return } if sum == k { *ans = append(*ans, path) return } for i:=1;i<10;i++{ t := i path = append(path, t) sum += t dfs(ans, path, sum, k) sum -= t path = path[:len(path)-1:len(path)-1] } } func solve(k int) [][]int{ ans := make([][]int,0) path := make([]int,0) dfs(&ans,path,0,k) return ans } func main(){ flag.Parse() if *cpuProfile != "" { f, err := os.Create(*cpuProfile) if err != nil { log.Fatal(err) } _ = pprof.StartCPUProfile(f) defer pprof.StopCPUProfile() } start := time.Now() fmt.Println(len(solve(24))) fmt.Println(time.Since(start)) if *memprofile != "" { f, err := os.Create(*memprofile) if err != nil { log.Fatal(err) } _ = pprof.WriteHeapProfile(f) defer pprof.StopCPUProfile() } }

程序运行完毕后:

go tool pprof algorithm memProfile.mprof top5 # 列出top5 list main.dfs # 列出函数dfs

对 于 服 务 类 型 , 需 要 在 m a i n 文 件 中 加 入 : \red{对于服务类型,需要在main文件中加入:} main

import _ "net/http/pprof" func main(){ go func(){ http.ListenAndServe("0.0.0.0:8899",nil) }() }

在服务运行后,来分析数据:

go tool pprof -inuse_space http://your_ip:8899/debug/pprof/heap

进入命令交互洁面,top查看占用比:

top

图片分析更直观:

go tool pprof -inuse_space -cum -svg http://your_ip:8899/debug/pprof/heap > heap_inuse.svg

参考: Profiling Go Programs + 优秀中文翻译 【实践】使用Go pprof做内存性能分析 Go pprof性能调优 Golang 大杀器之性能剖析 PProf

最新回复(0)