使用 Lighthouse-CI 断言 TTI < 3.5 s 并输出 diff。
解读
在国内一线互联网公司的面试中,面试官不仅考察候选人能否“跑通”Lighthouse-CI,更关注如何把性能卡点写进自动化流水线、如何给出可量化的 diff 报告、如何在失败时快速定位是代码回退还是网络劣化。
本题表面是“跑个断言”,实质是“把性能预算(Performance Budget)做成 CI 门禁”,并给出可审计、可追踪、可回滚的 diff。
面试官常追问:
- 如果 TTI 连续 3 次抖动在 3.4 s~3.6 s 之间,你怎样防止误杀?
- 国内网络环境(CDN 节点、灰度城市、弱网)如何排除干扰?
- 如果 diff 超过 10 % 但小于 20 %,你让流水线通过还是人工二次确认?
回答时必须体现阈值策略、统计置信、环境隔离、报告可视化四要素,否则会被判为“只玩过 demo”。
知识点
- Lighthouse-CI 架构:lhci 命令行、lhci-server(可选)、lhci-rc 配置三件套;与 Selenium 的关系是“Selenium 负责登陆、埋 cookie、走业务路径,Lighthouse-CI 负责采样”。
- TTI(Time to Interactive)定义:主线程静默 5 s 且网络请求 idle,国内面试常让区分 TTI、FID、LCP,务必背准指标含义。
- Performance Budget 断言语法:
“budgets” 数组里对 “interactive” 字段设 “<3500” 毫秒;lhci 支持 “warn” 与 “error” 双阈值,必须给出 error 阈值 3500,warn 阈值 3000 体现梯度。 - Diff 输出方式:
– 本地跑:lhci autorun 自动生成.lighthouseci/目录,里面有lhr-*.json,用lhci upload推到自建 lhci-server 即可得到 diff 面板;
– 无服务器场景:用lhci assert --preset=lighthouse:no-pwa结合jq命令行解析lhr.categories.performance.auditRefs['interactive'].numericValue,二次封装成 markdown 表格打印在 PR 评论。 - 环境一致性:
– 国内镜像源:chrome 下载路径配@puppeteer/browsers的PUPPETEER_DOWNLOAD_HOST=https://npmmirror.com/mirrors;
– 禁用缓存、固定 CPU 4x、网速用throttling配 4G 40/40 RTT 150 ms,与淘宝、京东线上真实 75 分位对齐。 - Selenium 与 Lighthouse-CI 的衔接:
Selenium 完成“登陆→跳转到待测 SPA→把 localStorage 打上lhci=skipWelcome标记→把 cookies 序列化到/tmp/cookies.json”,Lighthouse-CI 通过chrome-launcher复用同一用户目录,保证登录态一致。 - 统计防抖:
连续 3 次采样取 90 分位而非平均值,防止一次长尾误杀;代码层面用lhci collect --numberOfRuns=3即可。 - 门禁策略:
在 GitLab-CI 或 GitHub Actions 里,只有 “lhci-assert” job 成功才允许 merge;若断言失败,自动@owner并贴出 diff 截图,把 “TTI 3.7 s → 3.2 s” 高亮标绿,“3.2 s → 3.9 s” 标红。
答案
- 项目根目录新建
lighthouserc.js(CommonJS 写法,国内兼容性最好):
module.exports = {
ci: {
collect: {
startServerCommand: 'npm run start:prod', // 先起本地 prod 包
startServerReadyPattern: 'Listening on', // 防止端口未好就采样
url: ['http://localhost:3000/'],
numberOfRuns: 3,
settings: {
preset: 'desktop', // 也可设 mobile
throttling: {
rttMs: 150,
throughputKbps: 40 * 1024,
cpuSlowdownMultiplier: 1
},
chromeFlags: '--disable-storage-reset --disable-dev-shm-usage --no-sandbox'
}
},
assert: {
assertions: {
'interactive': ['error', { maxNumericValue: 3500 }],
'first-contentful-paint': ['warn', { maxNumericValue: 1800 }]
}
},
upload: {
target: 'temporary-public-storage' // 无服务器方案,7 天有效期
}
}
};
- GitHub Actions 片段(国内镜像加速 + Node 16):
jobs:
lhci:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: 使用国内镜像安装 Chrome
run: |
sudo apt update
sudo apt install -y wget
wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
sudo sh -c 'echo "deb [arch=amd64] https://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list'
sudo apt update
sudo apt install -y google-chrome-stable
- uses: actions/setup-node@v3
with:
node-version: 16
cache: 'npm'
registry-url: https://registry.npmmirror.com
- run: npm ci
- name: 运行 Lighthouse-CI
run: npx @lhci/cli@0.12.x autorun
env:
LHCI_GITHUB_APP_TOKEN: ${{ secrets.LHCI_GITHUB_APP_TOKEN }}
- 输出 diff:
若断言失败,控制台会打印:
✘ interactive failure for maxNumericValue
Expected: <= 3500
Found: 3820
URL: http://localhost:3000/
在 PR 评论中通过 lhci upload 返回的 https://storage.googleapis.com/lighthouse-infrastructure.appspot.com/reports/xxx... 链接,打开后左侧为基线,右侧为本次,TTI 差值 +320 ms 标红,实现“diff 可视化”。
- 与 Selenium 打通(可选加分):
在同一条流水线里先跑 Selenium 脚本,把登录态落到磁盘:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://example.com/login')
# 省略输入账号密码
with open('/tmp/cookies.json', 'w') as f:
f.write(str(driver.get_cookies()))
driver.quit()
然后在 lighthouserc.js 的 collect.settings.extraHeaders 里注入 Cookie,保证 Lighthouse 采样的是已登录的深链页面,避免跳到登录页导致 TTI 虚高。
拓展思考
- 把 TTI 预算写进前端代码:用
lighthouse-plugin-performance-budget把阈值落到package.json,MR 变更 budget 文件需 Tech-Lead 二次 approve,防止开发随手放宽。 - 弱网补偿策略:国内 4G 信号波动大,可在 CI 里跑两条流水线——一条 throttling 4G,一条 throttling 3G,只有 4G 失败才 block,3G 失败仅 warn,兼顾用户体验与交付效率。
- 与 Selenium Grid 分布式结合:把 Lighthouse-CI 作为 Grid 的一个 Node,复用同一 Chrome 实例,先跑 30 条 UI 回归,再在同一会话里切到性能采样,节省 40 % 容器资源。
- 失败自动回滚:若 TTI 连续两次突破 4.0 s,触发 GitLab-CI 的
rollbackjob,自动回滚上一次镜像并 @on-call,把性能退化当成 P1 故障处理。