All files / lib lighthouseCalibration.ts

100% Statements 13/13
100% Branches 8/8
100% Functions 3/3
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37                                  1x 1x   1x       8x 6x 6x         4x 4x 3x 3x    
/**
 * CPU calibration for Lighthouse, so the performance budgets measure the same simulated phone
 * whatever machine runs them.
 *
 * Lighthouse slows the CPU 4x by default, which assumes the host is a high-end desktop
 * (benchmark index 1500–2000) and lands on a mid-tier phone. Shared CI runners are often
 * "low-end desktop" class (1000–1500), where 4x simulates a much slower phone than intended
 * and Total Blocking Time swings from run to run. Lighthouse's docs say to calibrate the
 * multiplier to the host instead:
 * https://github.com/GoogleChrome/lighthouse/blob/main/docs/throttling.md#cpu-throttling
 *
 * The multiplier here keeps the simulated device at the one Lighthouse's default aims for
 * (index 1750 / 4). It never goes above Lighthouse's 4x, so a fast machine gets the stock
 * setting, and never below 1x.
 */
 
/** Middle of the "high-end desktop" bracket that Lighthouse's 4x default assumes. */
export const REFERENCE_BENCHMARK_INDEX = 1750;
export const DEFAULT_MULTIPLIER = 4;
/** The benchmark index of the device the budgets are meant to simulate. */
export const TARGET_BENCHMARK_INDEX = REFERENCE_BENCHMARK_INDEX / DEFAULT_MULTIPLIER;
 
/** The CPU slowdown to use on a host with this benchmark index, to one decimal place. */
export function cpuSlowdownFor(benchmarkIndex: number): number {
  if (!Number.isFinite(benchmarkIndex) || benchmarkIndex <= 0) return DEFAULT_MULTIPLIER;
  const exact = benchmarkIndex / TARGET_BENCHMARK_INDEX;
  return Math.round(Math.min(DEFAULT_MULTIPLIER, Math.max(1, exact)) * 10) / 10;
}
 
/** Median of a few benchmark readings; a single reading is noisy. */
export function medianOf(values: number[]): number {
  const sorted = values.filter(Number.isFinite).sort((a, b) => a - b);
  if (sorted.length === 0) return Number.NaN;
  const mid = Math.floor(sorted.length / 2);
  return sorted.length % 2 ? sorted[mid]! : (sorted[mid - 1]! + sorted[mid]!) / 2;
}