在计算机性能调试领域里,profiling 是指对应用程序的画像,画像就是应用程序使用 CPU 和内存的状况。 Go语言是一个对性能特别看重的语言,所以语言中自带了 profiling 的库,这篇文章就要讲解怎么在 golang 中作 profiling。html
Go语言项目中的性能优化主要有如下几个方面:node
Go语言内置了获取程序的运行数据的工具,包括如下两个标准库:git
runtime/pprof
:采集工具型应用运行数据进行分析net/http/pprof
:采集服务型应用运行时数据进行分析pprof开启后,每隔一段时间(10ms)就会收集下当前的堆栈信息,获取格格函数占用的CPU以及内存资源;最后经过对这些采样数据进行分析,造成一个性能分析报告。github
注意,咱们只应该在性能测试的时候才在代码中引入pprof。golang
若是你的应用程序是运行一段时间就结束退出类型。那么最好的办法是在应用退出的时候把 profiling 的报告保存到文件中,进行分析。对于这种状况,可使用runtime/pprof
库。 首先在代码中导入runtime/pprof
工具:web
import "runtime/pprof"
开启CPU性能分析:windows
pprof.StartCPUProfile(w io.Writer)
中止CPU性能分析:浏览器
pprof.StopCPUProfile()
应用执行结束后,就会生成一个文件,保存了咱们的 CPU profiling 数据。获得采样数据以后,使用go tool pprof
工具进行CPU性能分析。性能优化
记录程序的堆栈信息bash
pprof.WriteHeapProfile(w io.Writer)
获得采样数据以后,使用go tool pprof
工具进行内存性能分析。
go tool pprof
默认是使用-inuse_space
进行统计,还可使用-inuse-objects
查看分配对象的数量。
若是你的应用程序是一直运行的,好比 web 应用,那么可使用net/http/pprof
库,它可以在提供 HTTP 服务进行分析。
若是使用了默认的http.DefaultServeMux
(一般是代码直接使用 http.ListenAndServe(“0.0.0.0:8000”, nil)),只须要在你的web server端代码中按以下方式导入net/http/pprof
import _ "net/http/pprof"
若是你使用自定义的 Mux,则须要手动注册一些路由规则:
r.HandleFunc("/debug/pprof/", pprof.Index) r.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline) r.HandleFunc("/debug/pprof/profile", pprof.Profile) r.HandleFunc("/debug/pprof/symbol", pprof.Symbol) r.HandleFunc("/debug/pprof/trace", pprof.Trace)
若是你使用的是gin框架,那么推荐使用"github.com/DeanThompson/ginpprof"
。
无论哪一种方式,你的 HTTP 服务都会多出/debug/pprof
endpoint,访问它会获得相似下面的内容:
这个路径下还有几个子页面:
不论是工具型应用仍是服务型应用,咱们使用相应的pprof库获取数据以后,下一步的都要对这些数据进行分析,咱们可使用go tool pprof
命令行工具。
go tool pprof
最简单的使用方式为:
go tool pprof [binary] [source]
其中:
注意事项: 获取的 Profiling 数据是动态的,要想得到有效的数据,请保证应用处于较大的负载(好比正在生成中运行的服务,或者经过其余工具模拟访问压力)。不然若是应用处于空闲状态,获得的结果可能没有任何意义。
首先咱们来写一段有问题的代码:
// runtime_pprof/main.go package main import ( "flag" "fmt" "os" "runtime/pprof" "time" ) // 一段有问题的代码 func logicCode() { var c chan int for { select { case v := <-c: fmt.Printf("recv from chan, value:%v\n", v) default: } } } func main() { var isCPUPprof bool var isMemPprof bool flag.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") if err != nil { fmt.Printf("create cpu pprof failed, err:%v\n", err) return } pprof.StartCPUProfile(file) defer pprof.StopCPUProfile() } for i := 0; i < 8; i++ { go logicCode() } time.Sleep(20 * time.Second) if isMemPprof { file, err := os.Create("./mem.pprof") if err != nil { fmt.Printf("create mem pprof failed, err:%v\n", err) return } pprof.WriteHeapProfile(file) file.Close() } }
经过flag咱们能够在命令行控制是否开启CPU和Mem的性能分析。 将上面的代码保存并编译成runtime_pprof
可执行文件,执行时加上-cpu
命令行参数以下:
./runtime_pprof -cpu
等待30秒后会在当前目录下生成一个cpu.pprof
文件。
咱们使用go工具链里的pprof
来分析一下。
go tool pprof cpu.pprof
执行上面的代码会进入交互界面以下:
runtime_pprof $ go tool pprof cpu.pprof Type: cpu Time: Jun 28, 2019 at 11:28am (CST) Duration: 20.13s, Total samples = 1.91mins (568.60%) Entering interactive mode (type "help" for commands, "o" for options) (pprof)
咱们能够在交互界面输入top3
来查看程序中占用CPU前3位的函数:
(pprof) top3 Showing nodes accounting for 100.37s, 87.68% of 114.47s total Dropped 17 nodes (cum <= 0.57s) Showing top 3 nodes out of 4 flat flat% sum% cum cum% 42.52s 37.15% 37.15% 91.73s 80.13% runtime.selectnbrecv 35.21s 30.76% 67.90% 39.49s 34.50% runtime.chanrecv 22.64s 19.78% 87.68% 114.37s 99.91% main.logicCode
其中:
在大多数的状况下,咱们能够经过分析这五列得出一个应用程序的运行状况,并对程序进行优化。
咱们还可使用list 函数名
命令查看具体的函数分析,例如执行list logicCode
查看咱们编写的函数的详细分析。
(pprof) list logicCode Total: 1.91mins ROUTINE ================ main.logicCode in .../runtime_pprof/main.go 22.64s 1.91mins (flat, cum) 99.91% of Total . . 12:func logicCode() { . . 13: var c chan int . . 14: for { . . 15: select { . . 16: case v := <-c: 22.64s 1.91mins 17: fmt.Printf("recv from chan, value:%v\n", v) . . 18: default: . . 19: . . 20: } . . 21: } . . 22:}
经过分析发现大部分CPU资源被17行占用,咱们分析出select语句中的default没有内容会致使上面的case v:=<-c:
一直执行。咱们在default分支添加一行time.Sleep(time.Second)
便可。
或者能够直接输入web,经过svg图的方式查看程序中详细的CPU占用状况。 想要查看图形化的界面首先须要安装graphviz图形化工具。
Mac:
brew install graphviz
Windows: 下载graphviz 将graphviz
安装目录下的bin文件夹添加到Path环境变量中。 在终端输入dot -version
查看是否安装成功。
关于图形
的说明: 每一个框表明一个函数,理论上框的越大表示占用的CPU资源越多。 方框之间的线条表明函数之间的调用关系。 线条上的数字表示函数调用的次数。 方框中的第一行数字表示当前函数占用CPU的百分比,第二行数字表示当前函数累计占用CPU的百分比。
火焰图(Flame Graph)是 Bredan Gregg 建立的一种性能分析图表,由于它的样子近似 🔥而得名。上面的 profiling 结果也转换成火焰图,若是对火焰图比较了解能够手动来操做,不过这里咱们要介绍一个工具:go-torch
。这是 uber 开源的一个工具,能够直接读取 golang profiling 数据,并生成一个火焰图的 svg 文件。
go get -v github.com/uber/go-torch
火焰图 svg 文件能够经过浏览器打开,它对于调用图的最优势是它是动态的:能够经过点击每一个方块来 zoom in 分析它上面的内容。
火焰图的调用顺序从下到上,每一个方块表明一个函数,它上面一层表示这个函数会调用哪些函数,方块的大小表明了占用 CPU 使用的长短。火焰图的配色并无特殊的意义,默认的红、黄配色是为了更像火焰而已。
go-torch 工具的使用很是简单,没有任何参数的话,它会尝试从http://localhost:8080/debug/pprof/profile
获取 profiling 数据。它有三个经常使用的参数能够调整:
要生成火焰图,须要事先安装 FlameGraph工具,这个工具的安装很简单(须要perl环境支持),只要把对应的可执行文件加入到环境变量中便可。
git clone https://github.com/brendangregg/FlameGraph.git
FlameGraph
目录加入到操做系统的环境变量中。go-torch/render/flamegraph.go
文件中的GenerateFlameGraph
按以下方式修改,而后在go-torch
目录下执行go install
便可。// GenerateFlameGraph runs the flamegraph script to generate a flame graph SVG. func GenerateFlameGraph(graphInput []byte, args ...string) ([]byte, error) { flameGraph := findInPath(flameGraphScripts) if flameGraph == "" { return nil, errNoPerlScript } if runtime.GOOS == "windows" { return runScript("perl", append([]string{flameGraph}, args...), graphInput) } return runScript(flameGraph, args, graphInput) }
推荐使用https://github.com/wg/wrk 或 https://github.com/adjust/go-wrk
使用wrk进行压测:go-wrk -n 50000 http://127.0.0.1:8080/book/list
在上面压测进行的同时,打开另外一个终端执行go-torch -u http://127.0.0.1:8080 -t 30
,30秒以后终端会初夏以下提示:Writing svg to torch.svg
而后咱们使用浏览器打开torch.svg
就能看到以下火焰图了。
火焰图的y轴表示cpu调用方法的前后,x轴表示在每一个采样调用时间内,方法所占的时间百分比,越宽表明占据cpu时间越多。经过火焰图咱们就能够更清楚的找出耗时长的函数调用,而后不断的修正代码,从新采样,不断优化。
go test
命令有两个参数和 pprof 相关,它们分别指定生成的 CPU 和 Memory profiling 保存的文件:
咱们还能够选择将pprof与性能测试相结合,好比:
好比下面执行测试的同时,也会执行 CPU profiling,并把结果保存在 cpu.prof 文件中:
go test -bench . -cpuprofile=cpu.prof
好比下面执行测试的同时,也会执行 Mem profiling,并把结果保存在 cpu.prof 文件中:
go test -bench . -memprofile=./mem.prof
须要注意的是,Profiling 通常和性能测试一块儿使用,这个缘由在前文也提到过,只有应用在负载高的状况下 Profiling 才有意义。