PlotDirector/PlotLine.Tests/Browser/relationship-main-performance.cjs

244 lines
8.6 KiB
JavaScript

// Compare with captured pre-enhancement fixtures. One catalogue read per page, no per-node queries.
const { chromium } = require("playwright"),
fs = require("fs"),
path = require("path"),
assert = require("assert/strict");
(async () => {
const browser = await chromium.launch();
try {
const context = await browser.newContext({
viewport: { width: 1920, height: 1080 },
});
await context.route("https://cdn.jsdelivr.net/**", (r) =>
r.fulfill({
contentType: "application/javascript",
body: fs.readFileSync(
path.join(
__dirname,
"node_modules",
r.request().url().includes("cytoscape")
? "cytoscape/dist/cytoscape.min.js"
: r.request().url().includes("chart.umd")
? "chart.js/dist/chart.umd.js"
: "@microsoft/signalr/dist/browser/signalr.min.js",
),
),
}),
);
await context.route("**/CharacterAttributes/Directory?*", (r) =>
r.fulfill({ json: { canEdit: true, characters: [] } }),
);
const page = await context.newPage(),
out = "/tmp/pd-main-relationships-after",
results = [];
await page.addInitScript(() => {
window.mainFrames = [];
let last;
function tick(t) {
if (last) mainFrames.push(t - last);
last = t;
requestAnimationFrame(tick);
}
requestAnimationFrame(tick);
});
const navigationOnly = process.argv.includes("--navigation-only");
for (const count of navigationOnly ? [200] : [20, 75, 100, 200])
for (const network of navigationOnly
? ["dense"]
: ["sparse", "clusters", "dense", "isolated"]) {
await page.goto("http://127.0.0.1:5238/fixture?page=calendar-test");
await page.evaluate(() => localStorage.clear());
const started = Date.now();
await page.goto(
`http://127.0.0.1:5238/fixture?page=relationships&cast=${count}&network=${network}`,
);
await page.waitForFunction(() => {
const m = window.PlotDirectorRelationshipWorkspace?.mainCommands;
return m?.initialised && !m.busy && !m.routing;
});
const loadMs = Date.now() - started;
const membershipStarted = Date.now();
await page.evaluate(() =>
window.PlotDirectorRelationshipWorkspace.mainCommands.setPreset(
"full",
),
);
const membershipMs = Date.now() - membershipStarted;
await page.evaluate(() =>
window.PlotDirectorRelationshipWorkspace.mainCommands.arrange(
"clusters",
),
);
await page.waitForFunction(
() => !window.PlotDirectorRelationshipWorkspace.mainCommands.routing,
);
const result = await page.evaluate(() => {
const w = window.PlotDirectorRelationshipWorkspace,
cy = w.cy,
ns = cy.nodes(":visible"),
box = ns.boundingBox();
let overlaps = 0;
for (let i = 0; i < ns.length; i++)
for (let j = i + 1; j < ns.length; j++) {
const a = ns[i].boundingBox(),
b = ns[j].boundingBox();
if (a.x1 < b.x2 && a.x2 > b.x1 && a.y1 < b.y2 && a.y2 > b.y1)
overlaps++;
}
return {
nodes: ns.length,
edges: cy.edges(":visible").length,
overlaps,
layoutMs: w.lastLayout.durationMs,
routingMs: w.mainRoutingStats.durationMs,
communities: w.lastLayout.result.communities?.length,
aspect: box.w / box.h,
zoom: cy.zoom(),
smallestRenderedWidth: Math.min(
...ns.map((n) => n.renderedWidth()),
),
frameP95: mainFrames.sort((a, b) => a - b)[
Math.floor(mainFrames.length * 0.95)
],
};
});
assert.equal(
result.overlaps,
0,
count + "/" + network + " actual box overlaps",
);
if (navigationOnly) {
const positionsBefore = await page.evaluate(() => {
const g = document.getElementById("rwApp");
window.navigationFrames = [];
for (const name of ["wheel", "pointermove"])
g.addEventListener(
name,
() => {
const t = performance.now();
requestAnimationFrame(() =>
requestAnimationFrame(() =>
navigationFrames.push(performance.now() - t),
),
);
},
{ capture: true },
);
return JSON.stringify(
window.PlotDirectorRelationshipWorkspace.cy
.nodes()
.map((n) => [n.id(), n.position()]),
);
});
let retrievals = 0;
const countReads = (r) => {
if (
/\/(RelationshipMap|CharacterAttributes)\//.test(
new URL(r.url()).pathname,
)
)
retrievals++;
};
page.on("request", countReads);
const box = await page.locator("#relationshipMapGraph").boundingBox();
await page.mouse.move(
box.x + box.width * 0.5,
box.y + box.height * 0.5,
);
const started = Date.now();
await page.mouse.wheel(0, -80);
await page.evaluate(
() =>
new Promise((r) =>
requestAnimationFrame(() => requestAnimationFrame(r)),
),
);
const beforePan = await page.evaluate(() =>
window.PlotDirectorRelationshipWorkspace.cy.pan(),
);
await page.mouse.down({ button: "middle" });
await page.mouse.move(
box.x + box.width * 0.5 + 55,
box.y + box.height * 0.5 + 40,
{ steps: 10 },
);
await page.mouse.up({ button: "middle" });
await page.evaluate(
() =>
new Promise((r) =>
requestAnimationFrame(() => requestAnimationFrame(r)),
),
);
const response = await page.evaluate(() => ({
pan: window.PlotDirectorRelationshipWorkspace.cy.pan(),
positions: JSON.stringify(
window.PlotDirectorRelationshipWorkspace.cy
.nodes()
.map((n) => [n.id(), n.position()]),
),
frames: navigationFrames,
}));
assert.equal(response.positions, positionsBefore);
assert(
Math.abs(response.pan.x - beforePan.x - 55) < 1 &&
Math.abs(response.pan.y - beforePan.y - 40) < 1,
);
assert.equal(retrievals, 0);
result.navigation = {
totalNativeSequenceMs: Date.now() - started,
twoFrameResponseMaxMs: Math.max(...response.frames),
twoFrameResponseAverageMs:
response.frames.reduce((a, b) => a + b, 0) /
response.frames.length,
samples: response.frames.length,
retrievals,
nodePositionsUnchanged: true,
};
assert(
result.navigation.twoFrameResponseMaxMs < 500,
"200-character dense diagram remains navigable",
);
page.off("request", countReads);
}
results.push({ count, network, loadMs, membershipMs, ...result });
if (
!navigationOnly &&
((count === 75 && network === "clusters") ||
(count === 200 && network === "dense") ||
(count === 75 && network === "sparse"))
) {
await page.screenshot({
path: `${out}/main-${count}-${network}.png`,
});
await page.evaluate(() =>
window.PlotDirectorRelationshipWorkspace.mainCommands.fit(),
);
await page.screenshot({
path: `${out}/main-${count}-${network}-overview.png`,
});
}
console.log(
count,
network,
loadMs,
Math.round(result.layoutMs),
Math.round(result.routingMs),
result.overlaps,
);
}
fs.writeFileSync(
out +
(navigationOnly ? "/navigation-performance.json" : "/performance.json"),
JSON.stringify(results, null, 2),
);
console.log(
"PASS actual rendered full-cast20/75/100/200: sparse, connected subgroups, dense and isolated; measured layout/routing/frame intervals, zero card overlaps",
);
} finally {
await browser.close();
}
})().catch((e) => {
console.error(e.stack);
process.exitCode = 1;
});