Skip to content

go tool pprof - 性能分析工具

go tool pprof 是 Go 官方提供的性能分析工具,用于分析 CPU、内存等性能问题。

📋 概述

难度级别:⭐⭐⭐⭐
考察范围:性能优化/性能分析
技术标签性能分析 CPU分析 内存分析

问题分析

go tool pprof 是 Go 性能优化的核心工具,掌握其用法对于定位和解决性能问题至关重要。

🎯 核心功能

1. 基本用法

bash
# CPU 分析
go tool pprof cpu.prof

# 内存分析
go tool pprof mem.prof

# HTTP 方式分析
go tool pprof http://localhost:6060/debug/pprof/profile

# 查看帮助
go tool pprof -h

2. 生成分析数据

go
package main

import (
    "os"
    "runtime/pprof"
)

func main() {
    // CPU 分析
    f, _ := os.Create("cpu.prof")
    defer f.Close()
    pprof.StartCPUProfile(f)
    defer pprof.StopCPUProfile()
    
    // 你的代码
    doWork()
}

📝 详细示例

示例 1:CPU 分析

go
package main

import (
    "os"
    "runtime/pprof"
    "time"
)

func cpuIntensive() {
    for i := 0; i < 1000000; i++ {
        _ = i * i
    }
}

func main() {
    f, _ := os.Create("cpu.prof")
    defer f.Close()
    
    pprof.StartCPUProfile(f)
    defer pprof.StopCPUProfile()
    
    for i := 0; i < 10; i++ {
        cpuIntensive()
        time.Sleep(100 * time.Millisecond)
    }
}

运行分析:

bash
go run main.go
go tool pprof cpu.prof

# 在 pprof 交互界面
(pprof) top
(pprof) list cpuIntensive
(pprof) web

示例 2:内存分析

go
package main

import (
    "os"
    "runtime/pprof"
)

func memoryIntensive() {
    data := make([]byte, 1024*1024*100)  // 100MB
    _ = data
}

func main() {
    f, _ := os.Create("mem.prof")
    defer f.Close()
    
    memoryIntensive()
    
    pprof.WriteHeapProfile(f)
}

运行分析:

bash
go run main.go
go tool pprof mem.prof

# 在 pprof 交互界面
(pprof) top
(pprof) list memoryIntensive
(pprof) web

示例 3:HTTP 方式分析

go
package main

import (
    _ "net/http/pprof"
    "net/http"
)

func main() {
    go func() {
        http.ListenAndServe("localhost:6060", nil)
    }()
    
    // 你的应用代码
    runApp()
}

访问分析:

bash
# CPU 分析(30秒)
go tool pprof http://localhost:6060/debug/pprof/profile?seconds=30

# 内存分析
go tool pprof http://localhost:6060/debug/pprof/heap

# goroutine 分析
go tool pprof http://localhost:6060/debug/pprof/goroutine

🔧 高级用法

1. 交互式命令

bash
go tool pprof cpu.prof

# 常用命令
(pprof) top          # 显示 top 函数
(pprof) top10        # 显示 top 10
(pprof) list func    # 显示函数代码
(pprof) web          # 生成 SVG 图
(pprof) png          # 生成 PNG 图
(pprof) svg          # 生成 SVG 图
(pprof) help         # 显示帮助

2. 命令行模式

bash
# 直接生成报告
go tool pprof -top cpu.prof
go tool pprof -top10 cpu.prof
go tool pprof -list=funcName cpu.prof
go tool pprof -web cpu.prof

3. 对比分析

bash
# 对比两个 profile
go tool pprof -base=old.prof new.prof

🎯 最佳实践

1. 性能测试集成

go
func BenchmarkFunction(b *testing.B) {
    f, _ := os.Create("bench.prof")
    defer f.Close()
    
    pprof.StartCPUProfile(f)
    defer pprof.StopCPUProfile()
    
    b.ResetTimer()
    for i := 0; i < b.N; i++ {
        Function()
    }
}

2. 生产环境分析

go
// 在生产环境中启用 pprof
import _ "net/http/pprof"

func main() {
    // 只在开发环境启用
    if os.Getenv("ENV") == "dev" {
        go func() {
            log.Println(http.ListenAndServe("localhost:6060", nil))
        }()
    }
    
    // 应用代码
}

📊 分析类型

类型端点说明
CPU/debug/pprof/profileCPU 使用分析
内存/debug/pprof/heap堆内存分析
Goroutine/debug/pprof/goroutineGoroutine 分析
Block/debug/pprof/block阻塞分析
Mutex/debug/pprof/mutex互斥锁分析

🔍 常见问题

Q1: 如何分析内存泄漏?

bash
# 生成两个时间点的 heap profile
go tool pprof http://localhost:6060/debug/pprof/heap
# 等待一段时间
go tool pprof http://localhost:6060/debug/pprof/heap

# 对比分析
go tool pprof -base=heap1.prof heap2.prof

Q2: 如何分析 goroutine 泄漏?

bash
go tool pprof http://localhost:6060/debug/pprof/goroutine
(pprof) top
(pprof) list functionName

Q3: 如何生成火焰图?

bash
# 安装 go-torch
go install github.com/uber/go-torch

# 生成火焰图
go-torch -u http://localhost:6060 -t 30

📖 参考资源

正在精进