Repository Public
1// Budgets de performance du webmail sur le build de production : boîte de 10 000 messages semée par JMAP (aucun envoi), cinq mesures2// par indicateur, médiane comparée aux budgets de tests/e2e/perf-budgets.json (mesure de référence + 25 %, voir docs/next/PERFORMANCE.md).3// Résultats écrits dans <OUT>/perf.json. SPMAIL_PERF_RECORD=1 : mesurer sans juger (pour établir ou réviser les budgets).4import { expect, test, type BrowserContext, type Page } from "@playwright/test"5import fs from "node:fs"6import path from "node:path"7import { OUT, readState, writeState } from "./env"8import { addAuthenticator, exportCredential, purgeLeftoverMailboxes, track } from "./helpers"910const st = readState()11const BIG = `web-perf1-${st.run}@stg.spboucher.ai`12const SMALL = `web-perf2-${st.run}@stg.spboucher.ai`13const TOTAL = Number(process.env.E2E_BULK ?? 10000)14const RUNS = 515const BUDGETS = JSON.parse(fs.readFileSync(path.join(import.meta.dirname, "perf-budgets.json"), "utf8")) as Record<Metric, number>16type Metric = "cold_load_ms" | "mailbox_switch_ms" | "thread_open_ms" | "search_ms" | "worst_scroll_frame_ms" | "heap_after_scroll_mb"17const results: Partial<Record<Metric, number[]>> = {}18const median = (xs: number[]) => [...xs].sort((a, b) => a - b)[Math.floor(xs.length / 2)]19const record = (m: Metric, v: number) => void (results[m] ??= []).push(Math.round(v * 10) / 10)2021let context: BrowserContext22let page: Page23let bigIndex = 024let smallIndex = 12526async function api<T>(p: Page, method: string, url: string, body?: unknown): Promise<T> {27 return p.evaluate(28 async ({ method, url, body }) => {29 const r = await fetch(url, { method, headers: { "x-spmail": "1", ...(body === undefined ? {} : { "content-type": "application/json" }) }, body: body === undefined ? undefined : JSON.stringify(body) })30 if (!r.ok) throw new Error(`${method} ${url} → ${r.status} ${await r.text()}`)31 return r.json()32 },33 { method, url, body },34 ) as Promise<T>35}36const rows = (p: Page) => p.locator("#message-list [role=option]")37const big = (p: Page) => p.goto(`/m/${bigIndex}/inbox`)3839test.describe.configure({ mode: "serial" })4041test.beforeAll(async ({ browser }) => {42 test.setTimeout(600_000)43 const saved = readState().storage44 context = await browser.newContext({ viewport: { width: 1440, height: 900 }, locale: "fr-CA", timezoneId: "America/Toronto", ...(saved ? { storageState: JSON.parse(saved) } : {}) })45 page = await context.newPage()46 if (saved) await page.goto("/")47 else {48 // lancé seul : premier enrôlement de l'installation49 const authn = await addAuthenticator(context, page)50 await page.goto(st.enrollUrl)51 await page.getByLabel("Nom affiché").fill("Propriétaire Essai")52 await page.getByLabel("Identifiant").fill("owner")53 await page.getByRole("button", { name: "Créer la clé d'accès" }).click()54 await expect(page.locator("#sidebar")).toBeAttached()55 await exportCredential(authn)56 }57 await expect(page.locator("#sidebar")).toBeAttached()58 writeState({ storage: JSON.stringify(await context.storageState()) })59 await purgeLeftoverMailboxes(page)60 track(BIG, SMALL)61 for (const [address, name] of [[BIG, "Perf Grande"], [SMALL, "Perf Petite"]]) await api(page, "POST", "/api/admin/mailboxes", { address, display_name: name })62 // semis : TOTAL messages lus dans la boîte de réception de la grande boîte, 30 dans la petite (Email/set : aucun envoi SMTP)63 const seeded = await page.evaluate(64 async ({ boxes }) => {65 const me = await (await fetch("/api/me")).json()66 let n = 067 for (const [addr, total] of boxes as [string, number][]) {68 const mb = me.mailboxes.find((m: { address: string }) => m.address === addr).id69 const session = await (await fetch(`/api/jmap/${mb}/session`)).json()70 const accountId = session.primaryAccounts["urn:ietf:params:jmap:mail"]71 const call = async (methodCalls: unknown[]) => (await (await fetch(`/api/jmap/${mb}/api`, { method: "POST", headers: { "content-type": "application/json", "x-spmail": "1" }, body: JSON.stringify({ using: ["urn:ietf:params:jmap:core", "urn:ietf:params:jmap:mail"], methodCalls }) })).json()).methodResponses72 const inbox = ((await call([["Mailbox/get", { accountId, properties: ["role"] }, "m"]]))[0][1].list as { id: string; role: string }[]).find((b) => b.role === "inbox")!.id73 for (let start = 0; start < total; start += 400) {74 const create: Record<string, unknown> = {}75 for (let i = start; i < Math.min(total, start + 400); i++)76 create[`e${i}`] = { mailboxIds: { [inbox]: true }, keywords: { $seen: true }, from: [{ name: `Expéditeur ${i % 37}`, email: `exp${i % 37}@example.org` }], to: [{ email: addr }], subject: `Message ${String(i).padStart(5, "0")}${i % 500 === 42 ? " repère" : ""}`, receivedAt: new Date(Date.UTC(2026, 0, 1) + i * 60_000).toISOString().replace(".000Z", "Z"), bodyValues: { t: { value: `Corps du message ${i}. `.repeat(8) } }, textBody: [{ partId: "t", type: "text/plain" }] }77 const r = await call([["Email/set", { accountId, create }, "c"]])78 n += Object.keys(r[0][1].created ?? {}).length79 }80 }81 return n82 },83 { boxes: [[BIG, TOTAL], [SMALL, 30]] },84 )85 expect(seeded).toBe(TOTAL + 30)86 const me = await api<{ mailboxes: { address: string }[] }>(page, "GET", "/api/me")87 bigIndex = me.mailboxes.findIndex((m) => m.address === BIG)88 smallIndex = me.mailboxes.findIndex((m) => m.address === SMALL)89 // l'index de recherche du serveur de courrier a fini son travail quand un terme du dernier lot est trouvé90 await expect91 .poll(async () => ((await api<{ data: unknown[] }>(page, "GET", `/api/next/search?q=${encodeURIComponent(`subject:"Message ${String(TOTAL - 1).padStart(5, "0")}"`)}&mailbox_id=all`).catch(() => ({ data: [] }))).data ?? []).length, { timeout: 180_000, intervals: [2000] })92 .toBeGreaterThan(0)93})94test.afterAll(async () => {95 fs.writeFileSync(`${OUT}/perf.json`, JSON.stringify({ at: new Date().toISOString(), total: TOTAL, runs: RUNS, results, medians: Object.fromEntries(Object.entries(results).map(([k, v]) => [k, median(v as number[])])), budgets: BUDGETS }, null, 1))96 const me = await api<{ mailboxes: { id: string; address: string }[] }>(page, "GET", "/api/me").catch(() => ({ mailboxes: [] }))97 for (const m of me.mailboxes) if (m.address === BIG || m.address === SMALL) await api(page, "DELETE", `/api/admin/mailboxes/${m.id}?purge=delete-all-mail`).catch(() => {})98 await context?.close()99})100/** Juge la médiane ; SPMAIL_PERF_RECORD=1 mesure sans juger. */101function judge(m: Metric) {102 const med = median(results[m]!)103 test.info().annotations.push({ type: m, description: `médiane ${med} (mesures ${results[m]!.join(", ")}) · budget ${BUDGETS[m]}` })104 if (process.env.SPMAIL_PERF_RECORD !== "1") expect(med, `${m} : médiane de ${RUNS} mesures (${results[m]!.join(", ")}) au-dessus du budget`).toBeLessThanOrEqual(BUDGETS[m])105}106107test("perf : chargement à froid jusqu'à une liste utilisable (10 000 messages)", async ({ browser }) => {108 test.setTimeout(180_000)109 for (let i = 0; i < RUNS; i++) {110 // « à froid » : nouveau contexte (aucun cache HTTP, aucun service worker, aucune copie locale), même session111 const ctx = await browser.newContext({ viewport: { width: 1440, height: 900 }, locale: "fr-CA", timezoneId: "America/Toronto", storageState: await context.storageState(), serviceWorkers: "block" })112 const p = await ctx.newPage()113 const t0 = Date.now()114 await p.goto(`/m/${bigIndex}/inbox`)115 await rows(p).filter({ hasText: `Message ${String(TOTAL - 1).padStart(5, "0")}` }).waitFor()116 await p.locator("#message-list").focus()117 record("cold_load_ms", Date.now() - t0)118 await ctx.close()119 }120 judge("cold_load_ms")121})122123test("perf : changement de boîte", async () => {124 for (let i = 0; i < RUNS; i++) {125 await page.goto(`/m/${smallIndex}/inbox`)126 await rows(page).filter({ hasText: "Message 00029" }).waitFor()127 const t0 = Date.now()128 await page.locator("#sidebar").getByRole("link", { name: BIG }).click()129 await rows(page).filter({ hasText: `Message ${String(TOTAL - 1).padStart(5, "0")}` }).waitFor()130 record("mailbox_switch_ms", Date.now() - t0)131 }132 judge("mailbox_switch_ms")133})134135test("perf : ouverture d'un fil", async () => {136 await big(page)137 for (let i = 0; i < RUNS; i++) {138 const subject = `Message ${String(TOTAL - 2 - i).padStart(5, "0")}`139 const line = rows(page).filter({ hasText: subject })140 await line.waitFor()141 const t0 = Date.now()142 await line.click()143 await expect(page.locator(".reader-subject")).toContainText(subject)144 await expect(page.locator(".reader-body")).toContainText(`Corps du message ${TOTAL - 2 - i}.`)145 record("thread_open_ms", Date.now() - t0)146 await page.keyboard.press("u")147 }148 judge("thread_open_ms")149})150151test("perf : recherche, aller-retour complet", async () => {152 await big(page)153 await rows(page).first().waitFor()154 for (let i = 0; i < RUNS; i++) {155 const n = String(1042 + i * 500).padStart(5, "0")156 const input = page.locator("#search-input")157 await input.click()158 await input.fill(`"Message ${n}"`)159 const t0 = Date.now()160 await input.press("Enter")161 await page.locator("#list-pane [role=option]").filter({ hasText: `Message ${n} repère` }).first().waitFor()162 record("search_ms", Date.now() - t0)163 await big(page)164 await rows(page).first().waitFor()165 }166 judge("search_ms")167})168169test("perf : défilement de toute la liste — pire image, puis mémoire JS", async () => {170 test.setTimeout(300_000)171 const cdp = await context.newCDPSession(page)172 await cdp.send("Performance.enable")173 for (let i = 0; i < RUNS; i++) {174 await big(page)175 await rows(page).first().waitFor()176 // pire image : 120 pas de 400 px, une image par pas177 const worst = await page.evaluate(async () => {178 const el = document.querySelector(".mlist") as HTMLElement179 let worst = 0180 let last = performance.now()181 for (let i = 0; i < 120; i++) {182 el.scrollTop += 400183 await new Promise((r) => requestAnimationFrame(r))184 const now = performance.now()185 worst = Math.max(worst, now - last)186 last = now187 }188 return worst189 })190 record("worst_scroll_frame_ms", worst)191 // toute la liste, par bonds d'un vingtième, en laissant chaque fenêtre arriver192 await page.evaluate(async () => {193 const el = document.querySelector(".mlist") as HTMLElement194 for (let k = 1; k <= 20; k++) {195 el.scrollTop = (el.scrollHeight * k) / 20196 await new Promise((r) => setTimeout(r, 250))197 }198 })199 await rows(page).filter({ hasText: "Message 00000" }).waitFor()200 await cdp.send("HeapProfiler.collectGarbage")201 const { metrics } = await cdp.send("Performance.getMetrics")202 record("heap_after_scroll_mb", (metrics.find((m) => m.name === "JSHeapUsedSize")?.value ?? 0) / 1048576)203 }204 judge("worst_scroll_frame_ms")205 judge("heap_after_scroll_mb")206})207