Files
doona/apps/lite/scripts/perf-switch.cjs
Zakk e74e32f5cc feat(lite): add doona lite, a simple config editor
apps/lite is a separate Vite app (Astryx, React 19, StyleX) on the same
honk API, for people who want to switch nodes and edit groups,
subscriptions, nodes, routing and DNS without the observability pages.
Every change goes into a draft and one review dialog shows it in plain
words before it is written; dae syntax stays in the text editor. It has
a demo mode backed by an in-browser mock, browser, motion and
performance checks in apps/lite/scripts, and UI rules and working notes
in apps/lite/docs. It is linted with the repository config.
2026-10-26 22:05:40 +11:00

101 lines
4.3 KiB
JavaScript

const {chromium} = require('@playwright/test');
const base = process.env.LITE_PREVIEW_URL;
if (!base) throw new Error('Set LITE_PREVIEW_URL to the preview server.');
const rate = 4;
const routes = [
['subscriptions', '订阅'],
['nodes', '节点'],
['routing', '路由'],
['dns', 'DNS'],
['groups', '策略组']
];
const rows = {groups: '[data-group-row]', subscriptions: '[data-provider-id]', nodes: 'tbody tr', routing: '[data-rule-index]', dns: '[data-upstream]'};
(async () => {
const browser = await chromium.launch({args: ['--disable-features=OverlayScrollbar']});
const results = [];
const failures = [];
try {
const p = await browser.newPage({viewport: {width: 1440, height: 900}, locale: 'zh-CN'});
const cdp = await p.context().newCDPSession(p);
await cdp.send('Emulation.setCPUThrottlingRate', {rate});
await p.addInitScript(() => {
window.__perf = {tasks: [], shifts: []};
new PerformanceObserver(list => window.__perf.tasks.push(...list.getEntries().map(e => ({start: e.startTime, duration: e.duration})))).observe({
type: 'longtask',
buffered: true
});
new PerformanceObserver(list => window.__perf.shifts.push(...list.getEntries().map(e => ({start: e.startTime, value: e.value})))).observe({
type: 'layout-shift',
buffered: true
});
});
async function measure(label, action) {
const start = await p.evaluate(() => performance.now());
await action();
await p.waitForTimeout(2000);
const tasks = await p.evaluate(start => window.__perf.tasks.filter(e => e.start >= start).map(e => e.duration), start);
const max = Math.max(0, ...tasks);
results.push({switch: label, longTasks: tasks.map(n => +n.toFixed(1)), maxMs: +max.toFixed(1)});
if (max > 50) failures.push(`${label}: ${max.toFixed(1)} ms > 50 ms`);
}
await p.goto(base);
await measure('demo → 策略组', async () => {
await p
.getByRole('button', {name: /demo|演示|示範/i})
.first()
.click();
await p.locator('[data-group-row]:visible').first().waitFor();
});
for (const visit of [1, 2]) {
for (const [route, name] of routes) {
await measure(`${name} (${visit})`, async () => {
await p.locator(`a.astryx-side-nav-item[href="#${route}"]`).click();
await p.locator(`${rows[route]}:visible`).first().waitFor();
});
}
}
await p.close();
// A delayed response isolates the collection swap from navigation and boot.
for (const [route] of [['groups'], ...routes.filter(([route]) => route !== 'groups')]) {
const slow = await browser.newPage({viewport: {width: 1440, height: 900}, locale: 'zh-CN', reducedMotion: 'reduce'});
const session = await slow.context().newCDPSession(slow);
await session.send('Emulation.setCPUThrottlingRate', {rate});
await slow.addInitScript(() => {
window.__shifts = [];
new PerformanceObserver(list => window.__shifts.push(...list.getEntries().map(e => ({start: e.startTime, value: e.value})))).observe({
type: 'layout-shift',
buffered: true
});
});
await slow.goto(`${base}?slow=1200`);
await slow
.getByRole('button', {name: /demo|演示|示範/i})
.first()
.click();
if (route !== 'groups') {
await slow.locator('[data-group-row]:visible').first().waitFor();
await slow.evaluate(route => {
location.hash = route;
}, route);
}
const skeleton = slow.locator(`[data-loading-shape="${route}"]:visible`).first();
await skeleton.waitFor();
const start = await slow.evaluate(() => performance.now());
await slow.locator(`${rows[route]}:visible`).first().waitFor();
await skeleton.waitFor({state: 'hidden'});
await slow.waitForTimeout(500);
const cls = await slow.evaluate(start => window.__shifts.filter(e => e.start >= start).reduce((sum, e) => sum + e.value, 0), start);
results.push({switch: `${route} skeleton → content`, cls});
if (cls > 0) failures.push(`${route}: swap CLS ${cls} > 0`);
await slow.close();
}
console.log(JSON.stringify({rate, results, failures}, null, 2));
if (failures.length) process.exitCode = 1;
} finally {
await browser.close();
}
})().catch(error => {
console.error(error);
process.exitCode = 1;
});