golang性能分析之pprof

在 Go 语言中,使用 pprof 进行性能分析是优化代码的常用手段。以下简要介绍操作步骤:

1. 导入 pprof 包

在代码中导入 net/http/pprof 包(即使你不需要 HTTP 服务),它会自动注册性能分析相关的路由:

go 复制代码
import (
    _ "net/http/pprof" // 自动注册 pprof 路由
    "net/http"
)

func main() {
    // 启动一个 HTTP 服务(用于 pprof 分析)
    go func() {
        http.ListenAndServe("localhost:6060", nil)
    }()

    // ... 你的业务代码 ...
}

2. 生成性能分析数据

通过 HTTP 接口获取数据

启动程序后,访问以下接口获取分析数据:

bash 复制代码
http://localhost:6060/debug/pprof/:所有可用的性能分析类型。

http://localhost:6060/debug/pprof/profile:CPU 分析(默认采集 30 秒)。

http://localhost:6060/debug/pprof/heap:内存分析。

http://localhost:6060/debug/pprof/block:阻塞分析。

http://localhost:6060/debug/pprof/goroutine:Goroutine 分析。

通过命令行直接采集

bash 复制代码
# 采集 CPU 数据(默认 30 秒)
go tool pprof http://localhost:6060/debug/pprof/profile

# 采集内存数据
go tool pprof http://localhost:6060/debug/pprof/heap

# 采集 60 秒 CPU 数据
go tool pprof http://localhost:6060/debug/pprof/profile?seconds=60

3. 分析性能数据

交互式命令行分析

运行命令后进入交互模式:

bash 复制代码
go tool pprof http://localhost:6060/debug/pprof/profile

常用命令:

top:显示耗时最多的函数。

list 函数名:查看具体函数的代码行耗时。

web:生成可视化调用图(需安装 Graphviz)。

svg:生成 SVG 格式的火焰图。

生成火焰图

安装 Graphviz:

bash 复制代码
# macOS
brew install graphviz

# Ubuntu/Debian
apt-get install graphviz

生成火焰图:

bash 复制代码
go tool pprof -http=:8080 pprof.profile

浏览器打开 http://localhost:8080,选择 Flame Graph 查看。

4. 代码中手动采集数据

如果不使用 HTTP 服务,可以手动生成分析文件:

go 复制代码
import (
    "os"
    "runtime/pprof"
)

func main() {
    // CPU 分析
    f, _ := os.Create("cpu.pprof")
    pprof.StartCPUProfile(f)
    defer pprof.StopCPUProfile()

    // ... 你的代码 ...

    // 内存分析
    mf, _ := os.Create("mem.pprof")
    pprof.WriteHeapProfile(mf)
    defer mf.Close()
}

5. 分析 Benchmark 性能

在测试文件中结合 testing 和 pprof:

go 复制代码
func BenchmarkMyFunc(b *testing.B) {
    // 启动 CPU 分析
    f, _ := os.Create("benchmark_cpu.pprof")
    pprof.StartCPUProfile(f)
    defer pprof.StopCPUProfile()

    // 运行被测函数
    for i := 0; i < b.N; i++ {
        MyFunc()
    }
}

运行 Benchmark 并生成分析文件:

bash 复制代码
go test -bench=. -cpuprofile=benchmark_cpu.pprof
go tool pprof benchmark_cpu.pprof
  1. 常见分析场景
    CPU 瓶颈:查看 top 列表,优化高耗时函数。

内存泄漏:分析 heap,检查未释放的内存分配。

Goroutine 泄漏:通过 goroutine 分析,找到未退出的 Goroutine。

阻塞分析:使用 block 类型,查找程序阻塞点。

示例 :分析 HTTP 服务

启动一个 HTTP 服务并导入 pprof。

使用压测工具(如 wrk 或 ab)模拟请求。

采集 CPU 或内存数据:

bash 复制代码
go tool pprof http://localhost:6060/debug/pprof/profile

分析火焰图,找到热点函数。

通过以上步骤,你可以快速定位 Go 程序的性能瓶颈并进行优化。实践中建议结合火焰图和代码逻辑进行深度分析。

相关推荐
ascarl20103 分钟前
准确--k8s cgroup问题排查
java·开发语言
酷爱码28 分钟前
Spring Boot项目中JSON解析库的深度解析与应用实践
spring boot·后端·json
fpcc1 小时前
跟我学c++中级篇——理解类型推导和C++不同版本的支持
开发语言·c++
莱茵菜苗1 小时前
Python打卡训练营day46——2025.06.06
开发语言·python
AI小智1 小时前
Google刀刃向内,开源“深度研究Agent”:Gemini 2.5 + LangGraph 打造搜索终结者!
后端
爱学习的小道长1 小时前
Python 构建法律DeepSeek RAG
开发语言·python
java干货1 小时前
虚拟线程与消息队列:Spring Boot 3.5 中异步架构的演进与选择
spring boot·后端·架构
luojiaao1 小时前
【Python工具开发】k3q_arxml 简单但是非常好用的arxml编辑器,可以称为arxml杀手包
开发语言·python·编辑器
终焉代码1 小时前
STL解析——list的使用
开发语言·c++
一只叫煤球的猫1 小时前
MySQL 8.0 SQL优化黑科技,面试官都不一定知道!
后端·sql·mysql