main
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)}` };
55}
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;
61}
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`);
101}
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 }
136}
138if (argv.includes('--child')) await child();
139else parent();
Diagnostics4 git calls · 25 ms
CallStatusSizeTime025 ms
discover200152 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200145 B · hit2 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=145 git.response.cache=HIT git.response.duration_ms=2 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=145 git.pkt.payload.max=76 git.ls_refs.count=2 git.ls_refs.head=refs/heads/main
fetchblob:none · deepen 1020038 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=20 git.response.bytes=38434 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false 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=6 git.pkt.data=4 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=38434 git.pkt.payload.max=38339 git.sideband.band1.bytes=38338 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=63 git.pack.bytes=38338 git.pack.ofs_delta=0 git.pack.ref_delta=8 git.pack.chain.max=8 git.pack.trailer=0bc36ab7adf82d3109177f11f1ea93f1ce071f77 git.pack.full=55Open pack map
fetch2003.3 KiB · hit20 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=3333 git.response.cache=HIT git.response.duration_ms=20 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=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=3333 git.pkt.payload.max=3312 git.sideband.band1.bytes=3311 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://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/ply.git
- Ref
- refs/heads/main
- Commit
- 19912563f540228593df0c489082e5e2508e21e4
- cf-ray
- a5e2c0ea1ef589da
- Request
- 76ad0b5d…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format