Artifactspublic-v0

ply

main
ply4 git calls35 ms43 KiB3 cachedprotocol v2
290 lines14 KiBRaw
1// CPU benchmark of the byte paths a Worker is billed for: inflate() of one member, a parsePack collection, and the pack
2// a push writes. Each case runs in its own node process (a fresh heap, so one case's garbage is not the next one's GC),
3// and the trees under comparison are run interleaved, so a load change on the machine hits all of them alike.
4// node test/bench.mjs [--runs 5] [--case <substring>] [--tree <dir> ...] (default tree: this checkout)
5// A tree is any ply checkout (an older commit's worktree too): the cases import its src/ through its own ts-resolve,
6// using only APIs every tree since #172 has (inflate, parsePack, buildPack or measurePack/streamPack).
7// CPU is process.cpuUsage() (user + system, every thread: CompressionStream's zlib runs on libuv's pool) per iteration,
8// after a warm-up. `rss+` is the peak resident size during one operation over the size after a gc() and a pause before
9// it, sampled every millisecond from a worker thread: what the operation held, its garbage included (the output of an
10// inflate() is in it). The memory bound itself is test/stream-memory.test.js's and test/push-stream.test.js's.
11import { spawnSync } from 'node:child_process';
12import { Worker } from 'node:worker_threads';
13import { mkdirSync, existsSync, readFileSync, writeFileSync } from 'node:fs';
14import { tmpdir, loadavg } from 'node:os';
15import { PerformanceObserver } from 'node:perf_hooks';
16import { join, resolve } from 'node:path';
17import { deflateSync } from 'node:zlib';
18import { createHash } from 'node:crypto';
20const KIB = 1024;
21const MIB = KIB * KIB;
22const argv = process.argv.slice(2);
23const opt = (k, d) => { const i = argv.indexOf(`--${k}`); return i < 0 ? d : argv[i + 1]; };
24const opts = (k) => argv.flatMap((a, i) => (a === `--${k}` ? [argv[i + 1]] : []));
26// ---- deterministic inputs (the same bytes for every tree) ----
27function prng(seed) {
28 let s = seed >>> 0 || 1;
29 return () => { s ^= s << 13; s >>>= 0; s ^= s >>> 17; s ^= s << 5; s >>>= 0; return s; };
31function randomBytes(n, seed) {
32 const r = prng(seed);
33 const out = new Uint8Array(n);
34 for (let i = 0; i < n; i += 4) { const v = r(); out[i] = v; out[i + 1] = v >>> 8; out[i + 2] = v >>> 16; out[i + 3] = v >>> 24; }
35 return out;
37// Source-like text: lines of words from a 2,000-word vocabulary (deflates about 3:1 with matches and literals mixed).
38function textBytes(n, seed) {
39 const r = prng(seed);
40 const words = Array.from({ length: 2000 }, (_, i) => `w${(i * 2654435761 >>> 0).toString(36).slice(0, 2 + (i % 7))}`);
41 const out = new Uint8Array(n);
42 let o = 0;
43 while (o < n) {
44 let line = ' '.repeat(r() % 4);
45 const k = 3 + (r() % 9);
46 for (let j = 0; j < k; j++) line += `${words[r() % words.length]}${j + 1 < k ? ' ' : ''}`;
47 line += r() % 5 === 0 ? ';\n' : '\n';
48 for (let j = 0; j < line.length && o < n; j++) out[o++] = line.charCodeAt(j);
49 }
50 return out;
52const dataOf = (kind, n, seed) => (kind === 'rand' ? randomBytes(n, seed) : textBytes(n, seed));
54// A pack built here with node's zlib (not the tree's writer), cached by name: every tree parses the same bytes.
55function objHeader(type, size) {
56 const out = [];
57 let c = (type << 4) | (size & 15);
58 let s = Math.floor(size / 16);
59 while (s > 0) { out.push(c | 0x80); c = s & 0x7f; s = Math.floor(s / 128); }
60 out.push(c);
61 return Uint8Array.from(out);
63function buildTestPack(objects) {
64 const parts = [];
65 const hdr = new Uint8Array(12);
66 hdr.set([0x50, 0x41, 0x43, 0x4b, 0, 0, 0, 2]);
67 new DataView(hdr.buffer).setUint32(8, objects.length);
68 parts.push(hdr);
69 for (const o of objects) { parts.push(objHeader(o.type, o.data.length)); parts.push(new Uint8Array(deflateSync(o.data))); }
70 const body = Buffer.concat(parts);
71 return new Uint8Array(Buffer.concat([body, createHash('sha1').update(body).digest()]));
73function treeBytes(n, seed) {
74 const r = prng(seed);
75 const out = new Uint8Array(n);
76 let o = 0;
77 for (let e = 0; o < n; e++) {
78 const head = `100644 file-${e}-${r() % 100000}.txt\0`;
79 for (let j = 0; j < head.length && o < n; j++) out[o++] = head.charCodeAt(j);
80 for (let j = 0; j < 20 && o < n; j++) out[o++] = r() & 0xff;
81 }
82 return out;
84const PACKS = {
85 tiny: () => Array.from({ length: 100_000 }, (_, i) => ({ type: 3, data: new TextEncoder().encode(`blob ${i}\n`) })),
86 trees: () => Array.from({ length: 2000 }, (_, i) => ({ type: 2, data: treeBytes(4 * KIB, i + 1) })),
87 big: () => [{ type: 3, data: textBytes(64 * MIB, 7) }],
88};
89function packFile(name) {
90 const dir = join(tmpdir(), 'ply-bench');
91 mkdirSync(dir, { recursive: true });
92 const f = join(dir, `${name}.pack`);
93 if (!existsSync(f)) writeFileSync(f, buildTestPack(PACKS[name]()));
94 return new Uint8Array(readFileSync(f));
97// ---- the cases: setup (untimed) returns the operation; iters is per timed run ----
98const sizes = { '64k': 64 * KIB, '1m': MIB, '64m': 64 * MIB };
99const iters = { '64k': 400, '1m': 20, '64m': 1 };
100const BIG_LIMITS = { maxObjectSize: 128 * MIB, maxInflatedTotal: 1024 * MIB, maxPackObjects: 200_000 };
101const CASES = {};
102for (const kind of ['rand', 'text']) {
103 for (const [sz, n] of Object.entries(sizes)) {
104 CASES[`inflate-${kind}-${sz}`] = {
105 iters: iters[sz],
106 async setup(tree) {
107 const { inflate } = await import(`${tree}/src/inflate.ts`);
108 const data = dataOf(kind, n, 3);
109 const z = new Uint8Array(deflateSync(data));
110 return () => { const r = inflate(z, 0, { expectedSize: n, maxOutput: 128 * MIB }); if (!r.ok || r.value.out.length !== n) throw new Error('inflate'); };
111 },
112 };
113 }
115for (const [name, n] of [['tiny', 1], ['trees', 2], ['big', 1]]) {
116 CASES[`parsePack-${name}`] = {
117 iters: n,
118 async setup(tree) {
119 const { parsePack } = await import(`${tree}/src/packresolve.ts`);
120 const { DEFAULT_LIMITS } = await import(`${tree}/src/limits.ts`);
121 const pack = packFile(name);
122 const limits = { ...DEFAULT_LIMITS, ...BIG_LIMITS };
123 return () => { const r = parsePack(pack, { limits }); if (!r.ok) throw new Error(r.error.message); };
124 },
125 };
127for (const [kind, sz] of [['text', '1m'], ['text', '64m'], ['rand', '64m']]) {
128 CASES[`push-${kind}-${sz}`] = {
129 iters: sz === '1m' ? 10 : 1,
130 async setup(tree) {
131 const objects = [{ type: 3, data: dataOf(kind, sizes[sz], 5) }];
132 if (existsSync(`${tree}/src/packwrite.ts`)) {
133 const { measurePack, streamPack } = await import(`${tree}/src/packwrite.ts`);
134 return async () => {
135 const m = await measurePack(objects);
136 const reader = streamPack(m, new Uint8Array(0)).stream.getReader();
137 let n = 0;
138 for (;;) { const r = await reader.read(); if (r.done) break; n += r.value.length; }
139 if (n !== m.bytes) throw new Error('push length');
140 };
141 }
142 const { buildPack } = await import(`${tree}/src/pack.ts`);
143 return async () => { await buildPack(objects); };
144 },
145 };
148// SHA-1 alone (#214): one 1 MiB buffer, and hashObject over 2,000 4 KiB trees (an object id per fetched object)
149CASES['sha1-1m'] = {
150 iters: 50,
151 async setup(tree) {
152 const { Sha1 } = await import(`${tree}/src/sha1.ts`);
153 const data = dataOf('rand', MIB, 9);
154 return () => { new Sha1().update(data).hex(); };
155 },
156};
157CASES['hashObject-trees'] = {
158 iters: 20,
159 async setup(tree) {
160 const { hashObject } = await import(`${tree}/src/objects.ts`);
161 const trees = Array.from({ length: 2000 }, (_, i) => treeBytes(4 * KIB, i + 1));
162 return () => { for (const t of trees) hashObject('tree', t); };
163 },
164};
165// A v2 ls-refs answer (#209): n refs shaped like a large GitHub repository's listing (github.com/curl/curl lists
166// ~10,000+ with refs/pull/*): HEAD with its symref target, 1% branches, 5% annotated tags with a peeled oid, the rest
167// refs/pull/<k>/head and /merge pairs. One data frame per line (LF-terminated, as upload-pack writes it), then the
168// flush. The frames are built untimed; the operation is the listing reader (lsInit, lsRefsStep per frame, lsRefsEnd),
169// the code between the pkt-line reader and the caller. `cap` is the caller's maxRefs: a listing past it is refused.
170function lsRefsFrames(n, seed) {
171 const r = prng(seed);
172 const hex = () => { let h = ''; for (let i = 0; i < 5; i++) h += r().toString(16).padStart(8, '0'); return h; };
173 const te = new TextEncoder();
174 const lines = [`${hex()} HEAD symref-target:refs/heads/master`];
175 for (let i = 0; lines.length < n + 1; i++) {
176 const k = i % 100;
177 if (k === 0) lines.push(`${hex()} refs/heads/branch-${i}`);
178 else if (k < 6) lines.push(`${hex()} refs/tags/curl-7_${i} peeled:${hex()}`);
179 else lines.push(`${hex()} refs/pull/${i >> 1}/${i % 2 ? 'merge' : 'head'}`);
180 }
181 return [...lines.map((l) => ({ kind: 'data', payload: te.encode(`${l}\n`) })), { kind: 'flush' }];
183for (const [name, n, cap, it] of [['lsrefs-20', 20, null, 2000], ['lsrefs-10k', 10_000, null, 10], ['lsrefs-100k', 100_000, null, 2], ['lsrefs-100k-capped', 100_000, 10_000, 10]]) {
184 CASES[name] = {
185 iters: it,
186 refs: cap ?? n,
187 async setup(tree) {
188 const { lsInit, lsRefsStep, lsRefsEnd } = await import(`${tree}/src/lsrefs.ts`);
189 const frames = lsRefsFrames(n, 9);
190 const caps = { maxBytes: 1 << 30, maxRefs: cap ?? n + 1, unborn: { tag: 'none' } };
191 return () => {
192 let st = lsInit();
193 for (const f of frames) {
194 const next = lsRefsStep(st, f, caps);
195 if (!next.ok) {
196 if (cap !== null && next.error.what === 'ls-refs lines') return next;
197 throw new Error(next.error.message);
198 }
199 st = next.value;
200 }
201 const end = lsRefsEnd(st);
202 if (cap !== null || !end.ok || end.value.refs.size !== n + 1) throw new Error('lsrefs');
203 return end;
204 };
205 },
206 };
209// ---- child: one case on one tree ----
210async function child() {
211 const tree = resolve(opt('tree', '.'));
212 const c = CASES[opt('case', '')];
213 const op = await c.setup(tree);
214 await op(); // warm-up
215 globalThis.gc();
216 await new Promise((r) => setTimeout(r, 200));
217 const peak = new BigInt64Array(new SharedArrayBuffer(16));
218 const base = process.memoryUsage.rss();
219 // the sampler: the largest resident size seen until slot 1 says stop
220 const sampler = new Worker(`const { workerData: p, parentPort } = require('node:worker_threads');
221 parentPort.postMessage('up');
222 while (Atomics.load(p, 1) === 0n) { const r = BigInt(process.memoryUsage.rss()); if (r > Atomics.load(p, 0)) Atomics.store(p, 0, r); Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 1); }`, { eval: true, workerData: peak });
223 await new Promise((r) => sampler.once('message', r));
224 await op(); // the memory sample
225 Atomics.store(peak, 1, 1n);
226 await new Promise((r) => sampler.once('exit', r));
227 const rssPeak = Math.max(Number(peak[0]), process.memoryUsage.rss());
228 // allocation: the heap growth over one operation that ran with no collection in it (young-generation garbage
229 // included); with --max-semi-space-size large enough that a scavenge does not interrupt it. null when one did.
230 let gcs = 0, gcMs = 0;
231 const obs = new PerformanceObserver((list) => { for (const e of list.getEntries()) { gcs += 1; gcMs += e.duration; } });
232 obs.observe({ entryTypes: ['gc'] });
233 globalThis.gc();
234 await new Promise((r) => setTimeout(r, 50));
235 gcs = 0;
236 const h0 = process.memoryUsage().heapUsed;
237 const kept = await op();
238 const h1 = process.memoryUsage().heapUsed;
239 await new Promise((r) => setTimeout(r, 50)); // the observer's entries arrive on a later turn
240 const alloc = gcs === 0 ? h1 - h0 : null;
241 globalThis.gc();
242 const retained = process.memoryUsage().heapUsed - h0;
243 void kept; // held through the gc() above: retained is what the operation's result keeps
244 const n = c.iters;
245 gcs = 0; gcMs = 0;
246 const c0 = process.cpuUsage();
247 const t0 = performance.now();
248 for (let i = 0; i < n; i++) await op(); // oxlint-disable-line no-await-in-loop -- timed in sequence
249 const cpu = process.cpuUsage(c0);
250 const wall = performance.now() - t0;
251 await new Promise((r) => setTimeout(r, 50));
252 obs.disconnect();
253 process.stdout.write(`${JSON.stringify({ cpu: (cpu.user + cpu.system) / 1000 / n, wall: wall / n, rss: rssPeak - base, alloc, retained, gcMs: gcMs / n, gcs: gcs / n })}\n`);
256// ---- parent: every case × tree × run, interleaved ----
257const 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; };
258function parent() {
259 const trees = opts('tree').map((t) => resolve(t));
260 if (trees.length === 0) trees.push(resolve('.'));
261 const runs = Number(opt('runs', '5'));
262 const only = opt('case', '');
263 const names = Object.keys(CASES).filter((k) => k.includes(only));
264 for (const name of names.filter((k) => k.startsWith('parsePack-'))) packFile(name.slice('parsePack-'.length));
265 const res = new Map();
266 for (let r = 0; r < runs; r++) {
267 for (const name of names) {
268 for (const tree of trees) {
269 const p = spawnSync(process.execPath, ['--expose-gc', ...(CASES[name].refs ? ['--max-semi-space-size=1024'] : []), '--import', `${tree}/test/ts-resolve.mjs`, import.meta.filename, '--child', '--tree', tree, '--case', name], { encoding: 'utf8', maxBuffer: 1 << 20 });
270 if (p.status !== 0) throw new Error(`${name} on ${tree}: ${p.stderr}`);
271 const key = `${name}\t${tree}`;
272 res.set(key, [...(res.get(key) ?? []), JSON.parse(p.stdout)]);
273 }
274 }
275 }
276 const load = loadavg().map((x) => x.toFixed(2)).join(' ');
277 console.log(`load after: ${load}; ${runs} runs; cpu ms per op: median [min–max]; rss+ MiB median; ls-refs cases: ns per ref, allocated and retained MiB per op, GC ms per op (medians)`);
278 for (const name of names) {
279 for (const tree of trees) {
280 const xs = res.get(`${name}\t${tree}`);
281 const cpu = xs.map((x) => x.cpu);
282 const refs = CASES[name].refs;
283 const perRef = refs ? ` ns/ref ${(median(cpu) * 1e6 / refs).toFixed(0)} [${(Math.min(...cpu) * 1e6 / refs).toFixed(0)}–${(Math.max(...cpu) * 1e6 / refs).toFixed(0)}] alloc ${xs.some((x) => x.alloc === null) ? 'n/a' : (median(xs.map((x) => x.alloc)) / MIB).toFixed(2)} retained ${(median(xs.map((x) => x.retained)) / MIB).toFixed(2)} gc ms ${median(xs.map((x) => x.gcMs)).toFixed(2)}` : '';
284 console.log(`${name.padEnd(20)} ${tree.split('/').pop().padEnd(14)} ${median(cpu).toFixed(2).padStart(9)} [${Math.min(...cpu).toFixed(2)}–${Math.max(...cpu).toFixed(2)}] wall ${median(xs.map((x) => x.wall)).toFixed(2)} rss+ ${(median(xs.map((x) => x.rss)) / MIB).toFixed(1)}${perRef}`);
285 }
286 }
289if (argv.includes('--child')) await child();
290else parent();
Diagnostics4 git calls · 35 ms
CallStatusSizeTime035 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=3
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 · hit3 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=145
git.response.cache=HIT
git.response.duration_ms=3
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 · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=29
git.response.bytes=38434
git.response.cache=HIT
git.response.duration_ms=3
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=55
Open pack map
fetch2005.6 KiB · hit29 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=5754
git.response.cache=HIT
git.response.duration_ms=29
git.fetch.wants=7521ba6f40d211450f4263c735d4d8689bb1c6bc
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=5754
git.pkt.payload.max=5733
git.sideband.band1.bytes=5732
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
a5e2c827ffc94b67
Request
4cf05adc…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format