Skip to content

@ogygia/testing

Server profiles

ogygia.profile(path) asks the SSR profiler to render a path several times and returns its report, so a test can hold a page's server render, score and JavaScript weight to a budget.

On this page

The browser-side calls measure what a visitor’s browser saw. ogygia.profile(path) measures the server: it asks the SSR profiler to render the path several times and returns the profiler’s JSON report, with the page score, the median render, the forecast, the findings and, on a build, the page’s weight. The app has to list devtools({ profiler: true }).

import { test, expect } from './fixtures';

test('the product page renders quickly on the server', async ({
  ogygia
}) => {
  const profile = await ogygia.profile('/products/1');

  expect(profile).toMeetBudget({
    score: 80,
    renderMs: 200,
    jsBytes: 150_000
  });
  expect(profile).toHaveNoFindings({ severity: 'error' });
});

Options

profile(path, options?) sends POST /__profiler/page?p=<path>&runs=<runs>&format=json with the fixture’s profilerKey, and needs no page open. path starts with / and may carry a query. Options:

  • runs: the timed renders, 3 by default (the profiler’s own form uses 5). Use 5 or more when you compare numbers across runs.
  • fields: only these top-level sections of the report, plus the ones that name it (schema, version, engine, kind, id, created, target and links). A full report is hundreds of KB, mostly stacks, hot_functions and drill, so a CI check that reads a few sections should ask for them. Keep the sections your checks read: score for the score line, assets for jsBytes, findings for toHaveNoFindings(). renderMs reads target, which is always kept. A line whose section was left out fails as “not measured”.

profile() passes no other profiler option, so weigh, cold, gc and interval keep their defaults (#2310).

A recording renders the path through your real server, follows up to five redirects and profiles the final address. Page profiles explains each step, and Limits what makes one refuse to start. If the profiler answers anything but 200 (a 401 for a wrong key, a 404 when it isn’t listed or has no secret, a 409 while another recording runs, a 503 near the heap limit), profile() throws “the profiler answered for ” with the start of its body. It doesn’t retry a 409, and the server records one thing at a time, so run the tests that profile one after another (one worker, or a serial describe) rather than in parallel.

import { test, expect } from './fixtures';

test('the home page keeps its score', async ({ ogygia }) => {
  const profile = await ogygia.profile('/', {
    runs: 5,
    fields: ['score', 'findings', 'assets', 'summary']
  });
  expect(profile).toMeetBudget({ score: 85, jsBytes: 150_000 });
  expect(profile).toHaveNoFindings({ severity: 'error' });
});

Report

The report is the profiler’s JSON report with kind: 'profile'. The profiler’s own kind, the recording mode, moves to mode. The fields a test reads first are typed:

FieldWhat it holds
idThe report’s id on the profiler.
engine{ version }, the ogygia that produced it (CI profiles). A profiler before engine 3 sends none.
target{ page, runs }: the path profiled, and each timed render in ms.
score{ score, grade, categories, missing }, the page score, or null.
forecast{ now_ms, after_ms }: the median render now and after the suggested fixes, or null.
findingsEach with severity, code, message and fix. Findings lists every code.
assetsOn a build: totals (js at start, wire, lazy_js) and the heaviest files. On the dev server: { missing }, why it wasn’t weighed.
links{ html, json }, the report’s addresses on the profiler.

Every other section of the JSON is there too, untyped.

Budget lines

LinePasses when
scoreThe page score is at least this.
renderMsThe median of target.runs is at most this many ms.
jsBytesThe JavaScript loaded at start (assets.totals.js) is at most this many bytes. A build only: on the dev server it’s not measured, so the line fails.

CI profiles

A few things keep profiles comparable from run to run:

  • One version. A profile names the ogygia that produced it in engine.version, the same number the page reports use. Its detectors change with that version, so compare profiles only within one version. A profiler before engine 3 sends no engine.
  • A build. The dev server’s timings include Vite’s transforms and it doesn’t weigh the page. Profile a build (Builds).
  • Enough runs. The score and render use the median of the runs, so 5 runs a side steadies them. Several runs a side does the same across test runs.
  • Stable weights. The profiler weighs a page’s files within a time budget, and each file it couldn’t weigh says why in assets.missed_why (budget, timeout, status 404, file cap or error). profile() doesn’t pass the profiler’s weigh=full, which weighs every file. When assets.totals must not vary, call the profiler’s route yourself with weigh=full.

The CI reports page turns two runs of the same tests into a PR comment, profiles included. For a check without a test suite, npx ogygia perf judges a list of pages against a budget file (Perf budgets).