-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbenchmark-dashboard.js
More file actions
465 lines (409 loc) · 16.4 KB
/
Copy pathbenchmark-dashboard.js
File metadata and controls
465 lines (409 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#!/usr/bin/env node
/**
* Dashboard Benchmarking Script
*
* Measures FCP, LCP, TTMC, TTI (hydration), TBT, and simulated INP
* for each dashboard version (SSR, Client, RSC).
*
* Key metrics:
* - TTMC: Time to Meaningful Content (when actual data appears, not skeletons)
* - TTI: Time to Interactive (when interactive components are hydrated and respond to clicks)
* - INP: Interaction to Next Paint (measured by simulating clicks on sort/filter buttons)
* - TBT: Total Blocking Time (sum of long task durations > 50ms)
*/
const puppeteer = require('puppeteer');
const BASE = process.env.BASE_URL || 'http://localhost:3002';
const RUNS = parseInt(process.env.RUNS || '5', 10);
const WARMUP = parseInt(process.env.WARMUP || '2', 10);
const PAGES = [
{ name: 'SSR', path: '/analytics/ssr' },
{ name: 'Client', path: '/analytics/client' },
{ name: 'RSC', path: '/analytics/rsc' },
];
async function measurePage(browser, url, label) {
const page = await browser.newPage();
// Disable cache to simulate first visit
await page.setCacheEnabled(false);
// Collect performance entries, TTMC, and INP
await page.evaluateOnNewDocument(() => {
window.__perfEntries = [];
window.__lcpEntries = [];
window.__ttmcTimestamps = {};
window.__inpEntries = [];
window.__longTasks = [];
window.__ttiTimestamp = 0; // When first data-hydrated="true" appears
// Observe paint entries (FCP)
const paintObs = new PerformanceObserver((list) => {
for (const entry of list.getEntries()) {
window.__perfEntries.push({ name: entry.name, startTime: entry.startTime });
}
});
paintObs.observe({ type: 'paint', buffered: true });
// Observe LCP
const lcpObs = new PerformanceObserver((list) => {
for (const entry of list.getEntries()) {
window.__lcpEntries.push({
startTime: entry.startTime,
size: entry.size,
element: entry.element?.tagName,
});
}
});
lcpObs.observe({ type: 'largest-contentful-paint', buffered: true });
// Observe long tasks for TBT
try {
const ltObs = new PerformanceObserver((list) => {
for (const entry of list.getEntries()) {
window.__longTasks.push({ duration: entry.duration, startTime: entry.startTime });
}
});
ltObs.observe({ type: 'longtask', buffered: true });
} catch (e) {}
// Observe event timing for INP measurement
try {
const inpObs = new PerformanceObserver((list) => {
for (const entry of list.getEntries()) {
if (entry.duration > 0) {
window.__inpEntries.push({
duration: entry.duration,
processingStart: entry.processingStart,
startTime: entry.startTime,
name: entry.name,
});
}
}
});
inpObs.observe({ type: 'event', buffered: true, durationThreshold: 0 });
} catch (e) {}
// Track meaningful content appearance
const checks = {
kpi: () => {
const cards = document.querySelectorAll('.text-2xl, .text-3xl');
for (const card of cards) {
if (card.textContent && card.textContent.includes('$')) return true;
}
return false;
},
chart: () => {
const paths = document.querySelectorAll('svg path[d]');
for (const p of paths) {
if ((p.getAttribute('d') || '').length > 50) return true;
}
return false;
},
table: () => {
const rows = document.querySelectorAll('table tbody tr');
for (const row of rows) {
if (row.textContent && row.textContent.includes('#')) return true;
}
return false;
},
};
const observer = new MutationObserver(() => {
const now = performance.now();
for (const [name, check] of Object.entries(checks)) {
if (!window.__ttmcTimestamps[name] && check()) {
window.__ttmcTimestamps[name] = Math.round(now);
}
}
});
if (document.body) {
observer.observe(document.body, { childList: true, subtree: true, characterData: true });
} else {
document.addEventListener('DOMContentLoaded', () => {
observer.observe(document.body, { childList: true, subtree: true, characterData: true });
});
}
const checkAll = () => {
const now = performance.now();
for (const [name, check] of Object.entries(checks)) {
if (!window.__ttmcTimestamps[name] && check()) {
window.__ttmcTimestamps[name] = Math.round(now);
}
}
};
window.addEventListener('load', checkAll);
const interval = setInterval(() => {
checkAll();
// Also check for hydration (TTI)
if (!window.__ttiTimestamp) {
const hydrated = document.querySelector('[data-hydrated="true"]');
if (hydrated) window.__ttiTimestamp = Math.round(performance.now());
}
if (Object.keys(window.__ttmcTimestamps).length >= 3 || performance.now() > 30000) {
clearInterval(interval);
}
}, 50);
// Track TTI via MutationObserver — watches for data-hydrated attribute being set
const ttiObs = new MutationObserver((mutations) => {
if (window.__ttiTimestamp) return;
for (const mutation of mutations) {
if (mutation.type === 'attributes' && mutation.attributeName === 'data-hydrated') {
const val = mutation.target.getAttribute('data-hydrated');
if (val === 'true') {
window.__ttiTimestamp = Math.round(performance.now());
ttiObs.disconnect();
return;
}
}
// Also check subtree for new nodes with data-hydrated
if (mutation.type === 'childList') {
for (const node of mutation.addedNodes) {
if (node.nodeType === 1 && node.querySelector) {
const h = node.querySelector('[data-hydrated="true"]') || (node.getAttribute && node.getAttribute('data-hydrated') === 'true' ? node : null);
if (h) {
window.__ttiTimestamp = Math.round(performance.now());
ttiObs.disconnect();
return;
}
}
}
}
}
});
const startTtiObserver = () => {
ttiObs.observe(document.body || document.documentElement, {
attributes: true,
attributeFilter: ['data-hydrated'],
childList: true,
subtree: true,
});
};
if (document.body) startTtiObserver();
else document.addEventListener('DOMContentLoaded', startTtiObserver);
});
const startTime = Date.now();
// Navigate and wait for network idle
await page.goto(url, { waitUntil: 'networkidle0', timeout: 120000 });
// Wait for meaningful content (up to 15s)
try {
await page.waitForFunction(() => {
return window.__ttmcTimestamps && Object.keys(window.__ttmcTimestamps).length >= 2;
}, { timeout: 15000 });
} catch (e) {}
// Wait for hydration to complete
await new Promise(r => setTimeout(r, 500));
// TTI is captured by the MutationObserver in evaluateOnNewDocument
// No additional measurement needed here
// === Simulated INP ===
// Click interactive elements and measure response time
const inpResults = [];
// Try clicking a sort header
try {
await page.click('[data-sort-header]');
await new Promise(r => setTimeout(r, 100));
inpResults.push('sort');
} catch (e) {}
// Try clicking a filter button
try {
await page.click('[data-filter-btn="time"]');
await new Promise(r => setTimeout(r, 100));
inpResults.push('filter');
} catch (e) {}
// Try clicking a category button
try {
await page.click('[data-category-btn]');
await new Promise(r => setTimeout(r, 100));
inpResults.push('category');
} catch (e) {}
// Wait for INP entries to be recorded
await new Promise(r => setTimeout(r, 300));
const wallTime = Date.now() - startTime;
// Collect all metrics
const result = await page.evaluate(() => {
const nav = performance.getEntriesByType('navigation')[0];
const paints = window.__perfEntries || [];
const lcpEntries = window.__lcpEntries || [];
const ttmc = window.__ttmcTimestamps || {};
const inpEntries = window.__inpEntries || [];
const longTasks = window.__longTasks || [];
const fcp = paints.find(p => p.name === 'first-contentful-paint')?.startTime || 0;
const lcp = lcpEntries.length > 0
? lcpEntries[lcpEntries.length - 1].startTime
: 0;
const lcpElement = lcpEntries.length > 0
? lcpEntries[lcpEntries.length - 1].element
: 'N/A';
// TTMC
const ttmcValues = Object.values(ttmc);
const ttmcAll = ttmcValues.length > 0 ? Math.max(...ttmcValues) : 0;
const ttmcFirst = ttmcValues.length > 0 ? Math.min(...ttmcValues) : 0;
// TBT: sum of (duration - 50ms) for all long tasks
const tbt = longTasks.reduce((sum, task) => {
const blocking = task.duration - 50;
return sum + (blocking > 0 ? blocking : 0);
}, 0);
// INP: worst interaction duration from event timing
const inpDurations = inpEntries
.filter(e => e.name === 'pointerup' || e.name === 'click' || e.name === 'keyup')
.map(e => e.duration);
const worstInp = inpDurations.length > 0 ? Math.max(...inpDurations) : 0;
const avgInp = inpDurations.length > 0
? Math.round(inpDurations.reduce((s, d) => s + d, 0) / inpDurations.length)
: 0;
// TTI: time when first interactive element became hydrated (data-hydrated="true")
// Captured by MutationObserver during page load
const hydratedEl = document.querySelector('[data-hydrated="true"]');
let estimatedTTI = window.__ttiTimestamp || 0;
const dcl = nav?.domContentLoadedEventEnd || 0;
const load = nav?.loadEventEnd || 0;
const ttfb = nav?.responseStart || 0;
const scriptCount = document.querySelectorAll('script[src]').length;
const resources = performance.getEntriesByType('resource');
const jsResources = resources.filter(r => r.name.includes('.js'));
const jsDecodedSize = jsResources.reduce((sum, r) => sum + (r.decodedBodySize || 0), 0);
return {
ttfb: Math.round(ttfb),
fcp: Math.round(fcp),
lcp: Math.round(lcp),
lcpElement,
ttmcFirst: Math.round(ttmcFirst),
ttmcAll: Math.round(ttmcAll),
tti: estimatedTTI,
tbt: Math.round(tbt),
worstInp: Math.round(worstInp),
avgInp,
inpCount: inpDurations.length,
dcl: Math.round(dcl),
load: Math.round(load),
scriptCount,
jsDecodedKB: Math.round(jsDecodedSize / 1024),
isHydrated: !!hydratedEl,
};
});
result.wallTime = wallTime;
result.interactionsClicked = inpResults.length;
await page.close();
return result;
}
function median(arr) {
if (arr.length === 0) return 0;
const sorted = [...arr].sort((a, b) => a - b);
const mid = Math.floor(sorted.length / 2);
return sorted.length % 2 !== 0 ? sorted[mid] : (sorted[mid - 1] + sorted[mid]) / 2;
}
function formatMs(ms) {
if (ms === 0) return '—';
return ms >= 1000 ? `${(ms / 1000).toFixed(2)}s` : `${ms}ms`;
}
async function main() {
console.log(`\n📊 Dashboard Performance Benchmark`);
console.log(` Base URL: ${BASE}`);
console.log(` Runs: ${RUNS} (+ ${WARMUP} warmup)\n`);
const browser = await puppeteer.launch({
headless: 'new',
args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-dev-shm-usage'],
});
const results = {};
for (const { name, path } of PAGES) {
const url = `${BASE}${path}`;
console.log(`\n--- ${name} (${path}) ---`);
for (let i = 0; i < WARMUP; i++) {
process.stdout.write(` Warmup ${i + 1}/${WARMUP}...`);
try {
await measurePage(browser, url, `${name}-warmup-${i}`);
console.log(' done');
} catch (e) {
console.log(` error: ${e.message}`);
}
}
const runs = [];
for (let i = 0; i < RUNS; i++) {
process.stdout.write(` Run ${i + 1}/${RUNS}...`);
try {
const m = await measurePage(browser, url, `${name}-run-${i}`);
runs.push(m);
console.log(` FCP=${formatMs(m.fcp)} TTMC=${formatMs(m.ttmcAll)} TTI=${formatMs(m.tti)} TBT=${formatMs(m.tbt)} INP=${formatMs(m.worstInp)} JS=${m.jsDecodedKB}KB`);
} catch (e) {
console.log(` error: ${e.message}`);
}
}
if (runs.length > 0) {
results[name] = {
ttfb: median(runs.map(r => r.ttfb)),
fcp: median(runs.map(r => r.fcp)),
lcp: median(runs.map(r => r.lcp)),
ttmcFirst: median(runs.map(r => r.ttmcFirst)),
ttmcAll: median(runs.map(r => r.ttmcAll)),
tti: median(runs.map(r => r.tti)),
tbt: median(runs.map(r => r.tbt)),
worstInp: median(runs.map(r => r.worstInp)),
avgInp: median(runs.map(r => r.avgInp)),
dcl: median(runs.map(r => r.dcl)),
load: median(runs.map(r => r.load)),
jsDecodedKB: median(runs.map(r => r.jsDecodedKB)),
scriptCount: runs[0].scriptCount,
lcpElement: runs[0].lcpElement,
wallTime: median(runs.map(r => r.wallTime)),
isHydrated: runs.every(r => r.isHydrated),
runs: runs.length,
};
}
}
await browser.close();
// Print summary table
console.log('\n\n═════════════════════════════════════════════════════════════════════════');
console.log(' RESULTS SUMMARY');
console.log('═════════════════════════════════════════════════════════════════════════\n');
const metrics = ['ttfb', 'fcp', 'lcp', 'ttmcFirst', 'ttmcAll', 'tti', 'tbt', 'worstInp', 'avgInp', 'jsDecodedKB', 'scriptCount'];
const labels = {
ttfb: 'TTFB',
fcp: 'FCP',
lcp: 'LCP',
ttmcFirst: 'TTMC (1st)',
ttmcAll: 'TTMC (All)',
tti: 'TTI',
tbt: 'TBT',
worstInp: 'INP (worst)',
avgInp: 'INP (avg)',
jsDecodedKB: 'JS Size',
scriptCount: 'Scripts',
};
const colWidth = 14;
console.log('Metric'.padEnd(14) + Object.keys(results).map(n => n.padStart(colWidth)).join(''));
console.log('-'.repeat(14 + Object.keys(results).length * colWidth));
for (const metric of metrics) {
let row;
if (metric === 'jsDecodedKB') {
row = Object.values(results).map(r => `${r[metric]}KB`.padStart(colWidth));
} else if (metric === 'scriptCount') {
row = Object.values(results).map(r => `${r[metric]}`.padStart(colWidth));
} else {
row = Object.values(results).map(r => formatMs(r[metric]).padStart(colWidth));
}
console.log(labels[metric].padEnd(14) + row.join(''));
}
console.log('\n TTMC = Time to Meaningful Content | TTI = Time to Interactive (hydration complete)');
console.log(' INP = Interaction to Next Paint (simulated clicks on sort/filter/category buttons)');
console.log(' TBT = Total Blocking Time (long tasks > 50ms)');
// Improvement calculations
if (results['SSR'] && results['RSC']) {
console.log('\n--- RSC vs SSR Improvement ---');
for (const metric of ['fcp', 'lcp', 'ttmcFirst', 'ttmcAll', 'tti', 'tbt', 'worstInp']) {
const ssr = results['SSR'][metric];
const rsc = results['RSC'][metric];
if (ssr > 0) {
const improvement = ((ssr - rsc) / ssr * 100).toFixed(1);
console.log(` ${labels[metric]}: ${formatMs(ssr)} -> ${formatMs(rsc)} (${improvement}% ${rsc < ssr ? 'faster' : 'slower'})`);
}
}
console.log(` JS Size: ${results['SSR'].jsDecodedKB}KB -> ${results['RSC'].jsDecodedKB}KB`);
}
if (results['Client'] && results['RSC']) {
console.log('\n--- RSC vs Client Improvement ---');
for (const metric of ['fcp', 'lcp', 'ttmcFirst', 'ttmcAll', 'tti', 'tbt', 'worstInp']) {
const client = results['Client'][metric];
const rsc = results['RSC'][metric];
if (client > 0) {
const improvement = ((client - rsc) / client * 100).toFixed(1);
console.log(` ${labels[metric]}: ${formatMs(client)} -> ${formatMs(rsc)} (${improvement}% ${rsc < client ? 'faster' : 'slower'})`);
}
}
console.log(` JS Size: ${results['Client'].jsDecodedKB}KB -> ${results['RSC'].jsDecodedKB}KB`);
}
console.log('\n═════════════════════════════════════════════════════════════════════════\n');
}
main().catch(err => {
console.error('Benchmark failed:', err);
process.exit(1);
});