Skip to content

Commit 75d6471

Browse files
authored
perf(overlay): push deltas, pause idle pages and window long panel lists (#392)
Refs #342.
1 parent ad3855b commit 75d6471

41 files changed

Lines changed: 4033 additions & 177 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import { TestBed, type ComponentFixture } from '@angular/core/testing';
2+
import type { DevframeRpcClient } from 'devframe/client';
3+
import { describe, expect, it } from 'vitest';
4+
import { ComponentTree } from '../pages/component-tree';
5+
6+
// A tree of 2,000 instances renders only the rows near view, and stays
7+
// navigable by keyboard and described to screen readers.
8+
9+
const COUNT = 2000;
10+
11+
// jsdom has no CSS namespace; the page escapes ids with it.
12+
(globalThis as { CSS?: { escape(value: string): string } }).CSS ??= {
13+
escape: (value: string) => value,
14+
};
15+
16+
function bigTree() {
17+
const items = Array.from({ length: COUNT - 1 }, (_, i) => ({
18+
id: `c${i}`,
19+
name: `Item${i}`,
20+
tag: 'app-item',
21+
children: [],
22+
}));
23+
return [{ id: 'app', name: 'App', tag: 'app-root', children: items }];
24+
}
25+
26+
function client(roots: unknown[]): DevframeRpcClient {
27+
const state: Record<string, unknown> = {
28+
'component-tree': {
29+
pages: {
30+
a: { pageId: 'a', title: 'a', url: '/', roots, count: COUNT, detail: null, reportedAt: 1 },
31+
},
32+
},
33+
};
34+
const rpc = {
35+
call: () => Promise.resolve([]),
36+
callEvent: () => Promise.resolve(),
37+
sharedState: (name: string) =>
38+
Promise.resolve({ value: () => state[name] ?? null, on: () => () => {} }),
39+
};
40+
return { connectionMeta: {}, scope: () => ({ rpc }) } as unknown as DevframeRpcClient;
41+
}
42+
43+
async function settle(fixture: ComponentFixture<unknown>) {
44+
for (let i = 0; i < 4; i++) {
45+
await new Promise((resolve) => setTimeout(resolve));
46+
await fixture.whenStable();
47+
}
48+
}
49+
50+
async function setup(roots: unknown[]) {
51+
const fixture = TestBed.createComponent(ComponentTree);
52+
fixture.componentRef.setInput('rpc', client(roots));
53+
document.body.appendChild(fixture.nativeElement);
54+
await settle(fixture);
55+
return fixture;
56+
}
57+
58+
const rows = (fixture: ComponentFixture<ComponentTree>) =>
59+
Array.from((fixture.nativeElement as HTMLElement).querySelectorAll<HTMLElement>('.row'));
60+
61+
describe('ComponentTree with a long tree', () => {
62+
it('renders the rows near view and space for the rest', async () => {
63+
const fixture = await setup(bigTree());
64+
const shown = rows(fixture);
65+
expect(shown.length).toBeGreaterThan(10);
66+
expect(shown.length).toBeLessThan(200);
67+
const tree = (fixture.nativeElement as HTMLElement).querySelector<HTMLElement>('.tree')!;
68+
expect(tree.classList).toContain('windowed');
69+
expect(parseFloat(tree.style.paddingBottom)).toBeGreaterThan(30_000);
70+
expect((fixture.nativeElement as HTMLElement).textContent).toContain(
71+
'Rows render as you scroll',
72+
);
73+
fixture.destroy();
74+
});
75+
76+
it('tells screen readers where each row sits among its siblings', async () => {
77+
const fixture = await setup(bigTree());
78+
const item = rows(fixture).find((row) => row.dataset['id'] === 'c4')!;
79+
expect(item.getAttribute('aria-level')).toBe('2');
80+
expect(item.getAttribute('aria-posinset')).toBe('5');
81+
expect(item.getAttribute('aria-setsize')).toBe(String(COUNT - 1));
82+
// One Tab stop, on a rendered row.
83+
expect(rows(fixture).filter((row) => row.tabIndex === 0)).toHaveLength(1);
84+
fixture.destroy();
85+
});
86+
87+
it('moves to the last row with End, rendering it first', async () => {
88+
const fixture = await setup(bigTree());
89+
const first = rows(fixture)[0]!;
90+
first.focus();
91+
first.dispatchEvent(
92+
new KeyboardEvent('keydown', { key: 'End', bubbles: true, cancelable: true }),
93+
);
94+
await settle(fixture);
95+
const last = rows(fixture).find((row) => row.dataset['id'] === `c${COUNT - 2}`);
96+
expect(last).toBeTruthy();
97+
expect(document.activeElement).toBe(last);
98+
expect(last!.tabIndex).toBe(0);
99+
expect(rows(fixture).length).toBeLessThan(200);
100+
fixture.destroy();
101+
});
102+
103+
it('renders a short tree whole', async () => {
104+
const fixture = await setup([
105+
{
106+
id: 'app',
107+
name: 'App',
108+
tag: 'app-root',
109+
children: [{ id: 'x', name: 'X', tag: 'app-x', children: [] }],
110+
},
111+
]);
112+
expect(rows(fixture)).toHaveLength(2);
113+
const tree = (fixture.nativeElement as HTMLElement).querySelector<HTMLElement>('.tree')!;
114+
expect(tree.classList).not.toContain('windowed');
115+
fixture.destroy();
116+
});
117+
});
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { TestBed } from '@angular/core/testing';
2+
import { afterEach, describe, expect, it } from 'vitest';
3+
import { NetworkInspector } from '../pages/network-inspector';
4+
5+
// Long lists render only the rows near view: the HTTP call table here; the
6+
// component tree has its own test.
7+
8+
const COUNT = 2000;
9+
10+
const call = (i: number) => ({
11+
id: `c${i}`,
12+
url: `/api/items/${i}`,
13+
method: 'GET',
14+
status: 200,
15+
durationMs: 12,
16+
side: 'client' as const,
17+
cacheHit: false,
18+
faulted: false,
19+
at: 1000 + i,
20+
});
21+
22+
async function settle(fixture: { whenStable(): Promise<unknown> }) {
23+
for (let i = 0; i < 4; i++) {
24+
await new Promise((resolve) => setTimeout(resolve));
25+
await fixture.whenStable();
26+
}
27+
}
28+
29+
async function network(count: number) {
30+
const fixture = TestBed.createComponent(NetworkInspector);
31+
document.body.appendChild(fixture.nativeElement);
32+
fixture.componentInstance.pages.set([
33+
{
34+
pageId: 'p1',
35+
url: '/shop',
36+
title: 'Shop',
37+
hydration: null,
38+
reportedAt: 1100,
39+
calls: Array.from({ length: count }, (_, i) => call(i)),
40+
},
41+
]);
42+
await settle(fixture);
43+
return fixture;
44+
}
45+
46+
afterEach(() => TestBed.resetTestingModule());
47+
48+
describe('a long HTTP call table', () => {
49+
it('renders the rows near view and keeps the row count for screen readers', async () => {
50+
const fixture = await network(COUNT);
51+
const host = fixture.nativeElement as HTMLElement;
52+
const rows = host.querySelectorAll('tbody tr[data-vindex]');
53+
expect(rows.length).toBeGreaterThan(10);
54+
expect(rows.length).toBeLessThan(200);
55+
expect(host.querySelector('table')!.getAttribute('aria-rowcount')).toBe(String(COUNT + 1));
56+
expect(rows[3]!.getAttribute('aria-rowindex')).toBe('5');
57+
const spacer = host.querySelector<HTMLElement>('tbody tr.spacer:last-child td')!;
58+
expect(parseFloat(spacer.style.height)).toBeGreaterThan(50_000);
59+
expect(host.querySelector('.filters .total')!.textContent!.trim()).toBe(`${COUNT} of ${COUNT}`);
60+
fixture.destroy();
61+
});
62+
63+
it('goes to the last call with End', async () => {
64+
const fixture = await network(COUNT);
65+
const host = fixture.nativeElement as HTMLElement;
66+
const first = host.querySelector<HTMLElement>('button[data-call-id]')!;
67+
first.focus();
68+
first.dispatchEvent(
69+
new KeyboardEvent('keydown', { key: 'End', bubbles: true, cancelable: true }),
70+
);
71+
await settle(fixture);
72+
const focused = document.activeElement as HTMLElement;
73+
expect(focused.closest('tr')!.getAttribute('aria-rowindex')).toBe(String(COUNT + 1));
74+
// One Tab stop in the table.
75+
expect(host.querySelectorAll('button[data-call-id][tabindex="0"]')).toHaveLength(1);
76+
fixture.destroy();
77+
});
78+
79+
it('renders a short table whole, without spacers', async () => {
80+
const fixture = await network(20);
81+
const host = fixture.nativeElement as HTMLElement;
82+
expect(host.querySelectorAll('tbody tr[data-vindex]')).toHaveLength(20);
83+
expect(host.querySelector('tbody tr.spacer')).toBeNull();
84+
fixture.destroy();
85+
});
86+
});
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { TestBed } from '@angular/core/testing';
2+
import type { DevframeRpcClient } from 'devframe/client';
3+
import { describe, expect, it } from 'vitest';
4+
import { ComponentTree } from '../pages/component-tree';
5+
6+
// How long the component tree takes to render 2,000 instances, and how many
7+
// rows it puts in the DOM. Slow, so it runs only on demand:
8+
//
9+
// PANGULAR_BENCH=1 pnpm vitest run --config app/vitest.config.ts app/src/__tests__/panel-bench.test.ts
10+
11+
const COUNT = 2000;
12+
13+
/** Node's process, which the panel's types leave out. */
14+
const node = (
15+
globalThis as {
16+
process?: { env: Record<string, string | undefined>; stdout: { write(text: string): void } };
17+
}
18+
).process;
19+
20+
function roots() {
21+
const sections = Array.from({ length: 40 }, (_, s) => ({
22+
id: `s${s}`,
23+
name: 'Section',
24+
tag: 'app-section',
25+
children: [
26+
...Array.from({ length: 24 }, (_, r) => ({
27+
id: `s${s}r${r}`,
28+
name: 'Row',
29+
tag: 'app-row',
30+
children: [{ id: `s${s}r${r}c`, name: 'Cell', tag: 'app-cell', children: [] as unknown[] }],
31+
})),
32+
// One more row in all but the last section: 2,000 instances in all.
33+
...(s < 39 ? [{ id: `s${s}x`, name: 'Row', tag: 'app-row', children: [] }] : []),
34+
],
35+
}));
36+
return [{ id: 'app', name: 'App', tag: 'app-root', children: sections }];
37+
}
38+
39+
function client(tree: unknown[]): DevframeRpcClient {
40+
const state: Record<string, unknown> = {
41+
'component-tree': {
42+
pages: {
43+
a: {
44+
pageId: 'a',
45+
title: 'a',
46+
url: '/',
47+
roots: tree,
48+
count: COUNT,
49+
detail: null,
50+
reportedAt: 1,
51+
},
52+
},
53+
},
54+
};
55+
const rpc = {
56+
call: () => Promise.resolve([]),
57+
callEvent: () => Promise.resolve(),
58+
sharedState: (name: string) =>
59+
Promise.resolve({ value: () => state[name] ?? null, on: () => () => {} }),
60+
};
61+
return { connectionMeta: {}, scope: () => ({ rpc }) } as unknown as DevframeRpcClient;
62+
}
63+
64+
describe.runIf(node?.env['PANGULAR_BENCH'])('panel benchmark', () => {
65+
it('renders a 2,000 component tree', async () => {
66+
const tree = roots();
67+
const runs: number[] = [];
68+
let rows = 0;
69+
let nodes = 0;
70+
for (let i = 0; i < 5; i++) {
71+
const started = performance.now();
72+
const fixture = TestBed.createComponent(ComponentTree);
73+
fixture.componentRef.setInput('rpc', client(tree));
74+
document.body.appendChild(fixture.nativeElement);
75+
for (let k = 0; k < 4; k++) {
76+
await new Promise((resolve) => setTimeout(resolve));
77+
await fixture.whenStable();
78+
}
79+
runs.push(performance.now() - started);
80+
const host = fixture.nativeElement as HTMLElement;
81+
rows = host.querySelectorAll('.row').length;
82+
nodes = host.querySelectorAll('*').length;
83+
fixture.destroy();
84+
}
85+
runs.sort((a, b) => a - b);
86+
const result = { rows, domNodes: nodes, medianRenderMs: Math.round(runs[2]!) };
87+
node?.stdout.write(`[bench] ${JSON.stringify(result)}\n`);
88+
expect(rows).toBeGreaterThan(0);
89+
}, 120_000);
90+
});
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { ApplicationRef, signal } from '@angular/core';
2+
import { TestBed } from '@angular/core/testing';
3+
import { afterEach, describe, expect, it } from 'vitest';
4+
import { VirtualWindow } from '../ui/virtual-window';
5+
6+
// A windowed list that gets shorter while scrolled near its end, such as a
7+
// filter taking 2000 rows down to 301.
8+
9+
const ROW = 20;
10+
11+
function setup() {
12+
const count = signal(2000);
13+
const win = TestBed.runInInjectionContext(
14+
() => new VirtualWindow({ count, estimate: ROW, fixed: true }),
15+
);
16+
const host = document.createElement('div');
17+
host.style.overflowY = 'auto';
18+
let scrollTop = 0;
19+
Object.defineProperty(host, 'scrollTop', {
20+
get: () => scrollTop,
21+
set: (v: number) => (scrollTop = v),
22+
});
23+
Object.defineProperty(host, 'clientHeight', { get: () => 600 });
24+
document.body.appendChild(host);
25+
win.attach(host);
26+
const scrollTo = (top: number) => {
27+
scrollTop = top;
28+
host.dispatchEvent(new Event('scroll'));
29+
};
30+
const render = () => TestBed.inject(ApplicationRef).tick();
31+
return { count, win, host, scrollTo, setScroll: (top: number) => (scrollTop = top), render };
32+
}
33+
34+
afterEach(() => {
35+
TestBed.resetTestingModule();
36+
document.body.innerHTML = '';
37+
});
38+
39+
describe('virtual window', () => {
40+
it('still renders rows when the list shrinks below the scroll position', () => {
41+
const { count, win, scrollTo } = setup();
42+
scrollTo(1990 * ROW);
43+
expect(win.range().end).toBe(2000);
44+
count.set(301);
45+
const { start, end } = win.range();
46+
expect(end).toBe(301);
47+
expect(start).toBeLessThan(301);
48+
// The rows that fill the viewport at the end of the list, plus the overscan.
49+
expect(end - start).toBeGreaterThanOrEqual(600 / ROW);
50+
expect(win.padTop() + (end - start) * ROW + win.padBottom()).toBe(301 * ROW);
51+
});
52+
53+
it('reads the scroll position again when the row count changes', () => {
54+
const { count, win, scrollTo, setScroll, render } = setup();
55+
scrollTo(1990 * ROW);
56+
render();
57+
count.set(301);
58+
// The browser clamps the scroll position to the shorter list, with no scroll event.
59+
setScroll(301 * ROW - 600);
60+
render();
61+
// The list grows back before the next scroll: the window follows where the list really is.
62+
count.set(2000);
63+
expect(win.range().start).toBe((301 * ROW - 600) / ROW - 12);
64+
});
65+
});

0 commit comments

Comments
 (0)