使用 Lighthouse 制定性能预算

Katie Hempenius
Katie Hempenius

Lighthouse 现在支持性能预算。这项功能称为 LightWallet,可在 5 分钟内设置完毕,并提供有关效果指标以及网页资源大小和数量的反馈。

安装 Lighthouse

LightWallet 适用于命令行版本的 Lighthouse v5+。

首先,安装 Lighthouse:

npm install -g lighthouse

创建预算

创建一个名为 budget.json 的文件。在此文件中,添加以下 JSON:

[
  {
    "path": "/*",
    "timings": [
      {
        "metric": "interactive",
        "budget": 3000
      },
      {
        "metric": "first-meaningful-paint",
        "budget": 1000
      }
    ],
    "resourceSizes": [
      {
        "resourceType": "script",
        "budget": 125
      },
      {
        "resourceType": "total",
        "budget": 300
      }
    ],
    "resourceCounts": [
      {
        "resourceType": "third-party",
        "budget": 10
      }
    ]
  }
]

此示例 budget.json 文件设置了五个单独的预算:

  • Time to Interactive 的预算为 3000 毫秒。
  • 网页上 JavaScript 总量的预算为 125 KB。
  • 网页总大小的预算为 300 KB。
  • 向第三方源发出的请求数量预算为 10 个。

如需查看受支持的效果指标和资源类型的完整列表,请参阅 Lighthouse 文档的性能预算部分。

运行 Lighthouse

使用 --budget-path 标志运行 Lighthouse。此标志会告知 Lighthouse 预算文件的位置。

lighthouse https://example.com --budget-path=./budget.json

查看结果

如果 LightWallet 配置正确,Lighthouse 报告将在性能类别中包含预算部分。

Lighthouse 报告的“预算”部分

在 Lighthouse 报告的 JSON 版本中,Lightwallet 结果可在 performance-budget 审核的审核结果中找到。