本指南详细介绍了PProf工具的使用方法,包括服务端配置、客户端使用、文件分析、火焰图生成等内容,帮助你更好地进行性能分析。
在使用PProf进行性能分析前,你需要安装相关工具。以下是安装命令:
go install github.com/google/pprof/cmd/llhttp@latest
go install github.com/google/pprof/pprof/...
# 以下是一个典型的Docker运行时配置示例:
docker run -d --name my-golang-service \
-p 8080:8080 \
-v /my/app/path:/app \
--entrypoint "/app/pprof_server.sh" \
golang:latest
import (
"context"
"fmt"
"log"
"net/http"
"github.com/google/pprof/pkg/collector/collectormiddleware"
"github.com/google/pprof/pkg/collector/goroutine"
"github.com/google/pprof/pkg/collector/memory"
"github.com/google/pprof/pkg/collector/profile"
"github.com/google/pprof/pkg/collector/sched"
"github.com/google/pprof/pkg/collector/threadstats"
"github.com/google/pprof/pkg/frontender"
)
func main() {
ctx := context.Background()
// 配置性能收集中间件
middleware := collectormiddleware.New(
goroutine.NewCollector(),
memory.NewCollector(),
profile.NewCollector(),
sched.NewCollector(),
threadstats.NewCollector(),
)
// 初始化MCP前端服务
frontend, err := frontender.New(frontend.Config{
Middlewares: []frontender.Middleware{middleware},
Log: log.New(os.Stdout, "", log.LstdFlags),
})
if err != nil {
log.Fatal(err)
}
// 定义处理函数
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// 生成性能数据文件
pprof.CreateProfile(ctx, w, r, frontend)
})
// 启动HTTP服务器
server := &http.Server{
Addr: ":8082",
Handler: handler,
}
fmt.Printf("Starting PProf server on :8082\n")
if err := server.ListenAndServe(); err != nil {
log.Fatal(err)
}
}
# 下载并安装PProf工具
go install github.com/google/pprof/cmd/llhttp@latest
go install github.com/google/pprof/pprof/...
命令行分析示例
# 查看goroutine profile
pprof -text ./myapp goroutine
# 生成火焰图
pprof -svg ./myapp cpu > my_flame_graph.svg
# 分析内存分配情况
pprof -http ./myapp heap
{
"tool_name": "analyze_pprof",
"arguments": {
"profile_uri": "http://localhost:8082/debug/pprof/profile?uid=123456&type=cpu",
"profile_type": "cpu",
"top_n": 5,
"output_format": "text"
}
}
# 下载在线prof文件
curl -o my_profile.cpu https://example.com/pprof/cpu
# 分析下载的prof文件
pprof -http ./myapp my_profile.cpu
# 使用PProf工具直接生成SVG格式火焰图
pprof -svg ./myapp cpu > my_flame_graph.svg
# 或使用以下命令:
go tool pprof -svg ./myapp cpu > my_flame_graph.svg
# 启动PProf HTTP服务
go tool pprof ./myapp
# 访问地址:http://localhost:8082/debug/pprof
import (
"context"
"log"
"net/http"
"github.com/google/pprof/pkg/frontender"
)
func main() {
ctx := context.Background()
// 初始化MCP前端服务
frontend, err := frontender.New(frontend.Config{
Middlewares: []frontender.Middleware{
// 添加需要的性能收集中间件
goroutine.NewCollector(),
memory.NewCollector(),
},
Log: log.New(os.Stdout, "", log.LstdFlags),
})
if err != nil {
log.Fatal(err)
}
// 创建HTTP客户端
client := &http.Client{}
// 发送性能数据收集请求
resp, err := client.Get("http://localhost:8082/debug/pprof/profile")
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
// 处理响应内容(可自定义处理逻辑)
// 例如:解析性能数据并进行分析
}
# pprof_server.sh 示例脚本内容:
#!/bin/bash
set -eo pipefail
APP_PATH="/app/myapp"
CPU_PROFILE=/my/app/path/profiles/cpu.pprof
MEM_PROFILE=/my/app/path/profiles/mem.pprof
cd ${APP_PATH}
exec "$@"
# 设置性能分析相关环境变量
export GODEBUG="gcflags='-http2 profiling'"
export PPprof Addr=0.0.0.0:8082
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'go build -o myapp .' // 编译程序
}
}
stage('Profile') {
steps {
sh 'go install github.com/google/pprof/cmd/llhttp@latest' // 安装PProf工具
sh 'go test -bench . -run=' // 执行基准测试
sh 'go tool pprof -text ./myapp cpu > profiling_results.txt' // 生成文本格式性能报告
}
}
}
}
netstat -tuln | grep 8082命令确认服务是否在监听该端口。go version命令确认当前Go版本。# 查看当前运行进程的goroutine情况
go tool pprof -text ./myapp goroutine
# 分析内存分配情况
go tool pprof -http ./myapp mem
# 生成火焰图(CPU profiling)
go tool pprof -svg ./myapp cpu > my_flame_graph.svg
# 查看帮助信息
pprof --help
export GOGC=off # 关闭Go的垃圾回收机制
export GODEBUG="gcflags='-http2 profiling'" # 启用HTTP/2性能分析支持
export PPprof.Addr=0.0.0.0:8082 # 设置PProf服务监听地址