GitHubivarvong
ply
main
1// Round trips and wall time of the read operations against real remotes, old tree against new (issue #218).
2// node test/bench-rt.mjs [--runs 7] [--tree <dir> ...] [--bearer-file <json with .plaintext>] --remote '<url> <file d1> <file d2> <file d3> <dir d1>' ...
3// A tree is any ply checkout (an older commit's `git archive` of src/ and test/ts-resolve.mjs too): its src/ is imported
4// through its own module graph (module state such as the quirks memo is per tree). Each tree gets one quirks record per
5// remote, shared by every run after one warm-up pass, which is how a long-lived caller (ply-web: one record per origin
6// per isolate) runs: the steady state, not first contact. Runs are interleaved tree by tree and op by op, so a change in
7// the network hits every tree alike.
8// requests every HTTP request the op made (GET info/refs, POST git-upload-pack)
9// serial the requests on the critical path: a request that starts after every earlier one has ended starts a new
10// step; one that starts while another is in flight joins the current step
11// ms wall time of the op, a readFile's stream drained
12// `--bearer-file` sends `Authorization: Bearer <plaintext>` to an Artifacts host (*.artifacts.cloudflare.net) only.
13import { readFileSync } from 'node:fs';
14import { resolve } from 'node:path';
15import { pathToFileURL } from 'node:url';
16import { registerHooks } from 'node:module';
18registerHooks({
19 resolve(specifier, context, next) {
20 try { return next(specifier, context); } catch (e) {
21 if (e?.code !== 'ERR_MODULE_NOT_FOUND' || !/^\.\.?\//.test(specifier) || !specifier.endsWith('.js')) throw e;
22 return next(`${specifier.slice(0, -3)}.ts`, context);
23 }
24 },
25});
27const argv = process.argv.slice(2);
28const opt = (k, d) => { const i = argv.indexOf(`--${k}`); return i < 0 ? d : argv[i + 1]; };
29const opts = k => argv.flatMap((a, i) => (a === `--${k}` ? [argv[i + 1]] : []));
30const runs = Number(opt('runs', '7'));
31const trees = opts('tree').length ? opts('tree') : ['.'];
32const bearerFile = opt('bearer-file', null);
33const bearer = bearerFile === null ? null : JSON.parse(readFileSync(bearerFile, 'utf8')).plaintext;
34const remotes = opts('remote').map(s => { const [url, f1, f2, f3, d1] = s.split(' '); return { url, f1, f2, f3, d1 }; });
35if (remotes.length === 0) { console.error('give at least one --remote'); process.exit(2); }
37const load = async dir => {
38 const src = pathToFileURL(resolve(dir, 'src')).href;
39 const [read, log] = await Promise.all([import(`${src}/read.ts`), import(`${src}/log.ts`)]);
40 return { dir, ...read, readLog: log.readLog };
41};
42const loaded = await Promise.all(trees.map(load));
44// A fetch that records each request's start and end (the end is when its body has been read to the end or cancelled).
45function counting() {
46 const events = [];
47 const fetchImpl = async (input, init) => {
48 const ev = { start: performance.now(), end: Number.POSITIVE_INFINITY };
49 events.push(ev);
50 const res = await fetch(input, init);
51 if (!res.body) { ev.end = performance.now(); return res; }
52 const reader = res.body.getReader();
53 const body = new ReadableStream({
54 async pull(c) { const r = await reader.read(); if (r.done) { ev.end = performance.now(); c.close(); } else c.enqueue(r.value); },
55 cancel(reason) { ev.end = performance.now(); return reader.cancel(reason); },
56 });
57 return new Response(body, { status: res.status, statusText: res.statusText, headers: res.headers });
58 };
59 const serial = () => {
60 let steps = 0, until = -1;
61 for (const e of [...events].sort((a, b) => a.start - b.start)) {
62 if (e.start >= until) steps++;
63 until = Math.max(until, e.end);
64 }
65 return steps;
66 };
67 return { fetchImpl, events, serial };
68}
69const drain = async stream => { let n = 0; for await (const c of stream) n += c.length; return n; };
70const ops = r => [
71 ['readCommit', (ply, o) => ply.readCommit(r.url, { ref: null }, o)],
72 ['readLog 10', (ply, o) => ply.readLog(r.url, { ref: null, limit: 10 }, o)],
73 ['listTree /', (ply, o) => ply.listTree(r.url, { ref: null, path: '' }, o)],
74 [`listTree ${r.d1}`, (ply, o) => ply.listTree(r.url, { ref: null, path: r.d1 }, o)],
75 [`readFile ${r.f1}`, (ply, o) => ply.readFile(r.url, { ref: null, path: r.f1 }, o)],
76 [`readFile ${r.f2}`, (ply, o) => ply.readFile(r.url, { ref: null, path: r.f2 }, o)],
77 [`readFile ${r.f3}`, (ply, o) => ply.readFile(r.url, { ref: null, path: r.f3 }, o)],
78];
79const auth = url => (bearer !== null && new URL(url).host.endsWith('.artifacts.cloudflare.net') ? { auth: { bearer } } : {});
81async function once(ply, quirks, r, fn) {
82 const c = counting();
83 const t0 = performance.now();
84 const got = await fn(ply, { quirks, fetchImpl: c.fetchImpl, ...auth(r.url) });
85 if (!got.ok) throw new Error(`${got.error.code}: ${got.error.message}`);
86 if (got.value.stream) await drain(got.value.stream);
87 return { ms: performance.now() - t0, requests: c.events.length, serial: c.serial() };
88}
89const q = (a, p) => { const s = [...a].sort((x, y) => x - y); return s[Math.min(s.length - 1, Math.floor(p * s.length))]; };
90const fmt = a => `${Math.round(q(a, 0.5))} [${Math.round(q(a, 0))}–${Math.round(q(a, 1))}]`;
91const counts = a => { const lo = Math.min(...a), hi = Math.max(...a); return lo === hi ? `${lo}` : `${lo}–${hi}`; };
93for (const r of remotes) {
94 const quirks = loaded.map(() => ({}));
95 const table = new Map();
96 for (const [name, fn] of ops(r)) {
97 loaded.forEach(ply => table.set(`${name}\0${ply.dir}`, { ms: [], requests: [], serial: [] }));
98 // warm-up: the quirks record learns what this server takes (first contact is not what is measured)
99 for (const [i, ply] of loaded.entries()) await once(ply, quirks[i], r, fn);
100 }
101 for (let run = 0; run < runs; run++) {
102 for (const [name, fn] of ops(r)) {
103 for (const [i, ply] of loaded.entries()) {
104 const m = await once(ply, quirks[i], r, fn);
105 const row = table.get(`${name}\0${ply.dir}`);
106 row.ms.push(m.ms); row.requests.push(m.requests); row.serial.push(m.serial);
107 }
108 }
109 }
110 console.log(`\n${new URL(r.url).host}${new URL(r.url).pathname} (${runs} runs, steady state; median [min–max] ms)`);
111 console.log(` ${'op'.padEnd(44)} ${loaded.map(p => `${p.dir.slice(-14).padStart(14)}: req serial ms`.padEnd(46)).join(' ')}`);
112 for (const [name] of ops(r)) {
113 const cells = loaded.map(ply => { const row = table.get(`${name}\0${ply.dir}`); return `${counts(row.requests).padStart(20)} ${counts(row.serial).padStart(4)} ${fmt(row.ms).padEnd(20)}`; });
114 console.log(` ${name.padEnd(44)} ${cells.join(' ')}`);
115 }
116}
Diagnostics8 git calls · 113 ms
CallStatusSizeTime0113 ms
discover200191 B2 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=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 KiB · 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=11990 git.response.cache=HIT git.response.duration_ms=2 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 · hit5 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=88 git.response.bytes=4347 git.response.cache=HIT git.response.duration_ms=5 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=10Open pack map
fetchtree:0 · deepen 1200347 B · hit5 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=5 git.response.bytes=347 git.response.cache=HIT git.response.duration_ms=5 git.fetch.filter=tree:0 git.fetch.wants=19912563f540228593df0c489082e5e2508e21e4 git.fetch.haves.count=0 git.fetch.deepen=1 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=347 git.pkt.payload.max=247 git.sideband.band1.bytes=247 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=247 git.pack.trailer=4b72f39b5ea57c5ae3009f90b53e3a6a155093e7Open pack map
fetchcombine:blob:none+tree:2 · deepen 1200—88 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.cache=BYPASS git.fetch.filter=combine:blob:none+tree:2 git.fetch.wants=19912563f540228593df0c489082e5e2508e21e4 git.fetch.haves.count=0 git.fetch.deepen=1 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 error.type=remote_error error=remote: filter 'combine' not supported
fetchtree:0200919 B · hit3 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=3 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=5323b9266eea5ded637285d1c5320699f12be76eOpen 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=18 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=be7e0453974a5fe832f9065a33bb7e3434c10e9bOpen pack map
fetch2002.8 KiB · hit18 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=2822 git.response.cache=HIT git.response.duration_ms=18 git.fetch.wants=e5d134d1c994fd78116c7baede4ade27f61115d7 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=2822 git.pkt.payload.max=2795 git.sideband.band1.bytes=2795 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
- a5e9e9843eea8a31
- Request
- c80c8f8b…
- 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