main
1// The /bench routes. Every one is private: src/index.js admits a request only with PLY_TOKEN (a Bearer header, or
2// HTTP Basic with PLY_TOKEN as the password, so a browser can sign in without any script). A run is started by a
3// same-origin form POST (Sec-Fetch-Site / Origin checked) and answers with a redirect to its page once it is over.
4import { dig } from '../absent.ts';
5import { VIEWER_CSP } from '../viewer/script.ts';
6import { CATALOGUE, findScenario } from './catalogue.ts';
7import type { Counts, StartAnswer } from './entry.ts';
8import { REPLAY_FORMAT, replayJson, replaySlug, replayTest, type Replay } from './export.ts';
9import { page, type Html } from './html.ts';
10import { BENCH_CSS, indexPage, messagePage, runPage } from './render.ts';
11import type { RunSummary, StoredRun } from './store.ts';
12import { renderRay } from '../rays.ts';
13import { PLY_SHA } from '../version.ts';
15export type BenchRpc = {
16 start(scenario: string, seed: string): Promise<StartAnswer>;
17 runs(limit: number): Promise<RunSummary[]>;
18 run(id: string): Promise<StoredRun | null>;
19 body(id: string, i: number, which: 'request' | 'response'): Promise<Uint8Array | null>;
20 counts(): Promise<Counts>;
21};
22const BENCH_NAME = 'bench';
23const FORM_MAX = 4096;
25export type BenchRoute =
26 | { readonly tag: 'css' }
27 | { readonly tag: 'index' }
28 | { readonly tag: 'start' }
29 | { readonly tag: 'run'; readonly id: string }
30 | { readonly tag: 'body'; readonly id: string; readonly i: number; readonly which: 'request' | 'response' }
31 | { readonly tag: 'export'; readonly id: string; readonly file: 'json' | 'test' };
33export const isBenchPath = (path: string): boolean => path === '/bench' || path === '/bench.css' || path.startsWith('/bench/');
35export function benchRoute(method: string, path: string): BenchRoute | null {
36 if (method === 'GET' && path === '/bench.css') return { tag: 'css' };
37 if (method === 'GET' && (path === '/bench' || path === '/bench/')) return { tag: 'index' };
38 if (method === 'POST' && path === '/bench/runs') return { tag: 'start' };
39 if (method !== 'GET') return null;
40 const run = /^\/bench\/runs\/([0-9a-f]{16})$/.exec(path);
41 if (run !== null) return { tag: 'run', id: String(run[1]) };
42 const body = /^\/bench\/runs\/([0-9a-f]{16})\/exchanges\/(\d{1,5})\/(request|response)$/.exec(path);
43 if (body !== null) return { tag: 'body', id: String(body[1]), i: Number(body[2]), which: body[3] === 'request' ? 'request' : 'response' };
44 const ex = /^\/bench\/runs\/([0-9a-f]{16})\/[a-z0-9-]+\.(json|test\.js)$/.exec(path);
45 if (ex !== null) return { tag: 'export', id: String(ex[1]), file: ex[2] === 'json' ? 'json' : 'test' };
46 return null;
47}
48// The route label for spans and logs: never a run id.
49export const benchRouteKey = (r: BenchRoute | null): string => (r === null ? 'bench unmatched' : `bench ${r.tag}`);
51export function benchStub(ctx: unknown): BenchRpc | null {
52 const ns = dig(ctx, ['exports', 'ProbeBench']);
53 if (ns.tag === 'none' || !ns.value) return null;
54 const space = ns.value as { idFromName(name: string): unknown; get(id: unknown): BenchRpc };
55 if (typeof space.idFromName !== 'function') return null;
56 return space.get(space.idFromName(BENCH_NAME));
57}
59const HTML_HEADERS = { 'content-type': 'text/html; charset=utf-8', 'cache-control': 'no-store', 'content-security-policy': VIEWER_CSP, 'x-robots-tag': 'noindex', 'referrer-policy': 'no-referrer' };
60const htmlOut = (status: number, h: Html): Response => new Response(page(h), { status, headers: HTML_HEADERS });
61const fileOut = (body: BodyInit, type: string, name: string): Response => new Response(body, { status: 200, headers: { 'content-type': type, 'cache-control': 'no-store', 'content-disposition': `attachment; filename="${name}"`, 'x-content-type-options': 'nosniff' } });
63// A same-origin form post: the browser says so (Sec-Fetch-Site), or the Origin is this host.
64export function sameOrigin(req: Request): boolean {
65 const site = req.headers.get('sec-fetch-site');
66 if (site !== null) return site === 'same-origin';
67 const origin = req.headers.get('origin');
68 return origin !== null && origin === new URL(req.url).origin;
69}
71export function replayOf(run: StoredRun): Replay | null {
72 const entry = findScenario(run.scenario);
73 const r = run.result;
74 if (entry.tag === 'none' || r.end === null || r.verdict.tag === 'harness') return null;
75 const differences = r.verdict.tag === 'fail' ? r.verdict.failures.map(f => ({ pair: f.difference.pair, field: f.difference.field, left: f.difference.left, right: f.difference.right })) : [];
76 return { format: REPLAY_FORMAT, run: run.id, seed: run.seed, ply: run.ply, scenario: entry.value.scenario, observed: { steps: r.steps, end: r.end }, names: r.names, differences, cfRays: r.exchanges.map(x => renderRay(x.cfRay)) };
77}
79export async function handleBench(req: Request, ctx: unknown, route: BenchRoute | null): Promise<Response> {
80 if (route === null) return htmlOut(404, messagePage('Not found', 'No such workbench page.'));
81 if (route.tag === 'css') return new Response(BENCH_CSS, { status: 200, headers: { 'content-type': 'text/css; charset=utf-8', 'cache-control': 'no-store' } });
82 const stub = benchStub(ctx);
83 if (stub === null) return htmlOut(503, messagePage('Unavailable', 'The ProbeBench object is not bound to this Worker.'));
84 switch (route.tag) {
85 case 'index': {
86 const [runs, counts] = await Promise.all([stub.runs(25), stub.counts()]);
87 return htmlOut(200, indexPage(CATALOGUE, runs, counts, PLY_SHA, null));
88 }
89 case 'start': {
90 if (!sameOrigin(req)) return htmlOut(403, messagePage('Refused', 'A run is started only from the workbench page itself.'));
91 const text = (await req.text()).slice(0, FORM_MAX);
92 const form = new URLSearchParams(text);
93 const answer = await stub.start(String(form.get('scenario')), String(form.get('seed')));
94 if (answer.tag === 'refused') return htmlOut(answer.status, messagePage('Not started', answer.reason));
95 return new Response(null, { status: 303, headers: { location: `/bench/runs/${answer.run}`, 'cache-control': 'no-store' } });
96 }
97 case 'run': {
98 const run = await stub.run(route.id);
99 if (run === null) return htmlOut(404, messagePage('No such run', `There is no run ${route.id}.`));
100 const entry = findScenario(run.scenario);
101 return htmlOut(200, runPage(run, entry.tag === 'some' ? entry.value.scenario : null));
102 }
103 case 'body': {
104 const bytes = await stub.body(route.id, route.i, route.which);
105 if (bytes === null) return htmlOut(404, messagePage('No body', 'That exchange carried no body, or it was not kept.'));
106 return fileOut(new Uint8Array(bytes), 'application/octet-stream', `${route.id}-${route.i}.${route.which}.bin`);
107 }
108 case 'export': {
109 const run = await stub.run(route.id);
110 const replay = run === null ? null : replayOf(run);
111 if (replay === null) return htmlOut(404, messagePage('Nothing to export', 'This run observed nothing to replay (no end state).'));
112 const slug = replaySlug(replay);
113 return route.file === 'json' ? fileOut(replayJson(replay), 'application/json; charset=utf-8', `${slug}.json`) : fileOut(replayTest(replay), 'text/javascript; charset=utf-8', `${slug}.test.js`);
114 }
115 }
116}
Diagnostics4 git calls · 368 ms
CallStatusSizeTime0368 ms
discover200152 B5 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 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 1020040 KiB · hit8 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=355 git.response.bytes=41349 git.response.cache=HIT git.response.duration_ms=8 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=526b305930b8ef382f2d7a1f9c0238730a585921 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=41349 git.pkt.payload.max=41254 git.sideband.band1.bytes=41253 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=61 git.pack.bytes=41253 git.pack.ofs_delta=0 git.pack.ref_delta=0 git.pack.chain.max=0 git.pack.trailer=ab60efef4b7243678c75de64a4386986ee885c1c git.pack.full=61Open pack map
fetch2002.6 KiB355 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=2694 git.response.cache=MISS git.response.duration_ms=355 git.fetch.wants=71884f2c474864a1f8dcde3d8932fbb552dc4ab4 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=2694 git.pkt.payload.max=2673 git.sideband.band1.bytes=2672 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-web.git
- Ref
- refs/heads/main
- Commit
- 526b305930b8ef382f2d7a1f9c0238730a585921
- cf-ray
- a5e2bca2bc9ccfc3
- Request
- dc964d2a…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format