在 CI 流水线中自动化 HAR 对比
⚡ HAR Guard CI:直接在 CI 流程里捕获并比较 HAR 文件,及时发现 Web 应用的性能回归。用可复现的数据校验让每次部署都更可靠。
npm install --save-dev har-guard-ci
- 为每个分支提供确定性的性能闸门。
- Markdown 或 JSON 报告,适合 CI 日志和 PR 评论。
- 可直接嵌入现有 E2E 套件的 npm 脚本。
原生 CI 性能护栏
将 HAR Guard 直接接入流水线,在代码进入生产前标记出性能回归。
每次部署都能快速反馈
按需捕获最新 HAR,并在几分钟内与可信基线进行对比。
无缝适配现有工具链
用 Playwright 录制流量,在 Node 中运行对比,并把精简的 Markdown 报告发布到 PR。
常用命令
capture
使用 Playwright 为任何公开或需要认证的 URL 录制 HAR,并将归档写入磁盘。
npx har-guard capture https://example.com artifacts/example.harcompare
检查两个 HAR 文件,当时间或负载阈值被突破时使命令失败。
npx har-guard compare har/baseline.har har/candidate.har --reporter markdown安装
将该包与端到端工具一起安装。只有在需要捕获新的 HAR 文件时才需要 Playwright。
npm install --save-dev har-guard-cinpm install --save-dev playwright仅在使用 capture 命令时需要 Playwright。
捕获 HAR
安装 Playwright 并运行 capture 命令,传入需要测量的 URL 和输出的 .har 文件路径。该命令会启动 Chromium,等待页面加载完成,然后将 HAR 文件写入指定路径。
npx har-guard capture https://example.com artifacts/example.har将生成的 HAR 文件提交到仓库,便于以后将变更与之对比。
对比 HAR 文件
使用 compare 命令来检测基线 HAR 与候选 HAR 之间的回归。如果超过阈值或出现新的请求,命令会以非零状态码退出,从而让 CI 任务失败。
npx har-guard compare har/baseline.har har/candidate.har --reporter markdown在 CI 中运行
将 HAR Guard 接入 GitHub Actions 工作流(或任何可运行 Node 的 CI),在合并前校验每个 Pull Request。
name: HAR Guard
on:
pull_request:
jobs:
compare:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci
- name: Compare HAR files
run: npx har-guard compare har/baseline.har har/candidate.har --reporter markdown如果发现回归,该步骤会以状态码 1 结束并让工作流失败,从而守住性能预算。
探索示例仓库
完整的示例仓库——包含基线与候选 HAR 文件、npm 脚本以及可复用的 GitHub Actions 工作流——位于 examples/demo-repo。可将其作为接入 HAR Guard 的参考。