Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

g1t.sh
241 linesCodeBlameRaw

Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.

The runner base keeps its old comment, so the pushed base image still matches its folder1#!/usr/bin/env node
2// Where should g1t's Workers run? A throwaway experiment that answers it
3// with numbers, without touching any production Worker.
4//
5// It deploys a handful of tiny Workers named g1t-probe-*, one per
6// placement setting, each bound read-only in practice (it runs only
7// `SELECT 1`) to the g1t-repos D1 database, plus g1t-probe-edge, which runs
8// near the caller and calls each of the others through a service binding,
9// the way the site calls the services. Then it measures them from this
10// machine, prints a table, and deletes them.
11//
12// node scripts/perf/placement-probe.mjs deploy # make the probes (needs `wrangler login`)
13// node scripts/perf/placement-probe.mjs measure # 1 warm-up + 7 requests each, a table
14// node scripts/perf/placement-probe.mjs delete # remove them all
15// node scripts/perf/placement-probe.mjs apply '{"region":"aws:us-west-1"}'
16// # write a placement into every config that should have it
17// node scripts/perf/placement-probe.mjs apply '{"mode":"smart"}' # back to Smart Placement
18//
The docs folder is gone, and what it held lives where people read it: how a self-hosted g1t runs and how to deploy g1t to Cloudflare are pages on docs.g1t.sh under Run g1t yourself, and speed, rate limits and operating g1t.sh are sections of CONTRIBUTING.md; code that cited a file in docs/ now points to the page or section that covers it, or says what it means itself, and applied migrations and the runner images are left as they were.19// Nothing here runs in CI.
The runner base keeps its old comment, so the pushed base image still matches its folder20
21import { mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
22import { tmpdir } from "node:os";
23import { join } from "node:path";
24
25import { exec, wranglerEnv } from "../deploy/cloudflare.mjs";
26import { ROOT } from "../deploy/stack.mjs";
27
28const WRANGLER = join(ROOT, "node_modules/wrangler/bin/wrangler.js");
29const DATABASE = { name: "g1t-repos", id: "f9544c51-c3bf-4621-a96f-8a6d5cf24a97" };
30
31/** Each placement tried. Regions near D1's WNAM, one far one as a control. */
32export const VARIANTS = [
33 { name: "off", placement: { mode: "off" } },
34 { name: "smart", placement: { mode: "smart" } },
35 { name: "aws-us-west-1", placement: { region: "aws:us-west-1" } },
36 { name: "aws-us-west-2", placement: { region: "aws:us-west-2" } },
37 { name: "gcp-us-west1", placement: { region: "gcp:us-west1" } },
38 { name: "gcp-us-west2", placement: { region: "gcp:us-west2" } },
39 { name: "aws-us-east-1", placement: { region: "aws:us-east-1" } },
40];
41
42/**
43 * The Workers that read D1 or call services that do, by config: what
44 * `apply` sets the placement of. The rest (pages, models, og, runner,
45 * status, docs) serve from the edge or have no data of their own.
46 */
47export const PLACED = [
48 "apps/web/wrangler.jsonc",
49 "apps/api/wrangler.jsonc",
50 "apps/sudo/wrangler.jsonc",
51 "services/actions/wrangler.jsonc",
52 "services/billing/wrangler.jsonc",
53 "services/context/wrangler.jsonc",
54 "services/deployments/wrangler.jsonc",
55 "services/events/wrangler.jsonc",
56 "services/identity/wrangler.jsonc",
57 "services/integrations/wrangler.jsonc",
58 "services/projects/wrangler.jsonc",
59 "services/repos/wrangler.jsonc",
60 "services/search/wrangler.jsonc",
61 "services/security/wrangler.jsonc",
62 "services/webhooks/wrangler.jsonc",
63 "services/work/wrangler.jsonc",
64];
65
66const PROBE = `
67const QUERIES = 5;
68async function ranIn() {
69 const trace = await (await fetch("https://cloudflare.com/cdn-cgi/trace")).text();
70 return /colo=(\\w+)/.exec(trace)?.[1] ?? null;
71}
72async function timedQueries(db) {
73 const ms = [];
74 let meta = null;
75 for (let i = 0; i < QUERIES; i++) {
76 const started = Date.now();
77 const result = await db.prepare("SELECT 1 AS one").run();
78 ms.push(Date.now() - started);
79 meta = result.meta;
80 }
81 return { ms, region: meta?.served_by_region ?? null, primary: meta?.served_by_primary ?? null, sql_ms: meta?.timings?.sql_duration_ms ?? null };
82}
83export default {
84 async fetch(request, env) {
85 const { pathname } = new URL(request.url);
86 if (pathname === "/chain") {
87 const names = Object.keys(env).filter((key) => key.startsWith("TO_"));
88 const out = {};
89 await Promise.all(names.map(async (name) => {
90 const started = Date.now();
91 try {
92 const answer = await (await env[name].fetch("https://probe/")).json();
93 out[name.slice(3).toLowerCase().replaceAll("_", "-")] = { rpc_ms: Date.now() - started, ran_in: answer.ran_in, primary_ms: answer.primary.ms };
94 } catch (error) {
95 out[name] = { error: String(error) };
96 }
97 }));
98 return Response.json({ worker: env.NAME, ingress: request.cf?.colo ?? null, ran_in: await ranIn(), chain: out });
99 }
100 const where = await ranIn();
101 const primary = await timedQueries(env.DB);
102 const session = await timedQueries(env.DB.withSession("first-unconstrained"));
103 return Response.json({ worker: env.NAME, ingress: request.cf?.colo ?? null, ran_in: where, primary, session });
104 },
105};
106`;
107
108function configFor(variant, bindings = []) {
109 return {
110 name: `g1t-probe-${variant.name}`,
111 main: "worker.js",
112 account_id: "1e6f2cffa3f445920836e8ebe446bb58",
113 compatibility_date: "2026-09-26",
114 workers_dev: true,
115 preview_urls: false,
116 placement: variant.placement,
117 vars: { NAME: variant.name },
118 d1_databases: [{ binding: "DB", database_name: DATABASE.name, database_id: DATABASE.id }],
119 services: bindings,
120 };
121}
122
123async function wrangler(args, cwd) {
124 const { code, out } = await exec(process.execPath, [WRANGLER, ...args], { cwd, env: wranglerEnv() });
125 return { code, out };
126}
127
128const URLS = join(tmpdir(), "g1t-placement-probe", "urls.json");
129
130async function deploy() {
131 const urls = {};
132 const edge = { name: "edge", placement: { mode: "off" } };
133 const bindings = VARIANTS.map((v) => ({ binding: `TO_${v.name.toUpperCase().replaceAll("-", "_")}`, service: `g1t-probe-${v.name}` }));
134 for (const [variant, extra] of [...VARIANTS.map((v) => [v, []]), [edge, bindings]]) {
135 const dir = join(tmpdir(), "g1t-placement-probe", variant.name);
136 rmSync(dir, { recursive: true, force: true });
137 mkdirSync(dir, { recursive: true });
138 writeFileSync(join(dir, "worker.js"), PROBE);
139 writeFileSync(join(dir, "wrangler.json"), JSON.stringify(configFor(variant, extra), null, 2));
140 process.stdout.write(`deploying g1t-probe-${variant.name} ${JSON.stringify(variant.placement)} … `);
141 const { code, out } = await wrangler(["deploy", "--config", "wrangler.json"], dir);
142 const url = /https:\/\/g1t-probe-[\w-]+\.[\w-]+\.workers\.dev/.exec(out)?.[0];
143 console.log(code === 0 ? url ?? "deployed (no URL printed)" : "FAILED");
144 if (code !== 0) console.log(out.split("\n").slice(-15).join("\n"));
145 if (url) urls[variant.name] = url;
146 }
147 writeFileSync(URLS, JSON.stringify(urls, null, 2));
148 console.log("\nSmart Placement needs traffic from several places and up to 15 minutes before it moves anything;");
149 console.log("the region variants are placed at once. Wait a minute, then: node scripts/perf/placement-probe.mjs measure");
150}
151
152const median = (values) => {
153 const sorted = [...values].filter((v) => typeof v === "number").sort((a, b) => a - b);
154 return sorted.length ? sorted[Math.floor(sorted.length / 2)] : null;
155};
156
157async function timedFetch(url) {
158 const started = performance.now();
159 const response = await fetch(url, { headers: { "cache-control": "no-cache" } });
160 const ttfb = performance.now() - started;
161 const body = await response.json();
162 return { ttfb: Math.round(ttfb), placement: response.headers.get("cf-placement"), body };
163}
164
165async function measure() {
166 const urls = JSON.parse(readFileSync(URLS, "utf8"));
167 const rows = [];
168 for (const variant of VARIANTS) {
169 const url = urls[variant.name];
170 if (!url) continue;
171 await timedFetch(url).catch(() => null);
172 const runs = [];
173 for (let i = 0; i < 7; i++) runs.push(await timedFetch(url));
174 const last = runs.at(-1);
175 rows.push({
176 variant: variant.name,
177 "cf-placement": last.placement ?? "",
178 "ran in": last.body.ran_in,
179 "ttfb ms (p50)": median(runs.map((r) => r.ttfb)),
180 "D1 primary ms/query (p50)": median(runs.flatMap((r) => r.body.primary.ms)),
181 "D1 session ms/query (p50)": median(runs.flatMap((r) => r.body.session.ms)),
182 "session served by": `${last.body.session.region ?? "?"}${last.body.session.primary ? " (primary)" : ""}`,
183 });
184 }
185 console.table(rows);
186 if (urls.edge) {
187 await timedFetch(`${urls.edge}/chain`).catch(() => null);
188 const chains = [];
189 for (let i = 0; i < 5; i++) chains.push(await timedFetch(`${urls.edge}/chain`));
190 const names = Object.keys(chains[0].body.chain);
191 console.log(`\nFrom g1t-probe-edge (runs in ${chains[0].body.ran_in}) through a service binding, as the site calls a service:`);
192 console.table(
193 names.map((name) => ({
194 target: name,
195 "target ran in": chains.at(-1).body.chain[name].ran_in,
196 "rpc ms (p50)": median(chains.map((c) => c.body.chain[name].rpc_ms)),
197 "its D1 ms/query (p50)": median(chains.flatMap((c) => c.body.chain[name].primary_ms ?? [])),
198 })),
199 );
200 }
201 console.log("\nPick the region with the lowest D1 primary ms/query (the database's neighbour), then:");
202 console.log(` node scripts/perf/placement-probe.mjs apply '{"region":"<it>"}'`);
203}
204
205async function remove() {
206 for (const name of [...VARIANTS.map((v) => v.name), "edge"]) {
207 const { code } = await wrangler(["delete", "--name", `g1t-probe-${name}`, "--force"], ROOT);
208 console.log(`g1t-probe-${name}: ${code === 0 ? "deleted" : "not deleted (already gone?)"}`);
209 }
210}
211
212/** Sets `"placement"` in every config in PLACED, keeping the line's place. */
213export function withPlacement(text, placement) {
214 const fields = Object.entries(placement).map(([key, value]) => `"${key}": ${JSON.stringify(value)}`);
215 const line = `"placement": { ${fields.join(", ")} },`;
216 if (/"placement":\s*\{[^}]*\},?/.test(text)) return text.replace(/"placement":\s*\{[^}]*\},?/, line);
217 return text.replace(/(\r?\n)([ \t]*)("compatibility_date":[^\r\n]*)/, (_, nl, indent, date) => `${nl}${indent}${date}${nl}${indent}${line}`);
218}
219
220function apply(json) {
221 const placement = JSON.parse(json);
222 for (const file of PLACED) {
223 const path = join(ROOT, file);
224 const before = readFileSync(path, "utf8");
225 const after = withPlacement(before, placement);
226 if (after !== before) writeFileSync(path, after);
227 console.log(`${file}: ${after === before ? "unchanged" : JSON.stringify(placement)}`);
228 }
229}
230
231const [command, argument] = process.argv.slice(2);
232if (process.argv[1]?.replaceAll("\\", "/").endsWith("scripts/perf/placement-probe.mjs")) {
233 if (command === "deploy") await deploy();
234 else if (command === "measure") await measure();
235 else if (command === "delete") await remove();
236 else if (command === "apply" && argument) apply(argument);
237 else {
238 console.log("usage: node scripts/perf/placement-probe.mjs deploy | measure | delete | apply '<placement json>'");
239 process.exitCode = 1;
240 }
241}

This file's history is long; its oldest lines are credited to the oldest commit read.