The best way to control Claude Code, Codex and more from any computer or phone

main
ts 193 lines 6.97 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { describe, expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as Alchemy from "alchemy";
5 import * as Data from "effect/Data";
6 import * as Effect from "effect/Effect";
7 import * as FileSystem from "effect/FileSystem";
8 import * as HttpClientRequest from "effect/http/HttpClientRequest";
9 import * as Path from "effect/Path";
10 import * as Ref from "effect/Ref";
11 import * as Schedule from "effect/Schedule";
12 import { requestWorker } from "../Utils/WorkerRequest.ts";
13 import BenchV1 from "./fixtures/bench/v1.ts";
14
15 /**
16 * How long a deploy waits for a backfill as the keyspace grows, on one stack
17 * that stays deployed, so each step doesn't seed again:
18 *
19 * - `DOMAIN_SCALE=seed`: deploy accounts and seed `DOMAIN_SCALE_ACCOUNTS` of
20 * them (default 100,000), 10 deposits each;
21 * - `DOMAIN_SCALE=rebuild`: deploy a view keyed by account at version
22 * `DOMAIN_SCALE_VERSION` (each run a new one, so it is rebuilt from the
23 * history), print a `SCALE` line of JSON, and check a few keys;
24 * - `DOMAIN_SCALE=destroy`: remove the stack.
25 */
26
27 const step = process.env.DOMAIN_SCALE;
28 const accounts = Number(process.env.DOMAIN_SCALE_ACCOUNTS ?? "100000");
29 const version = Number(process.env.DOMAIN_SCALE_VERSION ?? "1");
30 const EVENTS = 10;
31 // Ten accounts per request, many requests at once.
32 const SEED_BATCH = 10;
33
34 const state = Cloudflare.state();
35 const { test, deploy, destroy } = Test.make({ providers: Cloudflare.providers(), state });
36
37 class NotYet extends Data.TaggedError("NotYet")<{
38 readonly status: number;
39 readonly body: string;
40 }> {}
41
42 const call = (url: string, method: "GET" | "POST", path: string) =>
43 requestWorker(
44 (method === "GET" ? HttpClientRequest.get : HttpClientRequest.post)(`${url}${path}`).pipe(
45 HttpClientRequest.setHeaders({ connection: "close", "cache-control": "no-cache" }),
46 ),
47 ).pipe(
48 Effect.flatMap((res) =>
49 res.text.pipe(
50 Effect.flatMap((body) =>
51 res.status === 200
52 ? Effect.succeed(body)
53 : Effect.fail(new NotYet({ status: res.status, body })),
54 ),
55 ),
56 ),
57 // A request can hang for minutes: send it again (every route here is idempotent).
58 Effect.timeout("2 minutes"),
59 Effect.retry({ schedule: Schedule.spaced("2 seconds"), times: 10 }),
60 );
61
62 const serving = (url: string, expected: string) =>
63 call(url, "GET", "/version").pipe(
64 Effect.filterOrFail(
65 (body) => body === expected,
66 (body) => new NotYet({ status: 200, body }),
67 ),
68 Effect.retry({ schedule: Schedule.spaced("2 seconds"), times: 90 }),
69 );
70
71 const now = Effect.sync(() => Date.now());
72
73 const stackOf = <W extends typeof BenchV1>(worker: W) =>
74 Alchemy.Stack(
75 `DomainScale-${accounts}`,
76 { providers: Cloudflare.providers(), state },
77 Effect.gen(function* () {
78 const deployed = yield* worker;
79 return { url: deployed.url.as<string>(), workerName: deployed.workerName };
80 }),
81 );
82
83 /** The view at `at`, from a fixture generated for it: the bundle carries the version. */
84 const versioned = (at: number) =>
85 Effect.gen(function* () {
86 const fs = yield* FileSystem.FileSystem;
87 const path = yield* Path.Path;
88 const dir = path.join(import.meta.dirname, "fixtures", "bench", "generated");
89 yield* fs.makeDirectory(dir, { recursive: true });
90 const file = path.join(dir, `v2-${at}.ts`);
91 yield* fs.writeFileString(
92 file,
93 `import * as Cloudflare from "@alchemy.run/cloudflare";
94 import * as Effect from "effect/Effect";
95 import * as Layer from "effect/Layer";
96 import * as Domain from "@alchemy.run/domain";
97 import { Account, Deposits } from "../../catch-up/ledger.ts";
98 import { benchWorker, hosted } from "../worker.ts";
99
100 /** The view at version ${at}. */
101 class DepositsAt extends Domain.View("Deposits", { ...Deposits.definition, version: ${at} }) {}
102 class AfterAt extends Domain.Model("Ledger", { aggregates: [Account], views: [DepositsAt] }) {}
103
104 /** Generated by CatchUp.scale.test.ts: the view at version ${at}. */
105 export default class DomainBenchWorker extends Cloudflare.Worker<DomainBenchWorker>()(
106 "DomainBenchWorker",
107 { main: import.meta.url },
108 benchWorker("v2", { kind: "deposits", view: DepositsAt as typeof Deposits }).pipe(
109 Effect.provide(hosted(AfterAt.layer(Layer.empty))),
110 ),
111 ) {}
112 `,
113 );
114 const module = yield* Effect.promise(() => import(file));
115 return module.default as typeof BenchV1;
116 });
117
118 describe("domain backfill at scale", () => {
119 test.skipIf(step !== "seed")(
120 `seed ${accounts} accounts`,
121 Effect.gen(function* () {
122 const { url } = yield* deploy(stackOf(BenchV1));
123 const base = url.replace(/\/+$/, "");
124 yield* serving(base, "v1");
125 const batches = Math.ceil(accounts / SEED_BATCH);
126 const done = yield* Ref.make(0);
127 const started = yield* now;
128 yield* Effect.forEach(
129 Array.from({ length: batches }, (_, i) => i * SEED_BATCH),
130 (from) =>
131 call(
132 base,
133 "POST",
134 `/seed/${from}/${Math.min(SEED_BATCH, accounts - from)}/${EVENTS}`,
135 ).pipe(
136 Effect.andThen(Ref.updateAndGet(done, (n) => n + 1)),
137 Effect.tap((n) =>
138 n % 500 === 0
139 ? Effect.flatMap(now, (at) =>
140 Effect.sync(() =>
141 console.log(
142 `SEED ${n * SEED_BATCH}/${accounts} ${((at - started) / 1000).toFixed(0)}s`,
143 ),
144 ),
145 )
146 : Effect.void,
147 ),
148 ),
149 { concurrency: 64, discard: true },
150 );
151 const ended = yield* now;
152 yield* Effect.sync(() =>
153 console.log(`SEEDED ${JSON.stringify({ accounts, seconds: (ended - started) / 1000 })}`),
154 );
155 }),
156 { timeout: 7_200_000 },
157 );
158
159 test.skipIf(step !== "rebuild")(
160 `rebuild a view over ${accounts} accounts at version ${version}`,
161 Effect.gen(function* () {
162 const worker = yield* versioned(version);
163 const started = yield* now;
164 const { url } = yield* deploy(stackOf(worker));
165 const ended = yield* now;
166 const base = url.replace(/\/+$/, "");
167 yield* serving(base, "v2");
168 const history = JSON.parse(yield* call(base, "GET", "/history")) as { complete: boolean };
169 const expected = Array.from({ length: EVENTS }, (_, n) => n + 1);
170 for (const i of [0, Math.floor(accounts / 2), accounts - 1]) {
171 const view = JSON.parse(yield* call(base, "GET", `/view/b${i}`)) as {
172 amounts: Array<number>;
173 } | null;
174 expect(view?.amounts).toEqual(expected);
175 }
176 expect(history.complete).toBe(true);
177 yield* Effect.sync(() =>
178 console.log(
179 `SCALE ${JSON.stringify({ accounts, version, deploySeconds: (ended - started) / 1000 })}`,
180 ),
181 );
182 }),
183 { timeout: 7_200_000 },
184 );
185
186 test.skipIf(step !== "destroy")(
187 "destroy",
188 Effect.gen(function* () {
189 yield* destroy(stackOf(BenchV1));
190 }),
191 { timeout: 1_800_000 },
192 );
193 });