diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ec83f2fb..4eae2207 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -113,10 +113,38 @@ jobs: - name: Smoke test run: sh scripts/smoke-image.sh kitsune - perf: - name: Perf + perf-server: + name: Perf (server) runs-on: ubuntu-latest - timeout-minutes: 45 + timeout-minutes: 20 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version: ${{ env.GO_VERSION }} + check-latest: true + - name: Server + run: | + printf '### Server, ms\n\n| Interaction | median | p95 | Budget (p95) |\n|---|--:|--:|--:|\n' >> "$GITHUB_STEP_SUMMARY" + go test -count=1 -run 'Speed$' -v ./internal/server + + # The browser suite, one library and one shard of its files a runner (web/perf/shards.ts splits + # them by time), each with one worker as before: the runners share nothing, so nothing timed + # competes for a CPU. Each sets up only its own library. + perf-browser: + name: Perf (${{ matrix.library }}, ${{ matrix.shard }}) + runs-on: ubuntu-latest + timeout-minutes: 25 + strategy: + fail-fast: false + matrix: + library: [ 1k, 50k ] + shard: [ 1/2, 2/2 ] + env: + KITSUNE_PERF_LIBRARIES: ${{ matrix.library }} + KITSUNE_PERF_SHARD: ${{ matrix.shard }} steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: @@ -141,14 +169,50 @@ jobs: echo "$RUNNER_TEMP/ffmpeg/bin" >> "$GITHUB_PATH" fi cd web && bun install --frozen-lockfile && bunx playwright install chromium - - name: Server - run: | - printf '### Server, ms\n\n| Interaction | median | p95 | Budget (p95) |\n|---|--:|--:|--:|\n' >> "$GITHUB_STEP_SUMMARY" - go test -count=1 -run 'Speed$' -v ./internal/server - name: Build - if: ${{ !cancelled() }} run: sh scripts/build.sh "$KITSUNE_PERF_DIR/kitsune-server" - name: Browser - if: ${{ !cancelled() }} working-directory: web run: bunx playwright test -c perf/playwright.config.ts + # The samples, for the one table `perf` writes, whatever this shard's tests said. + - name: Samples + if: ${{ !cancelled() }} + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: perf-samples-${{ strategy.job-index }} + path: ${{ env.KITSUNE_PERF_DIR }}/results/*.json + if-no-files-found: ignore + retention-days: 7 + + # Every shard's samples in one table, in this job's summary, and the one result for the run: it + # fails when the server's tests or any shard did. + perf: + name: Perf + needs: [ perf-server, perf-browser ] + if: ${{ !cancelled() }} + runs-on: ubuntu-latest + timeout-minutes: 10 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2.2.0 + with: + bun-version: ${{ env.BUN_VERSION }} + - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + pattern: perf-samples-* + merge-multiple: true + path: ${{ runner.temp }}/kitsune-perf/results + - name: Table + env: + KITSUNE_PERF_DIR: ${{ runner.temp }}/kitsune-perf + run: bun web/perf/summary.ts + - name: Result + if: ${{ always() }} + env: + SERVER: ${{ needs.perf-server.result }} + BROWSER: ${{ needs.perf-browser.result }} + run: | + echo "server: $SERVER, browser: $BROWSER" + test "$SERVER" = success && test "$BROWSER" = success diff --git a/AGENTS.md b/AGENTS.md index 5552d213..d24ba10e 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -854,14 +854,16 @@ and written through [audiometa](https://github.com/simonhull/audiometa). 50,000 songs after a fingerprint landed, a ReplayGain run of 1,000 real FLACs, per song, and `GET /admin/replaygain` over 50,000 songs with 2,000 to write. The browser suite also holds that a song plays on while its file moves. Each fails past its budget, - and CI's `perf` job runs both on every PR, with the table in the job summary. Budgets are about 1.5x what was measured when + and CI runs both on every PR: the speed tests in their own job, the browser suite as four shards (each + library in two, the files split by time in `web/perf/shards.ts`, one worker each), and the `perf` job + gathers every shard's samples into one table in its summary and fails when any of them did. Budgets are about 1.5x what was measured when they were set; on CI they are multiplied by `ciMultiplier`, which comes from CI's own numbers. - `mise run perf` runs everything (`scripts/perf.sh`; Playwright arguments pass through). It writes only under `$KITSUNE_PERF_DIR` (default `kitsune-perf` in the temp directory) and reuses the libraries there. Each server starts on a fresh database, and nothing is timed until its first scan and the fingerprint and loudness passes after it are done: the suite measures a server at rest. Only the loud albums lack ReplayGain, so the loudness pass has little to do. `KITSUNE_PERF_CHROMIUM` picks a local Chromium, `KITSUNE_PERF_LIBRARIES=1k` measures - one size, `KITSUNE_PERF_VERBOSE=1` prints every sample, and `KITSUNE_PERF_PORT` moves the servers off 14610, for a second worktree measuring at once. The speed tests skip under `-race`. + one size, `KITSUNE_PERF_SHARD=1/2` runs one shard of the files, `KITSUNE_PERF_VERBOSE=1` prints every sample, and `KITSUNE_PERF_PORT` moves the servers off 14610, for a second worktree measuring at once. The speed tests skip under `-race`. - Nothing new may wait on the play path, from a click to the `playing` event. A change on that path, or to a budget, carries the suite's numbers before and after, and no change is called faster without them. diff --git a/web/perf/playwright.config.ts b/web/perf/playwright.config.ts index 9133ce02..3a9a2e8e 100644 --- a/web/perf/playwright.config.ts +++ b/web/perf/playwright.config.ts @@ -1,14 +1,16 @@ import { defineConfig } from '@playwright/test'; import { join } from 'node:path'; import { libraries, perfDir } from './servers.ts'; +import { shard, shardFiles } from './shards.ts'; /** * The perf suite: one project per library size, one worker, so nothing else competes with what's - * timed. `KITSUNE_PERF_CHROMIUM` runs a Chromium of your own instead of Playwright's download. + * timed. `KITSUNE_PERF_CHROMIUM` runs a Chromium of your own instead of Playwright's download, and + * `KITSUNE_PERF_SHARD` runs one shard of the files (`shards.ts`). */ export default defineConfig({ testDir: '.', - testMatch: '*.perf.ts', + testMatch: shard === undefined ? '*.perf.ts' : shardFiles(shard), outputDir: join(perfDir, 'test-results'), globalSetup: './setup.ts', workers: 1, diff --git a/web/perf/report.ts b/web/perf/report.ts index 8be108b9..2ff47921 100644 --- a/web/perf/report.ts +++ b/web/perf/report.ts @@ -1,11 +1,14 @@ /** * Turns the samples the tests wrote into one table: median and p95 per interaction and library, * against its budget. It goes to the console, to `results/table.md`, and on CI to the job summary. + * A shard's table holds only what it measured and stays out of the summary: CI gathers every + * shard's samples and writes the whole table once (`summary.ts`). */ import { appendFileSync, existsSync, readdirSync, readFileSync, writeFileSync } from 'node:fs'; import { join } from 'node:path'; import { budgets, multiplier, percentile } from './budgets.ts'; import { libraries, resultsDir } from './servers.ts'; +import { shard } from './shards.ts'; /** One interaction measured on one library. */ export interface Result { @@ -43,6 +46,9 @@ export function writeReport(): void { `|---|${libraries.map(() => '--:|--:').join('|')}|--:|` ]; for (const [id, budget] of Object.entries(budgets.browser)) { + if (shard !== undefined && !libraries.some((l) => results.has(`${l.name}--${id}`))) { + continue; // Another shard's. + } const cells = libraries.map((library) => { const samples = results.get(`${library.name}--${id}`)?.samples ?? []; const p95 = percentile(samples, 95); @@ -52,11 +58,13 @@ export function writeReport(): void { lines.push(`| ${budget.interaction} | ${cells.join(' | ')} | ${String(budget.p95Ms)} ms |`); } const scale = multiplier === 1 ? '' : ` Budgets are multiplied by ${String(multiplier)} here.`; - const table = `### Browser, ms (20 runs each)\n\n${lines.join('\n')}\n\n${scale}\n`; + const part = + shard === undefined ? '' : `, shard ${String(shard.index)} of ${String(shard.total)}`; + const table = `### Browser, ms (20 runs each${part})\n\n${lines.join('\n')}\n\n${scale}\n`; writeFileSync(join(resultsDir, 'table.md'), table); console.log(`\n${table}`); const summary = process.env['GITHUB_STEP_SUMMARY']; - if (summary) { + if (summary && shard === undefined) { appendFileSync(summary, `${table}\n`); } } diff --git a/web/perf/shards.ts b/web/perf/shards.ts new file mode 100644 index 00000000..7f469d9b --- /dev/null +++ b/web/perf/shards.ts @@ -0,0 +1,79 @@ +/** + * Splits the suite's files into shards of about equal time, so CI can run it on several runners at + * once, each still with one worker. Playwright's own `--shard` splits by test count, and these tests + * run from 3 s to 2 minutes, so its shards came out two to four times apart. + * + * `KITSUNE_PERF_SHARD=i/n` runs shard `i` of `n`: every shard sorts the files by `seconds`, longest + * first, and gives each to the shard with the least so far, so they all agree on the split and every + * file runs in exactly one. A file missing from `seconds` counts as `unknownSeconds` and still runs. + */ +import { readdirSync } from 'node:fs'; +import { fileURLToPath } from 'node:url'; + +/** + * About how long each file takes on CI on the 50k library, from a run's log. They only steer the + * split: refresh them when the shards drift apart. + */ +const seconds: Record = { + 'album.perf.ts': 74, + 'artist.perf.ts': 116, + 'columns.perf.ts': 82, + 'continues.perf.ts': 127, + 'cover.perf.ts': 23, + 'filePlaylists.perf.ts': 48, + 'home.perf.ts': 71, + 'library.perf.ts': 120, + 'organize.perf.ts': 100, + 'playlists.perf.ts': 45, + 'resume.perf.ts': 6, + 'search.perf.ts': 4, + 'shuffle.perf.ts': 21, + 'smartPlaylists.perf.ts': 30, + 'trash.perf.ts': 24 +}; +const unknownSeconds = 60; + +/** One shard of the suite: `index` of `total`, from 1. */ +export interface Shard { + index: number; + total: number; +} + +/** The shard `KITSUNE_PERF_SHARD` names, or undefined when the whole suite runs. */ +export const shard: Shard | undefined = parseShard(process.env['KITSUNE_PERF_SHARD']); + +function parseShard(value: string | undefined): Shard | undefined { + if (value === undefined || value === '') { + return undefined; + } + const [index, total] = value.split('/').map(Number); + if ( + index === undefined || + total === undefined || + !Number.isInteger(index) || + !Number.isInteger(total) || + index < 1 || + index > total + ) { + throw new Error(`KITSUNE_PERF_SHARD must be i/n with 1 ≤ i ≤ n, not ${value}`); + } + return { index, total }; +} + +/** The suite's files in `shard`, in name order, as Playwright would run them unsharded. */ +export function shardFiles({ index, total }: Shard): string[] { + const dir = fileURLToPath(new URL('.', import.meta.url)); + const files = readdirSync(dir).filter((name) => name.endsWith('.perf.ts')); + const weight = (file: string) => seconds[file] ?? unknownSeconds; + const sorted = [...files].sort((a, b) => weight(b) - weight(a) || (a < b ? -1 : 1)); + const loads = Array.from({ length: total }, () => 0); + const mine: string[] = []; + for (const file of sorted) { + const least = loads.indexOf(Math.min(...loads)); + loads[least] = (loads[least] ?? 0) + weight(file); + if (least === index - 1) { + mine.push(file); + } + } + return mine.sort(); +} diff --git a/web/perf/smartPlaylists.perf.ts b/web/perf/smartPlaylists.perf.ts index 62530db3..7a305dbd 100644 --- a/web/perf/smartPlaylists.perf.ts +++ b/web/perf/smartPlaylists.perf.ts @@ -1,6 +1,6 @@ import type { Page } from '@playwright/test'; import { expect, runs, test } from './fixture.ts'; -import { atRest } from './pages.ts'; +import { albumTracks, atRest } from './pages.ts'; const writes = { 'X-Kitsune-Request': '1' }; @@ -65,9 +65,31 @@ test('Playlists: open a smart playlist of 500 songs from its card', async ({ pag await remove(page, id); }); +/** + * Rates the first song of the Library's first album 4★, so "Rating is at least" counts more at + * ★★★ than at ★★★★★ whatever else this run has rated: the generated library comes unrated. + */ +async function rateOne(page: Page): Promise { + const listed = await page.request.get('/api/v1/albums?sort=name&limit=1'); + const { albums } = (await listed.json()) as { albums: { id: string }[] }; + const album = albums[0]; + if (album === undefined) { + throw new Error('no album to rate'); + } + const [song] = await albumTracks(page, album.id); + if (song === undefined) { + throw new Error(`no song on ${album.id}`); + } + const rated = await page.request.put(`/api/v1/me/ratings/tracks/${song}`, { + headers: writes, + data: { stars: 4 } + }); + expect(rated.status()).toBe(204); +} + test('Rules editor: press a star, to the count changed', async ({ page, perf }) => { - // The generated library comes rated: "Rating is at least" counts more at ★★★ than at - // ★★★★★, and the server says how many each. + // The server says how many songs each rating chooses. + await rateOne(page); const [atThree, atFive] = [ await counted(page, ratedAtLeast(6)), await counted(page, ratedAtLeast(10)) diff --git a/web/perf/summary.ts b/web/perf/summary.ts new file mode 100644 index 00000000..7dd39b35 --- /dev/null +++ b/web/perf/summary.ts @@ -0,0 +1,7 @@ +/** + * Writes the whole table from samples gathered into `results`, as the suite's teardown does: CI + * runs it once every shard is done, with each shard's samples downloaded there. + */ +import { writeReport } from './report.ts'; + +writeReport();