在 PR 阶段自动评论性能报告?

解读

Matplotlib 作为被 10 万+ 开源项目直接依赖的“绘图基础设施”,任何性能回退都可能在全球 CI 中引发雪崩式超时。国内大厂(阿里、腾讯、字节)在内部镜像源同步后,同样会第一时间感知性能抖动。因此,PR 阶段必须“秒级”给出可量化的性能报告,让 Reviewer 在代码合并前就能判断:本次改动是否引入渲染瓶颈、内存泄漏或 Agg 后端退化。自动评论性能报告,本质是把“可观测性”左移到贡献者 Fork 阶段,降低 Maintainer 的 Review 成本,同时符合国内开源合规审计“变更可追踪”要求。

知识点

  1. CI 触发钩子:GitHub Actions / Gitee Go / 内部 GitLab-CI 的 pull_request_target 事件,需加 permissions: pull-requests: write 以评论。
  2. 基准框架:airspeed-velocity(asv)是 Matplotlib 官方采用的基准工具,支持多维度参数化 Benchmark;需提前在 benchmarks/ 目录下写好 time_xxx 函数。
  3. 差异计算:asv 的 continuous 命令可对比 HEAD^ 与 PR 分支,输出 ratio 与 confidence;需设定阈值(默认 1.2× 视为回归)。
  4. 评论模板:采用 asv publish 生成的 JSON + Jinja2 渲染,输出 Markdown 表格(此处用文字描述:Benchmark 名称、旧耗时、新耗时、差异百分比、置信区间、状态图标)。
  5. 资源隔离:国内云厂商(华为云 KooCI、阿里云云效)的容器需固定 CPU 型号、关闭超线程、绑定 NUMA,避免多租户噪声。
  6. 缓存策略:ccache + matplotlib.test.cache 复用字体与 Agg 缓存,减少 30% 冷启动时间。
  7. 安全合规:PR 来自 Fork 时,secrets 不可直接注入;需通过 workflow_run 二次触发,防止泄漏 PyPI token。
  8. 失败降级:基准运行超时(>30 min)或置信区间不足时,自动贴上“性能结果待定”标签,提醒 Maintainer 手工拉取分支复测。

答案

给出一套可直接落地的 GitHub Actions 方案,兼顾国内同步镜像场景:

  1. 仓库目录
    benchmarks/ ├── init.py └── bench_agg.py # 含 time_draw_path(sizes=[100, 1_000, 10_000])
    .github/ └── workflows/ └── perf-comment.yml

  2. perf-comment.yml 核心片段

    name: PR Performance Report
    on:
      pull_request_target:
        types: [opened, synchronize]
    jobs:
      perf:
        runs-on: ubuntu-22.04
        permissions:
          pull-requests: write
        steps:
          - uses: actions/checkout@v4
            with:
              fetch-depth: 0
          - uses: actions/setup-python@v4
            with:
              python-version: "3.11"
          - name: Install asv & deps
            run: |
              pip install asv virtualenv
              sudo apt-get update -y
              sudo apt-get install -y libfreetype6-dev libpng-dev pkg-config
          - name: Run asv continuous
            run: |
              asv continuous --factor 1.2 --split HEAD^ HEAD \
                --bench bench_agg \
                --environment conda:3.11 \
                --record-samples \
                --output json > results.json
          - name: Render comment
            id: render
            run: |
              python .github/scripts/render_asv_comment.py results.json > comment.md
          - name: Post comment
            uses: actions/github-script@v6
            with:
              script: |
                const fs = require('fs');
                const body = fs.readFileSync('comment.md','utf8');
                github.rest.issues.createComment({
                  issue_number: context.issue.number,
                  owner: context.repo.owner,
                  repo: context.repo.repo,
                  body: body
                });
    
  3. render_asv_comment.py 逻辑
    读取 results.json,过滤置信区间≥95% 且差异≥20% 的条目;若全部通过,输出“✅ 未发现显著性能回退”;否则逐条列出 Benchmark 名称、旧时间、新时间、差异比例、置信区间,并 @PR 作者。

  4. 国内加速
    在华为云 KooCI 自建 Runner,镜像源配置 https://repo.huaweicloud.com/pypi/simple,asv 环境模板预装 numpy-1.26+mkl wheel,整体提速 40%。

  5. Review 流程
    Maintainer 看到评论后,若出现 🔴 回归,直接贴 asv profile 火焰图命令,要求贡献者在本地 py-spy 复现;连续两次回归即触发“合并阻塞”标签,符合国内代码评审红线制度。

拓展思考

  1. GPU 后端拓展:Matplotlib 正在实验 mpl-gpu 分支,使用 Vulkan 渲染。CI 需新增 runs-on: [self-hosted, gpu],性能报告应区分 CPU-Agg vs GPU 渲染帧率;国内可用阿里云 eGPU 实例 gn6v,按需启动节省 70% 成本。
  2. 内存回归检测:asv 默认只测时间,可自定义 mem_draw_path 基准,使用 tracemalloc 峰值;当 PR 引入 ≥5% 内存回退 时,自动触发 memory-profiler 逐行报告,方便定位缓存未释放。
  3. 交互式小部件性能:针对 Jupyter Widget 场景,引入 ui-perf 基准,通过 playwright headless 录制 <canvas> 帧率;评论里嵌入 Lighthouse 性能得分,让前端开发者也能看懂。
  4. 回归自动 Bisect:若评论被标记为“性能回归”,机器人自动创建 bisect/job/123 分支,使用 asv run ALL --skip-existing-commits,二分定位首次引入回退的 commit,输出 git bisect log 并 @作者,实现“无人值守”根因分析。