天天看点

golang-pprof性能分析工具1.安装环境2.性能监控代码3.CPU性能查看3.引用

编写好了golang服务之后,接着要开始关注服务的CPU,内存使用情况。golang提供了性能剖析工具,记录一些自己搜集到的信息,写下一些实践的情况。在golang中内置了pprof工具,专门来做golang语言的优化。

  • 1.安装环境
  • 2.性能监控代码
  • 3.CPU性能查看
  • 3.引用

1.安装环境

go get -u github.com/google/pprof
           

2.性能监控代码

这段代码将会开启一个http的网站,对外提供监控访问的地址。

import (
	"net/http"
    _ "net/http/pprof"
)
go func() {
    http.ListenAndServe(":10003", nil)
}()
           
golang-pprof性能分析工具1.安装环境2.性能监控代码3.CPU性能查看3.引用

能通过这个网址检查程序的内存分配,CPU占用,mutex,gorouine之类的信息。可以直接通过浏览器访问获取信息。

  • CPU profile:报告程序的 CPU 使用情况,按照一定频率去采集应用程序在 CPU 和寄存器上面的数据
  • Memory Profile(Heap Profile):报告程序的内存使用情况
  • Block Profiling:报告 goroutines 不在运行状态的情况,可以用来分析和查找死锁等性能瓶颈
  • Goroutine Profiling:报告 goroutines 的使用情况,有哪些 goroutine,它们的调用关系是怎样的

3.CPU性能查看

打开一个命令行输入:

PS D:\work\trunk\doc> go tool pprof -http=":8081" http://127.0.0.1:10006/debug/pprof/profile?seconds=200
Fetching profile over HTTP from http://127.0.0.1:10006/debug/pprof/profile?seconds=200
Saved profile in C:\Users\xxxxxxxxx\pprof\pprof.samples.cpu.003.pb.gz
Serving web UI on http://localhost:8081
           

在本地将会开启 -http=":8081" 表示在8081网站,将会呈现最后结果。这次监控将会抓取 http://127.0.0.1:10006/debug/pprof/profile 提供的性能信息。?seconds=200 表示持续抓取200sec。到时间之后它们将会存储到本地的一个文件中: Saved profile in C:\Users\xxxxxxxxx\pprof\pprof.samples.cpu.003.pb.gz 。

想知道这个处理的细节,可以直接阅读golang的源码:

// C:\Go\src\net\http\pprof\pprof.go
// Profile responds with the pprof-formatted cpu profile.
// Profiling lasts for duration specified in seconds GET parameter, or for 30 seconds if not specified.
// The package initialization registers it as /debug/pprof/profile.
func Profile(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("X-Content-Type-Options", "nosniff")
	sec, err := strconv.ParseInt(r.FormValue("seconds"), 10, 64)
	if sec <= 0 || err != nil {
		sec = 30
	}
}
           

它的接口有两个能支持输入seconds

"trace":        "A trace of execution of the current program. You can specify the duration in the seconds GET parameter. After you get the trace file, use the go tool trace command to investigate the trace.",
"profile":      "CPU profile. You can specify the duration in the seconds GET parameter. After you get the profile file, use the go tool pprof command to investigate the profile.",
           

通过加载文件来调出分析网站:

PS D:\work\trunk\doc> go tool pprof -http=":8989" C:\Users\xxxxxxxxx\pprof\pprof.samples.cpu.003.pb.gz
Serving web UI on http://localhost:8989
           

也能通过这个命令直接加载之前监控的剖析文件。

列名 含义
flat 函数执行消耗时间,采样时,该函数正在运行的次数*采样频率(10ms),即得到估算的函数运行”采样时间”。这里不包括函数等待子函数返回。
flat% flat / 总采样时间值
sum% 前面每一行的flat占比总和
cum 累计量,该函数出现在调用堆栈的采样时间,包括函数等待子函数返回。因此 flat <= cum
cum% cum占用总时间的比例

火焰图方式

golang-pprof性能分析工具1.安装环境2.性能监控代码3.CPU性能查看3.引用

调用关系图

golang-pprof性能分析工具1.安装环境2.性能监控代码3.CPU性能查看3.引用

3.引用

  • [1] 火焰图工具网站
  • [2] Golang性能测试工具PProf应用详解
  • [3] 深度解密go语言之pprof
  • [4] graphviz官网
  • [5] Go 大杀器之性能剖析 PProf
  • [6] qcachegrindwin下载地址