article
README
🚀 使用简体中文运行 PProf 的 Docker 容器
本项目提供了一个使用 Docker 容器运行 PProf 的解决方案,帮助开发者方便地进行性能剖析和分析。
🚀 快速开始
启动步骤
- 拉取 PProf 镜像
docker pull dockerhub.schaefer.name/pprof:latest-amd64
- 创建配置文件
pprof-config.yaml
address: ":8080"
pprofs:
- name: "Heap profile"
pattern: ".*\.heap"
contentTypes: ["application/octet-stream"]
subProcessProfile: true
type: "heap"
- name: "CPU profile"
pattern: ".*\.cpu"
contentTypes: ["text/plain"]
type: "cpu"
- name: "Goroutine profile"
pattern: ".*\.goroutine"
contentTypes: ["text/plain"]
type: "goroutine"
- 启动 PProf 容器
docker run -d --name pprof -p 8080:8080 -v $(pwd)/pprof-config.yaml:/etc/pprof/config.yaml dockerhub.schaefer.name/pprof:latest-amd64
- 访问 PProf 的 Web 界面
# 在浏览器访问 http://<your-docker-host>:8080 或者 http://localhost:8080 如果在本机运行
📦 安装指南
将项目代码与 PProf 集成
- 在你的 Go 项目中添加以下代码进行性能剖析
import (
"fmt"
"log"
"net/http"
"github.com/google/pprof/agent"
)
func main() {
log.Println("Starting application on port 8081")
http.HandleFunc("/", handler)
http.ListenAndServe(":8081", nil)
}
func handler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/debug/pprof" {
w.Header().Set("Content-Type", "application/octet-stream")
agent.Profile(r.Context(), w, agent.MainArgs{})
return
}
fmt.Fprintf(w, "Hello, World!")
}
- 使用 curl 启动分析
# 分析 CPU 文件
curl -X POST http://localhost:8080/pprof/upload -F "file=@./testdata/gobench.cpu"
# 开启交互式 PProf UI(仅限 macOS)
curl -X POST http://localhost:8080/pprof/debug \
-H "Content-Type: application/json; charset=utf-8" \
-d '{"address":"http://localhost:8081/debug/pprof/"}'
💻 使用示例
基础用法
分析本地 CPU 剖析文件
{
"tool_name": "analyze_pprof",
"arguments": {
"profile_path": "./testdata/gobench.cpu",
"type": "cpu"
}
}
生成火焰图
{
"tool_name": "generate_flamegraph",
"arguments": {
"profile_path": "./testdata/gobench.cpu",
"output_path": "./flame-graph.svg"
}
}
📚 详细文档
发布说明
版本更新日志
v0.1.2 (2023-05-26)
- 修复了火焰图生成中的内存泄漏问题
- 提高了剖析文件解析的效率
- 支持 goroutine 剖析的子进程模式
- 新增了基于时间戳的剖析文件命名规则
已知问题
- 不支持分析非常大的剖析文件(超过 1GB)
- 在处理某些特定类型的剖析数据时可能会出现延迟
使用说明
请确保在运行 PProf 容器之前已经拉取最新的镜像,并正确配置您的应用程序以生成相应的剖析文件。如遇问题,请参考官方文档或联系技术支持。
🚧 未来改进计划
- 支持更多类型的剖析文件
- 提供基于 Web 的交互式分析界面
- 增加对多线程和异步应用的支持
- 优化内存使用效率
- 新增结果导出功能
微信扫一扫