Go性能调优
在计算机性能调试领域里,profiling 是指对应用程序的画像,画像就是应用程序使用 CPU 和内存的情况Go语言是一个对性能特别看重的语言,因此语言中自带了 profiling 的库,这篇文章就要讲解怎么在 golang中做 profiling。
Go性能优化
Go语言项目中的性能优化主要有以下几个方面:
- CPU profile: 报告程序的 CPU 使用情况,按照一定频率去采集应用程序在 CPU 和存器上面的数据
- Memory Profile (Heap Profile) : 报告程序的内存使用情况
- Block Profiling: 报告 goroutines 不在运行状态的情况,可以用来分析和查找死锁等性能瓶颈
- Goroutine Profiling: 报告 goroutines 的使用情况,有哪些 goroutine,它们的调用关系是怎样的
采集性能数据
Go语言内置了获取程序的运行数据的工具,包括以下两个标准库
runtime/pprof: 采集工具型应用运行数据进行分析 net/http/pprof : 采集服务型应用运行时数据进行分析
pprof开启后,每隔一段时间 (10ms) 就会收集下当前的堆栈信息,获取格格函数占用的CPU以及内存资源最后通过对这些采样数据进行分析,形成一个性能分析报告。 注意,我们只应该在性能测试的时候才在代码中引入pprof
工具型应用
如果你的应用程序是运行一段时间就结束退出类型。那么最好的办法是在应用退出的时候把 profiling的报告保存到文件中,进行分析。对于这种情况,可以使用 runtime/pprof 库。 首先在代码中导runtime/pprof工具:
import "runtime/pprof"
CPU性能分析 开启CPU性能分析:
pprof.StartCPUProfile(w io.Writer)
应用执行结束后,就会生成一个文件,保存了我们的 CPU profiling 数据。得到采样数据之后,使用 gotool pprof 工具进行CPU性能分析。
内存性能优化
记录程序的堆栈信息
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,访问它会得到类似下面的内容
这个路径下还有几个子页面:
·/debug/pprof/profile: 访问这个链接会自动进行 CPU profiling,持续 30s,并生成一个文件供下载
/debug/pprof/heap: Memory Profiling 的路径,访问这个链接会得到一个内存 Profiling 结果的文件
/debug/pprof/block: block Profiling 的路经
·/debug/pprof/goroutines: 运行的 goroutines 列表,以及调用关系
go tool pprof命令
不管是工具型应用还是服务型应用,我们使用相应的pprof库获取数据之后,下一步的都要对这些数据进行分析,我们可以使用 go tool pprof 命令行工具
go tool pprof 最简单的使用方式为:
go tool pprof [binary] [source]
其中:
·binary 是应用的二进制文件,用来解析各种符号
·source 表示 profile 数据的来源,可以是本地的文件,也可以是 http 地址
注意事项: 获取的 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
其中: 当前函数占用CPU的耗时flat:
·flat: :当前函数占用CPU的耗时百分比
·sun%: 函数占用CPU的耗时累计百分比
·cum: 当前函数加上调用当前函数的函数占用CPU的总耗时
·cum%:当前函数加上调用当前函数的函数占用CPU的总耗时百分比
最后一列: 函数名称. 在大多数的情况下,我们可以通过分析这五列得出一个应用程序的运行情况,并对程序进行优化. 我们还可以使用 list 函数名 命令查看具体的函数分析,例如执行 ist 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) 即可。