Back to MCP directory
publicPublicdnsLocal runtime

pprof-analyzer-mcp

一个基于Go的MCP服务器,提供分析Go pprof性能分析文件的工具,支持多种分析类型和输出格式。

article

README

🚀 简体中文版PProf使用指南

本指南详细介绍了PProf工具的使用方法,包括服务端配置、客户端使用、文件分析、火焰图生成等内容,帮助你更好地进行性能分析。

🚀 快速开始

📦 安装指南

在使用PProf进行性能分析前,你需要安装相关工具。以下是安装命令:

go install github.com/google/pprof/cmd/llhttp@latest
go install github.com/google/pprof/pprof/...

✨ 主要特性

  • 支持服务端配置,以Docker为例可快速搭建运行环境。
  • 客户端支持MCP协议,可通过Go语言进行配置。
  • 能对本地和远程的prof文件进行分析。
  • 可生成火焰图直观展示性能数据。
  • 支持与CI/CD pipeline集成,如Jenkins。

💻 使用示例

服务端配置(以Docker为例)

# 以下是一个典型的Docker运行时配置示例:
docker run -d --name my-golang-service \
  -p 8080:8080 \
  -v /my/app/path:/app \
  --entrypoint "/app/pprof_server.sh" \
  golang:latest

客户端使用说明 - 配置MCP协议支持(以Go语言为例)

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)
    }
}

分析prof文件

分析本地prof文件
# 下载并安装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
使用MCP协议进行分析(HTTP方式)
{
    "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文件分析示例
# 下载在线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

使用MCP协议进行性能分析(Go语言客户端)

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()

    // 处理响应内容(可自定义处理逻辑)
    // 例如:解析性能数据并进行分析
}

🔧 技术细节

配置参考

HTTP服务配置示例
# 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

工具链集成

CI/CD pipeline集成(Jenkins示例)
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' // 生成文本格式性能报告
            }
        }
    }
}

📚 详细文档

常见问题排查

网络连接问题
  • 检查防火墙设置,确保端口8082开放。
  • 使用netstat -tuln | grep 8082命令确认服务是否在监听该端口。
性能数据不全
  • 确保所有必要的性能收集中间件已正确配置。
  • 检查日志输出,确保没有错误信息。
安装问题
  • 确保Go语言环境正确配置,版本不低于1.20。
  • 使用go version命令确认当前Go版本。

参考资料

  1. Golang官方文档
  2. PProf工具文档
  3. 性能分析最佳实践

附录

常用命令
# 查看当前运行进程的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服务监听地址
额外资源
help

Runtime guide

cloud

Hosted runtime

Hosted servers run from a provider-managed environment. You usually connect the MCP client to the hosted endpoint or follow the provider's authorization flow, without keeping a local process alive

  1. Open provider connection page
  2. Authorize or copy endpoint
  3. Connect from your MCP client
terminal

Local runtime / other methods

Local servers run on your own machine or infrastructure. You normally copy the server_config into your MCP client, install the required package, and provide env variables from env_schema when needed

  1. Copy server_config
  2. Install required package
  3. Fill env variables and restart client