52 lines
2.2 KiB
PowerShell
52 lines
2.2 KiB
PowerShell
<#
|
|
.SYNOPSIS
|
|
Measures the Largest Contentful Paint (and layout shift) of a page with Lighthouse, mobile profile, a few runs, and
|
|
prints the median. Use it before and after the hero markers are in a website, on the TEST hosts.
|
|
|
|
.DESCRIPTION
|
|
Runs `npx lighthouse` (performance category only, mobile emulation with the default throttling) -Runs times against
|
|
-Url and reports the LCP element, the LCP time, CLS and total blocking time per run and the median LCP. Needs Node
|
|
(npx) and Chrome or Edge. Nothing is written to the repository; the JSON reports go to -OutDirectory (default: a
|
|
temporary folder).
|
|
|
|
.PARAMETER Url
|
|
The page, e.g. https://test.andreaslop.nl/
|
|
|
|
.PARAMETER Runs
|
|
Number of runs (default 3; the median is what counts, a single run varies).
|
|
|
|
.PARAMETER OutDirectory
|
|
Where the Lighthouse JSON reports are kept.
|
|
|
|
.EXAMPLE
|
|
./scripts/Measure-Lcp.ps1 -Url https://test.andreaslop.nl/ -Runs 5
|
|
#>
|
|
[CmdletBinding()]
|
|
param(
|
|
[Parameter(Mandatory)] [string] $Url,
|
|
[int] $Runs = 3,
|
|
[string] $OutDirectory = (Join-Path ([IO.Path]::GetTempPath()) 'lcp-measure')
|
|
)
|
|
|
|
$ErrorActionPreference = 'Stop'
|
|
New-Item -ItemType Directory -Force -Path $OutDirectory | Out-Null
|
|
$results = @()
|
|
for ($i = 1; $i -le $Runs; $i++) {
|
|
$file = Join-Path $OutDirectory ("lh-{0}-{1}.json" -f ([uri]$Url).Host, $i)
|
|
& npx --yes lighthouse@12 $Url --only-categories=performance --form-factor=mobile --output=json --output-path=$file `
|
|
--chrome-flags="--headless=new --no-sandbox" --quiet | Out-Null
|
|
$report = Get-Content -LiteralPath $file -Raw | ConvertFrom-Json
|
|
$audits = $report.audits
|
|
$element = $audits.'largest-contentful-paint-element'.details.items[0].items[0].node.snippet
|
|
$results += [pscustomobject]@{
|
|
Run = $i
|
|
LcpMs = [math]::Round($audits.'largest-contentful-paint'.numericValue)
|
|
Cls = [math]::Round($audits.'cumulative-layout-shift'.numericValue, 3)
|
|
TbtMs = [math]::Round($audits.'total-blocking-time'.numericValue)
|
|
Element = $element
|
|
}
|
|
}
|
|
$results | Format-Table -AutoSize
|
|
$sorted = $results.LcpMs | Sort-Object
|
|
Write-Host ("Median LCP: {0} ms (reports in {1})" -f $sorted[[int][math]::Floor(($sorted.Count - 1) / 2)], $OutDirectory)
|