Repository navigation
chore(web): profile the typing bench on throttled desktop Chrome #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| // Splits a CDP CPU profile into groups by the nearest named root on each sample's stack, then ranks self time. | ||
|
|
||
| /** | ||
| * @param {{ nodes: { id: number, callFrame: { functionName: string }, children?: number[] }[], samples: number[], timeDeltas: number[] }} profile | ||
| * @param {Record<string, string>} roots function name to group name | ||
| * @param {number} top functions kept per group | ||
| * @returns {Record<string, { fn: string, selfMs: number }[]>} | ||
| */ | ||
| export function summarize(profile, roots, top) { | ||
| const byId = new Map(); | ||
| const parent = new Map(); | ||
| for (const node of profile.nodes) { | ||
| byId.set(node.id, node); | ||
| for (const child of node.children ?? []) parent.set(child, node.id); | ||
| } | ||
| const groupOf = new Map(); | ||
| const group = (id) => { | ||
| const known = groupOf.get(id); | ||
| if (known) return known; | ||
| const name = byId.get(id)?.callFrame.functionName; | ||
| const up = parent.get(id); | ||
| let found = 'other'; | ||
| if (Object.hasOwn(roots, name)) found = roots[name]; | ||
| else if (up !== undefined) found = group(up); | ||
| groupOf.set(id, found); | ||
| return found; | ||
| }; | ||
| // Microseconds per group and function, so sums stay exact. | ||
| const totals = new Map(); | ||
| profile.samples.forEach((id, i) => { | ||
| const fn = byId.get(id)?.callFrame.functionName || '(anonymous)'; | ||
| if (fn === '(idle)') return; | ||
| const g = group(id); | ||
| const fns = totals.get(g) ?? new Map(); | ||
| fns.set(fn, (fns.get(fn) ?? 0) + (profile.timeDeltas[i] ?? 0)); | ||
| totals.set(g, fns); | ||
| }); | ||
| const out = {}; | ||
| for (const [g, fns] of totals) { | ||
| out[g] = [...fns] | ||
| .sort((a, b) => b[1] - a[1]) | ||
| .slice(0, top) | ||
| .map(([fn, us]) => ({ fn, selfMs: us / 1000 })); | ||
| } | ||
| return out; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { test } from 'node:test'; | ||
| // @ts-expect-error plain JavaScript script without types | ||
| import { summarize } from '../../scripts/profile-summary.mjs'; | ||
|
|
||
| type Node = { id: number; callFrame: { functionName: string }; children?: number[] }; | ||
|
|
||
| // A CDP profile: nodes by id with children, one sample per node id, timeDeltas in microseconds. | ||
| function profile(nodes: Node[], samples: number[], intervalUs: number) { | ||
| return { | ||
| nodes, | ||
| startTime: 0, | ||
| endTime: samples.length * intervalUs, | ||
| samples, | ||
| timeDeltas: samples.map(() => intervalUs), | ||
| }; | ||
| } | ||
|
|
||
| const node = (id: number, functionName: string, children: number[] = []): Node => ({ | ||
| id, | ||
| callFrame: { functionName }, | ||
| children, | ||
| }); | ||
|
|
||
| const ROOTS = { drawKeyed: 'key draw', runEngine: 'engine', applyInput: 'handler' }; | ||
|
|
||
| test('samples go to the nearest root on their stack, snapshot frames to other', () => { | ||
| const nodes = [ | ||
| node(1, '(root)', [2, 7]), | ||
| node(2, 'frame', [3, 10]), | ||
| node(3, 'drawKeyed', [4]), | ||
| node(4, 'draw', [5]), | ||
| node(5, 'frame', [6]), | ||
| node(6, 'paint'), | ||
| node(7, 'runEngine', [8]), | ||
| node(8, 'wasm-function[cp_reparse]'), | ||
| node(10, 'draw', [11]), | ||
| node(11, 'frame', [12]), | ||
| node(12, 'paint'), | ||
| ]; | ||
| const samples = [6, 6, 6, 12, 12, 12, 12, 8, 8]; | ||
| const out = summarize(profile(nodes, samples, 100), ROOTS, 12); | ||
| assert.deepEqual(out['key draw'], [{ fn: 'paint', selfMs: 0.3 }]); | ||
| assert.deepEqual(out.engine, [{ fn: 'wasm-function[cp_reparse]', selfMs: 0.2 }]); | ||
| assert.deepEqual(out.other, [{ fn: 'paint', selfMs: 0.4 }]); | ||
| }); | ||
|
|
||
| test('each group lists at most top functions, largest self time first', () => { | ||
| const nodes = [ | ||
| node(1, '(root)', [2]), | ||
| node(2, 'runEngine', [3, 4, 5]), | ||
| node(3, 'a'), | ||
| node(4, 'b'), | ||
| node(5, 'c'), | ||
| ]; | ||
| const out = summarize(profile(nodes, [3, 4, 4, 5, 5, 5], 1000), ROOTS, 2); | ||
| assert.deepEqual(out.engine, [ | ||
| { fn: 'c', selfMs: 3 }, | ||
| { fn: 'b', selfMs: 2 }, | ||
| ]); | ||
| }); | ||
|
|
||
| test('a frame named like an Object method stays in its root group', () => { | ||
| const nodes = [ | ||
| node(1, '(root)', [2]), | ||
| node(2, 'runEngine', [3, 4]), | ||
| node(3, 'constructor'), | ||
| node(4, 'toString'), | ||
| ]; | ||
| const out = summarize(profile(nodes, [3, 4], 1000), ROOTS, 12); | ||
| assert.deepEqual(Object.keys(out), ['engine']); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.