GitHubivarvong

ply

main
ply6 git calls136 ms26 KiB4 cachedprotocol v2
139 lines7.8 KiBRaw
1// CPU and allocation of parseTree over real trees: every tree of a repository at a pinned commit, parsed as a read
2// does. Each run is its own node process (a fresh heap), and the trees under comparison are run interleaved, so a load
3// change on the machine hits all of them alike.
4// node test/bench-tree.mjs [--runs 7] [--corpus <repo>@<rev> ...] [--tree <dir> ...] [--op parse|fetched]
5// A corpus is every tree object `git ls-tree -r -t <rev>` names plus the root, read with `git cat-file --batch` from a
6// local clone and cached in the temp dir; the default is this checkout at HEAD. A tree is any ply checkout (an older
7// commit's worktree too), whose src/ is imported through its own ts-resolve.
8// parse parseTree(data) of every tree, once per pass
9// fetched what a read pays per fetched tree: assertFetched(data) then parseTree(data) (before #208), or the parse
10// assertFetched hands over (treeEntries, after it), whichever the tree has
11// Per run: CPU (process.cpuUsage, user + system) per pass after warm-up passes, reported as ns per entry; and bytes
12// allocated per entry, read as the heap growth over one pass under a young generation large enough that no GC runs
13// in it (the run says so if one did: then the number is a lower bound and is printed with `gc!`).
14import { spawnSync } from 'node:child_process';
15import { mkdirSync, existsSync, readFileSync, writeFileSync } from 'node:fs';
16import { loadavg, tmpdir } from 'node:os';
17import { join, resolve, basename } from 'node:path';
18import { PerformanceObserver } from 'node:perf_hooks';
20const argv = process.argv.slice(2);
21const opt = (k, d) => { const i = argv.indexOf(`--${k}`); return i < 0 ? d : argv[i + 1]; };
22const opts = (k) => argv.flatMap((a, i) => (a === `--${k}` ? [argv[i + 1]] : []));
24// ---- the corpus: [u32 length][bytes]... of every tree at <repo>@<rev> ----
25function corpusFile(spec) {
26 const at = spec.lastIndexOf('@');
27 const repo = resolve(at < 0 ? spec : spec.slice(0, at));
28 const rev = at < 0 ? 'HEAD' : spec.slice(at + 1);
29 const git = (args, input) => {
30 const p = spawnSync('git', ['-C', repo, ...args], { input, maxBuffer: 1 << 30 });
31 if (p.status !== 0) throw new Error(`git ${args.join(' ')}: ${p.stderr}`);
32 return p.stdout;
33 };
34 const sha = git(['rev-parse', `${rev}^{commit}`]).toString().trim();
35 const dir = join(tmpdir(), 'ply-bench-tree');
36 mkdirSync(dir, { recursive: true });
37 const f = join(dir, `${basename(repo)}-${sha.slice(0, 12)}.trees`);
38 if (existsSync(f)) return { file: f, name: `${basename(repo)}@${sha.slice(0, 7)}` };
39 const root = git(['rev-parse', `${sha}^{tree}`]).toString().trim();
40 const oids = [root, ...git(['ls-tree', '-r', '-t', sha]).toString().split('\n').filter((l) => l.startsWith('040000 tree ')).map((l) => l.slice(12, 52))];
41 const out = git(['cat-file', '--batch'], `${oids.join('\n')}\n`);
42 const parts = [];
43 let p = 0;
44 while (p < out.length) {
45 const nl = out.indexOf(10, p);
46 const [, type, size] = out.subarray(p, nl).toString().split(' ');
47 if (type !== 'tree') throw new Error(`not a tree: ${out.subarray(p, nl)}`);
48 const n = Number(size);
49 const len = Buffer.alloc(4); len.writeUInt32BE(n);
50 parts.push(len, out.subarray(nl + 1, nl + 1 + n));
51 p = nl + 1 + n + 1;
52 }
53 writeFileSync(f, Buffer.concat(parts));
54 return { file: f, name: `${basename(repo)}@${sha.slice(0, 7)}` };
56function readCorpus(file) {
57 const b = readFileSync(file);
58 const trees = [];
59 for (let p = 0; p < b.length;) { const n = b.readUInt32BE(p); trees.push(new Uint8Array(b.subarray(p + 4, p + 4 + n))); p += 4 + n; }
60 return trees;
63// ---- child: one op over one corpus on one tree ----
64async function child() {
65 const tree = resolve(opt('tree', '.'));
66 const op = opt('op', 'parse');
67 const trees = readCorpus(opt('file', ''));
68 const { parseTree, assertFetched } = await import(`${tree}/src/objects.ts`);
69 const fetch = await import(`${tree}/src/fetch.ts`);
70 const { DEFAULT_LIMITS } = await import(`${tree}/src/limits.ts`);
71 let entries = 0;
72 for (const t of trees) { const r = parseTree(t); if (!r.ok) throw new Error(r.error.message); entries += r.value.length; }
73 let sink = 0;
74 const max = DEFAULT_LIMITS.maxTreeEntries;
75 const pass = op === 'parse'
76 ? () => { for (const t of trees) sink += parseTree(t).value.length; }
77 // since #208 the fetch keeps assertFetched's parse on the fetched object and the reader takes it (treeEntries);
78 // before, the reader parsed the same bytes again
79 : typeof fetch.treeEntries === 'function'
80 ? () => { for (const t of trees) { const c = assertFetched('tree', t, DEFAULT_LIMITS); const o = { data: t, tree: c.value.tag === 'some' ? { tag: 'some', value: { entries: c.value.value, maxTreeEntries: max } } : c.value }; sink += fetch.treeEntries(o, DEFAULT_LIMITS).value.length; } }
81 : () => { for (const t of trees) { if (!assertFetched('tree', t, DEFAULT_LIMITS).ok) throw new Error('tree'); sink += parseTree(t, DEFAULT_LIMITS).value.length; } };
82 for (let i = 0; i < 5; i++) pass(); // warm-up: every function optimized
83 // allocation: one pass between two full GCs, no GC inside it (counted)
84 let gcs = 0;
85 const obs = new PerformanceObserver((l) => { gcs += l.getEntries().length; });
86 obs.observe({ entryTypes: ['gc'] });
87 globalThis.gc();
88 await new Promise((r) => setTimeout(r, 50));
89 gcs = 0;
90 const h0 = process.memoryUsage().heapUsed;
91 pass();
92 const h1 = process.memoryUsage().heapUsed;
93 await new Promise((r) => setTimeout(r, 50));
94 const allocGcs = gcs;
95 obs.disconnect();
96 const passes = Number(opt('passes', '20'));
97 const c0 = process.cpuUsage();
98 for (let i = 0; i < passes; i++) pass();
99 const cpu = process.cpuUsage(c0);
100 process.stdout.write(`${JSON.stringify({ ns: ((cpu.user + cpu.system) * 1000) / passes / entries, bytes: (h1 - h0) / entries, gcs: allocGcs, entries, trees: trees.length, sink })}\n`);
103// ---- parent: every corpus × op × tree × run, interleaved ----
104const median = (xs) => { const s = [...xs].sort((a, b) => a - b); const m = s.length >> 1; return s.length % 2 ? s[m] : (s[m - 1] + s[m]) / 2; };
105function parent() {
106 const trees = opts('tree').map((t) => resolve(t));
107 if (trees.length === 0) trees.push(resolve('.'));
108 const corpora = (opts('corpus').length ? opts('corpus') : ['.']).map(corpusFile);
109 const ops = opts('op').length ? opts('op') : ['parse'];
110 const runs = Number(opt('runs', '7'));
111 const passes = opt('passes', '20');
112 const res = new Map();
113 for (let r = 0; r < runs; r++) {
114 for (const c of corpora) {
115 for (const op of ops) {
116 for (const tree of trees) {
117 const p = spawnSync(process.execPath, ['--expose-gc', '--max-semi-space-size=512', '--min-semi-space-size=512', '--import', `${tree}/test/ts-resolve.mjs`, import.meta.filename, '--child', '--tree', tree, '--file', c.file, '--op', op, '--passes', passes], { encoding: 'utf8', maxBuffer: 1 << 20 });
118 if (p.status !== 0) throw new Error(`${c.name} ${op} on ${tree}: ${p.stderr}`);
119 const key = `${c.name}\t${op}\t${tree}`;
120 res.set(key, [...(res.get(key) ?? []), JSON.parse(p.stdout)]);
121 }
122 }
123 }
124 }
125 console.log(`node ${process.version}; load after ${loadavg().map((x) => x.toFixed(2)).join(' ')}; ${runs} runs × ${passes} passes; ns/entry median [min–max]; B/entry allocated (median)`);
126 for (const c of corpora) {
127 for (const op of ops) {
128 for (const tree of trees) {
129 const xs = res.get(`${c.name}\t${op}\t${tree}`);
130 const ns = xs.map((x) => x.ns);
131 const gc = xs.some((x) => x.gcs > 0) ? ' gc!' : '';
132 console.log(`${c.name.padEnd(22)} ${op.padEnd(8)} ${tree.split('/').pop().padEnd(18)} ${xs[0].trees} trees ${xs[0].entries} entries ${median(ns).toFixed(1).padStart(7)} ns [${Math.min(...ns).toFixed(1)}–${Math.max(...ns).toFixed(1)}] ${median(xs.map((x) => x.bytes)).toFixed(0).padStart(5)} B${gc}`);
133 }
134 }
135 }
138if (argv.includes('--child')) await child();
139else parent();
Diagnostics6 git calls · 136 ms
CallStatusSizeTime0136 ms
discover200191 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=191
git.server.agent=git/github-9922ec520325-Linux
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch server-option object-format
git.server.fetch=shallow wait-for-done filter
git.protocol.version=2
git.pkt.frames=9
git.pkt.data=7
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=191
git.pkt.payload.max=36
ls-refs20012 KiB107 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=101
git.response.bytes=11990
git.response.cache=EXPIRED
git.response.duration_ms=107
git.protocol.version=2
git.pkt.frames=152
git.pkt.data=151
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=11990
git.pkt.payload.max=122
git.ls_refs.count=151
git.ls_refs.head=refs/heads/main
fetchtree:0 · deepen 102004.2 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=4347
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=19912563f540228593df0c489082e5e2508e21e4
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=7
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=4347
git.pkt.payload.max=4247
git.sideband.band1.bytes=4247
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=10
git.pack.bytes=4247
git.pack.ofs_delta=0
git.pack.ref_delta=0
git.pack.chain.max=0
git.pack.trailer=a6caf8e129bea82c9bae4cbc7c130c9205add400
git.pack.full=10
Open pack map
fetchtree:0200919 B · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=919
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=800d30ba8923860bfc460ca884b7f9ba22083588
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=919
git.pkt.payload.max=892
git.sideband.band1.bytes=892
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=892
git.pack.trailer=5323b9266eea5ded637285d1c5320699f12be76e
Open pack map
fetchtree:02005.6 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=5724
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=58fabb6768b3f737ee9cd8bac647bdc27405fb4d
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=5724
git.pkt.payload.max=5697
git.sideband.band1.bytes=5697
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=5697
git.pack.trailer=be7e0453974a5fe832f9065a33bb7e3434c10e9b
Open pack map
fetch2003.2 KiB · hit22 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=22
git.response.bytes=3321
git.response.cache=HIT
git.response.duration_ms=22
git.fetch.wants=f19462b1f9208dd17421a229cbf29c6672dafbb7
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=3321
git.pkt.payload.max=3294
git.sideband.band1.bytes=3294
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://github.com/ivarvong/ply
Ref
refs/heads/main
Commit
19912563f540228593df0c489082e5e2508e21e4
cf-ray
a5e14c0b9880a140
Request
14103020…
Protocol
v2 · sha1
Agent
git/github-9922ec520325-Linux
Filter
advertised
Fetch
shallow wait-for-done filter
Capabilities
agent ls-refs fetch server-option object-format