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 程序的性能瓶颈并进行优化。实践中建议结合火焰图和代码逻辑进行深度分析。

相关推荐
OKkankan4 小时前
python 进阶(三):面向对象进阶——从“会写 class”到设计可维护的后端对象
开发语言·数据结构·python
打工仔折腾 AI4 小时前
极空间部署Portainer实战:绕过Socket权限、解决9000端口冲突与固定内网访问
人工智能·后端·python·性能优化
魔都吴所谓4 小时前
【Python】 多线程 & 多进程&协程 常见题 + 应用场景整理
开发语言·python
FfHUCisI4 小时前
Linux 网络 IO 模型演进与 Epoll 内部机制
golang
king13598745505 小时前
LiteRT Tensor API:C++ 编写端侧模型图,XNNPACK、GPU 与 NPU 怎么选?
开发语言·c++·人工智能·chatgpt·架构·aigc
geovindu5 小时前
rust: Template Method Pattern(续)
开发语言·后端·设计模式·rust·行为模式·板方法模式
外收内放5 小时前
面向对象高级(封装)
开发语言·python
每天一道题5 小时前
从单体到微服务:Service、Instance 与服务拆分到底是什么关系?
后端·微服务·云原生·架构
怕浪猫5 小时前
前端对接聊天 API:我用 React 做了一个比 ChatGPT 还好看的界面
前端·后端·面试
devilnumber5 小时前
Java 内部迭代 vs 外部迭代:从原理到实战避坑
java·开发语言