SPB Git

Repository Public

perf-k.mjs 7.9 KB 87 lines javascript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : Note SPBoucher AI8 * File             : tests/e2e/next/perf-k.mjs9 * Module           : tests10 * Repository       : https://git.spboucher.ai/note-spboucher-ai11 * Project Website  : https://note.spboucher.ai12 * Created          : 2026-11-1913 * Last Modified    : 2026-11-1914 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: contact@spboucher.ai23 * Development      : dev@spboucher.ai24 * Support          : support@spboucher.ai25 * Security/Admin   : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License          : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243// file: tests/e2e/next/perf-k.mjs44// description: measures the Knowledge Graph 2.0 (and the Thought Constellation) on the synthetic workspace of45//   scripts/next-k-perf-seed.ts — time to first paint of the graph, duration and frame rate of the layout phase, then frame times46//   while zooming with the wheel and panning with a drag (requestAnimationFrame intervals read in the page while Playwright drives47//   the mouse). Prints one JSON object. Numbers are real measurements of this run; nothing is estimated.48// author: Simon-Pierre Boucher — contact@spboucher.ai49// usage: E2E_BASE_URL=http://localhost:8487 node tests/e2e/next/perf-k.mjs <sessionTokenFile> [--headed]50import { chromium } from "@playwright/test"; import fs from "node:fs";51const base = process.env.E2E_BASE_URL ?? "http://localhost:8487"; const token = fs.readFileSync(process.argv[2], "utf8").trim();52// --gpu: the full Chromium binary in its new headless mode with the Metal backend (the default headless shell rasterises on the CPU)53const gpu = process.argv.includes("--gpu");54const b = await chromium.launch({ headless: !process.argv.includes("--headed"), ...(gpu ? { channel: "chromium", args: ["--use-angle=metal", "--enable-gpu-rasterization", "--ignore-gpu-blocklist", "--enable-zero-copy"] } : {}) });55const ctx = await b.newContext({ baseURL: base, viewport: { width: 1440, height: 900 }, deviceScaleFactor: 2, extraHTTPHeaders: { cookie: `__Host-note_session=${token}` } }); const p = await ctx.newPage();56const stats = (a) => { if (!a.length) return null; const s = [...a].sort((x, y) => x - y); const q = (f) => s[Math.min(s.length - 1, Math.floor(f * s.length))]; const mean = a.reduce((x, y) => x + y, 0) / a.length; return { frames: a.length, meanMs: +mean.toFixed(1), p50Ms: +q(0.5).toFixed(1), p95Ms: +q(0.95).toFixed(1), maxMs: +s[s.length - 1].toFixed(1), fps: +(1000 / mean).toFixed(1) }; };57const sample = async (ms, drive) => { await p.evaluate(() => { const w = window; w.__raf = []; let last = performance.now(); w.__rafOn = true; const tick = (t) => { w.__raf.push(t - last); last = t; if (w.__rafOn) requestAnimationFrame(tick); }; requestAnimationFrame(tick); }); const t0 = Date.now(); while (Date.now() - t0 < ms) await drive(Date.now() - t0); return stats(await p.evaluate(() => { window.__rafOn = false; return window.__raf.slice(1); })); };58const out = { at: new Date().toISOString(), viewport: "1440x900@2x", browser: `Chromium ${b.version()} ${process.argv.includes("--headed") ? "headed" : gpu ? "headless (full binary, Metal)" : "headless shell (CPU raster)"}` };59// ---- graph60await p.addInitScript(() => { window.__kgPerf = []; });61const t0 = Date.now(); await p.goto(process.argv.includes("--legacy") ? "/graph?renderer=force" : "/graph"); const canvas = p.getByTestId("graph-canvas"); await canvas.waitFor({ timeout: 120_000 });62out.graph = { renderer: (await canvas.getAttribute("data-renderer")) ?? "react-force-graph-2d", nodes: Number(await canvas.getAttribute("data-nodes")), edges: Number(await canvas.getAttribute("data-edges")), lod: await canvas.getAttribute("data-lod"), dataAndFirstCanvasMs: Date.now() - t0 };63await p.waitForFunction(() => window.__kgPerf.length > 0, null, { timeout: 120_000 }); out.graph.firstFrameMs = Date.now() - t0;64await p.waitForFunction(() => Boolean(window.__kgSettled), null, { timeout: 180_000 }); out.graph.layoutSettledMs = Date.now() - t0;65const layoutFrames = await p.evaluate(() => { const a = window.__kgPerf; const d = []; for (let i = 1; i < a.length; i++) d.push(a[i] - a[i - 1]); return d; }); out.graph.layoutPhase = stats(layoutFrames);66const bb = await canvas.boundingBox(); const cx = bb.x + bb.width / 2, cy = bb.y + bb.height / 2; await p.mouse.move(cx, cy); await p.waitForTimeout(500);67out.graph.wheelZoom = await sample(5000, async (t) => { await p.mouse.wheel(0, Math.floor(t / 1250) % 2 === 0 ? -40 : 40); await p.waitForTimeout(12); });68await p.mouse.move(cx, cy); await p.mouse.down();69out.graph.dragPan = await sample(5000, async (t) => { const a = t / 400; await p.mouse.move(cx + Math.cos(a) * 220, cy + Math.sin(a) * 140); await p.waitForTimeout(8); });70await p.mouse.up();71// zoomed in (labels on): pan again72for (let i = 0; i < 14; i++) { await p.mouse.move(cx, cy); await p.mouse.wheel(0, -120); await p.waitForTimeout(30); } await p.mouse.down();73out.graph.dragPanZoomedIn = await sample(4000, async (t) => { const a = t / 400; await p.mouse.move(cx + Math.cos(a) * 200, cy + Math.sin(a) * 120); await p.waitForTimeout(8); });74await p.mouse.up();75// selecting a node from the list (inspector + highlighted edges drawing themselves)76await p.getByTestId("open-insights").click().catch(() => {}); const tSel = Date.now(); await p.locator("[data-testid=insight-central] button").nth(1).click({ timeout: 60_000 }).catch(() => {}); await p.getByTestId("node-inspector").waitFor({ timeout: 60_000 }).then(() => { out.graph.selectCentralPageMs = Date.now() - tSel; }).catch(() => { out.graph.selectCentralPageMs = null; });77// ---- constellation78const t1 = Date.now(); const r = await p.request.get("/api/graph/constellation"); const c = await r.json(); out.constellation = { apiMs: Date.now() - t1, cached: c.cached, points: c.points?.length ?? 0, links: c.links?.length ?? 0, clusters: c.clusters?.length ?? 0, explained: c.explained };79const t2 = Date.now(); await p.goto("/constellation"); const sky = p.locator("[data-testid=constellation-canvas]");80if (await sky.waitFor({ timeout: 120_000 }).then(() => true).catch(() => false)) {81  out.constellation.firstCanvasMs = Date.now() - t2; await p.waitForTimeout(2500); const sb = await sky.boundingBox(); const sx = sb.x + sb.width / 2, sy = sb.y + sb.height / 2; await p.mouse.move(sx, sy);82  out.constellation.wheelZoom = await sample(5000, async (t) => { await p.mouse.wheel(0, Math.floor(t / 1250) % 2 === 0 ? -60 : 60); await p.waitForTimeout(12); });83  await p.mouse.down(); out.constellation.dragPan = await sample(4000, async (t) => { const a = t / 400; await p.mouse.move(sx + Math.cos(a) * 220, sy + Math.sin(a) * 140); await p.waitForTimeout(8); }); await p.mouse.up();84}85out.gl = await p.evaluate(() => { try { const c = document.createElement("canvas"); const g = c.getContext("webgl"); const e = g.getExtension("WEBGL_debug_renderer_info"); return e ? g.getParameter(e.UNMASKED_RENDERER_WEBGL) : "webgl"; } catch { return null; } });86console.log(JSON.stringify(out, null, 1)); await b.close();87
Files main
Menu

Theme