Skip to content
Source
Sign inSign up
TanStack/redact
Open History to see the last commit

bench-devtools.mjs

89 lines5.1 KB

1import { build } from 'esbuild'
2import { chromium } from 'playwright'
3import { resolve } from 'node:path'
4import { createRequire } from 'node:module'
5​
6const baseline = process.argv[2]
7if (!baseline) throw new Error('Pass the baseline repository directory.')
8const current = resolve(import.meta.dirname, '..')
9const workload = process.env.BENCH_WORKLOAD || 'full'
10const sparseIndex = Number(process.env.BENCH_LEAF || 0)
11if (!['full', 'sparse'].includes(workload)) throw new Error('BENCH_WORKLOAD must be full or sparse.')
12const reactVersion = createRequire(import.meta.url)('react/package.json').version
13const backend = await build({
14 stdin: {
15 contents: `import {initialize} from 'react-devtools-inline/backend'; initialize(window);`,
16 resolveDir: resolve(current, 'tests'),
17 },
18 bundle: true, write: false, format: 'iife', platform: 'browser',
19 tsconfigRaw: { compilerOptions: { useDefineForClassFields: true } },
20 define: { 'process.env.NODE_ENV': '"development"' },
21})
22const browser = await chromium.launch({ headless: true, ...(process.env.CHROME_CHANNEL ? { channel: process.env.CHROME_CHANNEL } : {}) })
23try {
24 for (const attached of [false, true]) {
25 const results = { baseline: [], current: [], react: [] }
26 const bundles = {}
27 for (const [name, directory] of [['baseline', baseline], ['current', current], ['react', current]]) {
28 const source = resolve(directory, 'packages/redact/src')
29 const result = await build({
30 stdin: {
31 contents: `
32 import {createElement as h, useState, useMemo} from ${JSON.stringify(name === 'react' ? 'react' : source + '/react/index.ts')};
33 import {createRoot} from ${JSON.stringify(name === 'react' ? 'react-dom/client' : source + '/dom/client.ts')};
34 import {flushSync} from ${JSON.stringify(name === 'react' ? 'react-dom' : source + '/dom/index.ts')};
35 let update;
36 function Leaf({value}) {
37 const [local,set]=useState(0);
38 if (${JSON.stringify(workload)}==='sparse' && value===${sparseIndex}) update=set;
39 const label=useMemo(()=>String(value+local),[value,local]);return h('span',null,label);
40 }
41 function App() {
42 const [value,set]=useState(0); if (${JSON.stringify(workload)}==='full') update=set;
43 return h('div',null,Array.from({length:${workload === 'sparse' ? 1000 : 300}},(_,i)=>h(Leaf,{key:i,value:value+i})));
44 }
45 window.runBenchmark=()=>{
46 const host=document.createElement('div');document.body.append(host);
47 const root=createRoot(host);flushSync(()=>root.render(h(App)));
48 for(let i=1;i<=20;i++)flushSync(()=>update(i));
49 const start=performance.now();
50 for(let i=21;i<=120;i++)flushSync(()=>update(i));
51 const elapsed=performance.now()-start;
52 flushSync(()=>root.unmount());host.remove();return elapsed;
53 };
54 `,
55 resolveDir: current,
56 },
57 bundle: true, write: false, format: 'iife', metafile: true,
58 tsconfigRaw: { compilerOptions: { useDefineForClassFields: true } },
59 define: { 'process.env.NODE_ENV': '"development"' },
60 })
61 if (name === 'react' && !Object.keys(result.metafile.inputs).some(path => path.includes('/react-dom/cjs/'))) throw new Error('The reference must bundle real React DOM, not a source alias.')
62 bundles[name] = result.outputFiles[0].text
63 }
64 const rounds = Number(process.env.BENCH_ROUNDS || 12)
65 for (let round = 0; round < rounds; round++) {
66 for (const name of round % 2 ? ['react', 'current', 'baseline'] : ['baseline', 'current', 'react']) {
67 const page = await browser.newPage()
68 if (attached) await page.addScriptTag({ content: backend.outputFiles[0].text })
69 await page.addScriptTag({ content: bundles[name] })
70 const profiler = process.env.DEVTOOLS_CPU_PROFILE && attached && name === 'current' && round === 0 ? await page.context().newCDPSession(page) : null
71 if (profiler) { await profiler.send('Profiler.enable'); await profiler.send('Profiler.start') }
72 results[name].push(await page.evaluate(() => window.runBenchmark()))
73 if (profiler) {
74 const { profile } = await profiler.send('Profiler.stop')
75 const nodes = new Map(profile.nodes.map(node => [node.id, node.callFrame.functionName]))
76 const totals = new Map()
77 profile.samples.forEach((id, index) => totals.set(nodes.get(id), (totals.get(nodes.get(id)) || 0) + profile.timeDeltas[index]))
78 console.log('CPU profile', [...totals].sort((a,b)=>b[1]-a[1]).slice(0,25))
79 await profiler.detach()
80 }
81 await page.close()
82 }
83 }
84 const median = values => { const sorted = [...values].sort((a,b)=>a-b); return (sorted[Math.floor((sorted.length - 1)/2)] + sorted[Math.floor(sorted.length/2)])/2 }
85 console.log(JSON.stringify({ workload, sparseIndex, reactVersion, attached, samples: results, medianBaselineMs: median(results.baseline), medianCurrentMs: median(results.current), medianReactMs: median(results.react), ratio: median(results.current)/median(results.baseline) }))
86 }
87} finally {
88 await browser.close()