main
1// ply-load: open-loop, fixed-rate request generators inside Cloudflare (one Durable Object per region × shard).
2// Control (bearer = PLY_LOAD_TOKEN, the load-test credential; this Worker never holds PLY_TOKEN, the /v1 bearer):
3// POST /run {runId, target, version, workload, rate, seconds, arms, clients, fault, regions, shards, targets?, versions?, shared?}
4// shared: client labels omit region and shard, so every generator in the run sends as the SAME clients
5// (a burst from one client spread over regions). Default: each (region, shard) has its own clients.
6// versions: request i is pinned to versions[i % n]; the row's arm index is then the version index.
7// targets: request i goes to targets[(i + per-shard offset) % n]; its index is the row's last field.
8// GET /run/<runId>?regions=a,b&shards=n → every shard's state and raw rows
9// Workloads: "latency" spreads requests over many synthetic clients, each well under the limit; "abuse" gives each
10// client a fixed rate (e.g. 2× the limit) and keeps every (t, status) so the laptop computes enforcement lag and overshoot.
11// Requests carry x-ply-load-token (PLY_LOAD_TOKEN), x-ply-rate-client, x-ply-rate-mode (the arm) and, pinned,
12// Cloudflare-Workers-Version-Overrides. No address is involved: the client label is the rate identity.
13import { DurableObject } from 'cloudflare:workers';
15type Env = { PLY_LOAD_TOKEN: string; GEN: DurableObjectNamespace<Generator> };
16type Arm = 'legacy' | 'batched' | 'memo' | 'default';
17type Fault = { header: string; fromS: number; toS: number };
18type ShardCfg = {
19 runId: string;
20 target: string;
21 version: string;
22 workload: 'latency' | 'abuse';
23 rate: number;
24 seconds: number;
25 arms: Arm[];
26 clients: number;
27 fault: Fault | null;
28 hint: string;
29 shard: number;
30 maxInFlight: number;
31 /** Optional: request i goes to targets[(i + shard offset) % n] instead of `target` (e.g. one old commit per request, cold upstream). */
32 targets?: string[];
33 /** Optional: request i is pinned to versions[i % n] (the row's arm index is the version index): a per-request interleaved A/B. */
34 versions?: string[];
35 /** Optional: one set of client labels for the whole run (no region/shard in the label). */
36 shared?: boolean;
37};
38/** [t ms since start, arm index, client index, status, ttfb ms, total ms, version ok 1/0, target index] */
39type Row = [number, number, number, number, number, number, number, number];
41const CHUNK = 900;
42const HINT_ORDER = ['wnam', 'enam', 'sam', 'weur', 'eeur', 'apac', 'oc', 'afr', 'me'];
43const HINTS = new Set(HINT_ORDER);
45function sleep(ms: number): Promise<void> {
46 return new Promise(r => setTimeout(r, Math.max(0, ms)));
47}
49export class Generator extends DurableObject<Env> {
50 #rows: Row[] = [];
51 #live = false;
53 constructor(ctx: DurableObjectState, env: Env) {
54 super(ctx, env);
55 ctx.storage.sql.exec(`CREATE TABLE IF NOT EXISTS run (k TEXT PRIMARY KEY, v TEXT NOT NULL)`);
56 ctx.storage.sql.exec(`CREATE TABLE IF NOT EXISTS rows (seq INTEGER PRIMARY KEY, data TEXT NOT NULL)`);
57 }
59 #get(k: string): string | null {
60 const r = this.ctx.storage.sql.exec<{ v: string }>(`SELECT v FROM run WHERE k = ?`, k).toArray()[0];
61 return r === undefined ? null : r.v;
62 }
63 #set(k: string, v: string): void {
64 this.ctx.storage.sql.exec(`INSERT INTO run (k, v) VALUES (?, ?) ON CONFLICT (k) DO UPDATE SET v = excluded.v`, k, v);
65 }
67 async start(cfg: ShardCfg): Promise<{ ok: true }> {
68 if (this.#get('cfg') !== null) return { ok: true };
69 this.#set('cfg', JSON.stringify(cfg));
70 this.#set('state', 'pending');
71 this.#set('next', '0');
72 await this.ctx.storage.setAlarm(Date.now() + 2_000);
73 return { ok: true };
74 }
76 override async alarm(): Promise<void> {
77 const raw = this.#get('cfg');
78 if (raw === null) return;
79 const cfg = JSON.parse(raw) as ShardCfg;
80 const state = this.#get('state');
81 if (state === 'done' || state === 'failed') return;
82 if (state === 'running' && !this.#live) {
83 // A restart mid-chunk: keep what was stored, continue from the stored cursor.
84 this.#set('restarts', String(Number(this.#get('restarts') || '0') + 1));
85 }
86 if (state === 'pending') {
87 this.#set('state', 'running');
88 this.#set('t0', String(Date.now()));
89 const trace = await fetch('https://www.cloudflare.com/cdn-cgi/trace').then(r => r.text()).catch(() => '');
90 this.#set('colo', /colo=(\w+)/.exec(trace)?.[1] || '?');
91 }
92 this.#live = true;
93 const t0 = Number(this.#get('t0'));
94 const total = Math.floor(cfg.rate * cfg.seconds);
95 let i = Number(this.#get('next'));
96 const end = Math.min(total, i + CHUNK);
97 const inflight = new Set<Promise<void>>();
98 let shed = Number(this.#get('shed') || '0');
99 for (; i < end; i++) {
100 const at = t0 + (i * 1000) / cfg.rate;
101 await sleep(at - Date.now());
102 if (inflight.size >= cfg.maxInFlight) { shed++; continue; }
103 const p = this.#one(cfg, i, t0).finally(() => inflight.delete(p));
104 inflight.add(p);
105 }
106 await Promise.all([...inflight]);
107 this.ctx.storage.sql.exec(`INSERT INTO rows (data) VALUES (?)`, JSON.stringify(this.#rows));
108 this.#rows = [];
109 this.#set('next', String(i));
110 this.#set('shed', String(shed));
111 if (i >= total) {
112 this.#set('state', 'done');
113 this.#set('ended', String(Date.now()));
114 } else {
115 await this.ctx.storage.setAlarm(Date.now());
116 }
117 }
119 async #one(cfg: ShardCfg, i: number, t0: number): Promise<void> {
120 const versions = cfg.versions && cfg.versions.length ? cfg.versions : null;
121 const armIndex = versions ? i % versions.length : i % cfg.arms.length;
122 const arm = versions ? cfg.arms[0] : cfg.arms[armIndex];
123 const version = versions ? (versions[armIndex] as string) : cfg.version;
124 // latency: rotate over many clients; abuse: every client sends at the shard rate / clients.
125 const client = Math.floor(i / cfg.arms.length) % cfg.clients;
126 const headers: Record<string, string> = {
127 'x-ply-load-token': this.env.PLY_LOAD_TOKEN,
128 'x-ply-rate-client': cfg.shared ? `${cfg.runId}:shared:${arm}:${client}` : `${cfg.runId}:${cfg.hint}:${cfg.shard}:${arm}:${client}`,
129 'user-agent': 'ply-load/1',
130 };
131 if (arm !== 'default') headers['x-ply-rate-mode'] = arm;
132 if (version) headers['cloudflare-workers-version-overrides'] = `ply="${version}"`;
133 const sent = Date.now();
134 const t = sent - t0;
135 if (cfg.fault !== null && (arm === 'batched' || arm === 'memo') && t >= cfg.fault.fromS * 1000 && t < cfg.fault.toS * 1000) headers['x-ply-rate-fault'] = cfg.fault.header;
136 let status = 0;
137 let ttfb = 0;
138 let verOk = 0;
139 const list = cfg.targets && cfg.targets.length ? cfg.targets : null;
140 // Each (region, shard) starts at a different offset, so with a list no two generators ask for the same URL first.
141 const ti = list ? (i + cfg.shard * 7919 + HINT_ORDER.indexOf(cfg.hint) * 104729) % list.length : -1;
142 const target = list ? (list[ti] as string) : cfg.target;
143 try {
144 const res = await fetch(target, { headers, signal: AbortSignal.timeout(20_000) });
145 ttfb = Date.now() - sent;
146 status = res.status;
147 verOk = !version || res.headers.get('x-ply-version') === version ? 1 : 0;
148 await res.arrayBuffer();
149 } catch {
150 status = -1;
151 }
152 this.#rows.push([t, armIndex, client, status, ttfb, Date.now() - sent, verOk, ti]);
153 }
155 report(): { state: string; colo: string; cfg: ShardCfg | null; shed: number; restarts: number; t0: number; rows: Row[] } {
156 const raw = this.#get('cfg');
157 const rows: Row[] = [];
158 for (const r of this.ctx.storage.sql.exec<{ data: string }>(`SELECT data FROM rows ORDER BY seq`).toArray()) rows.push(...(JSON.parse(r.data) as Row[]));
159 return {
160 state: this.#get('state') || 'none',
161 colo: this.#get('colo') || '?',
162 cfg: raw === null ? null : (JSON.parse(raw) as ShardCfg),
163 shed: Number(this.#get('shed') || '0'),
164 restarts: Number(this.#get('restarts') || '0'),
165 // Epoch ms of the shard's row clock (t = 0): rows from shards in different regions merge on t0 + t.
166 t0: Number(this.#get('t0') || '0'),
167 rows,
168 };
169 }
170}
172const enc = new TextEncoder();
174/** Constant time: both sides hashed to equal-length digests, compared with the platform's timingSafeEqual. */
175async function authorized(req: Request, env: Env): Promise<boolean> {
176 const secret = typeof env.PLY_LOAD_TOKEN === 'string' ? env.PLY_LOAD_TOKEN : '';
177 if (secret.length === 0) return false;
178 const h = req.headers.get('authorization') || '';
179 const [x, y] = await Promise.all([crypto.subtle.digest('SHA-256', enc.encode(h)), crypto.subtle.digest('SHA-256', enc.encode(`Bearer ${secret}`))]);
180 return crypto.subtle.timingSafeEqual(x, y);
181}
183function json(obj: unknown, status = 200): Response {
184 return new Response(JSON.stringify(obj), { status, headers: { 'content-type': 'application/json' } });
185}
187function shardStub(env: Env, runId: string, hint: string, shard: number) {
188 return env.GEN.get(env.GEN.idFromName(`${runId}:${hint}:${shard}`), { locationHint: hint as DurableObjectLocationHint });
189}
191export default {
192 async fetch(req: Request, env: Env): Promise<Response> {
193 if (!(await authorized(req, env))) return json({ error: 'unauthorized' }, 401);
194 const url = new URL(req.url);
195 if (req.method === 'POST' && url.pathname === '/run') {
196 const body = (await req.json()) as Omit<ShardCfg, 'hint' | 'shard'> & { regions: string[]; shards: number };
197 const regions = body.regions.filter(h => HINTS.has(h));
198 const started: string[] = [];
199 for (const hint of regions) {
200 for (let shard = 0; shard < body.shards; shard++) {
201 const { regions: _r, shards: _s, ...rest } = body;
202 await shardStub(env, body.runId, hint, shard).start({ ...rest, hint, shard, maxInFlight: rest.maxInFlight || 64 });
203 started.push(`${hint}/${shard}`);
204 }
205 }
206 return json({ ok: true, runId: body.runId, started });
207 }
208 const m = /^\/run\/([\w.-]+)$/.exec(url.pathname);
209 if (req.method === 'GET' && m !== null) {
210 const regions = (url.searchParams.get('regions') || '').split(',').filter(h => HINTS.has(h));
211 const shards = Number(url.searchParams.get('shards') || '1');
212 const rowsWanted = url.searchParams.get('rows') === '1';
213 const out = [];
214 for (const hint of regions) {
215 for (let shard = 0; shard < shards; shard++) {
216 const r = await shardStub(env, m[1], hint, shard).report();
217 out.push({ hint, shard, state: r.state, colo: r.colo, shed: r.shed, restarts: r.restarts, t0: r.t0, n: r.rows.length, rows: rowsWanted ? r.rows : [] });
218 }
219 }
220 return json({ ok: true, shards: out });
221 }
222 return json({ error: 'not found' }, 404);
223 },
224};
Diagnostics4 git calls · 344 ms
CallStatusSizeTime0344 ms
discover200152 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=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 · hit4 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=4 git.response.bytes=145 git.response.cache=HIT git.response.duration_ms=4 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 · 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=41349 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=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
fetch2004.1 KiB338 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=338 git.response.bytes=4209 git.response.cache=MISS git.response.duration_ms=338 git.fetch.wants=8bae4d0c4a185c788ab64759926bff371ee12a97 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=4209 git.pkt.payload.max=4188 git.sideband.band1.bytes=4187 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
- a5e35bdd597edfff
- Request
- 9b154da2…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format