The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 4 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 5 | import { BunObject } from "./bun-object.ts"; |
| 6 | import { EffectfulObject } from "./effectful-object.ts"; |
| 7 | import { OpencodeObject } from "./opencode-object.ts"; |
| 8 | import { RemoteObject } from "./remote-object.ts"; |
| 9 | |
| 10 | /** |
| 11 | * Cloudflare Worker host for the container cold-start benchmark. Each request |
| 12 | * names a fresh DO instance (`?name=`), which boots its own container. |
| 13 | * |
| 14 | * - `GET /boot?variant=effectful|bun|remote|opencode&name=K` boots one |
| 15 | * container and returns `{ readyMs }`. It does NOT shut down. |
| 16 | * - `GET /shutdown?variant=…&name=K` tears the container down so the next boot |
| 17 | * is an independent cold start and the account's container cap isn't exhausted. |
| 18 | * |
| 19 | * TIMING: the clock runs HERE in the Worker, around the entire DO call — not |
| 20 | * inside the DO, so it also covers DO creation. `boot()` starts the container |
| 21 | * and waits for it. Wrapping the |
| 22 | * `getByName(name).boot()` call captures DO creation + container start + |
| 23 | * readiness probe, while staying inside Cloudflare (no client network in the |
| 24 | * number) — symmetric with the MicroVM hosts, which start the clock before |
| 25 | * `RunMicrovm`. |
| 26 | */ |
| 27 | export default class ContainerWorker extends Cloudflare.Worker<ContainerWorker>()( |
| 28 | "BenchContainerWorker", |
| 29 | { |
| 30 | main: import.meta.filename, |
| 31 | }, |
| 32 | Effect.gen(function* () { |
| 33 | const effectful = yield* EffectfulObject; |
| 34 | const remote = yield* RemoteObject; |
| 35 | const bun = yield* BunObject; |
| 36 | const opencode = yield* OpencodeObject; |
| 37 | |
| 38 | const objectFor = (variant: string) => |
| 39 | variant === "remote" |
| 40 | ? remote |
| 41 | : variant === "bun" |
| 42 | ? bun |
| 43 | : variant === "opencode" |
| 44 | ? opencode |
| 45 | : effectful; |
| 46 | |
| 47 | return { |
| 48 | fetch: Effect.gen(function* () { |
| 49 | const request = yield* HttpServerRequest; |
| 50 | const url = new URL(request.url, "http://x"); |
| 51 | const name = url.searchParams.get("name") ?? "default"; |
| 52 | const variant = url.searchParams.get("variant") ?? "effectful"; |
| 53 | |
| 54 | if (url.pathname === "/boot") { |
| 55 | // Time the WHOLE thing: DO creation, then the container start and |
| 56 | // readiness probe inside boot(). |
| 57 | const start = yield* Effect.sync(() => Date.now()); |
| 58 | yield* objectFor(variant).getByName(name).boot(); |
| 59 | const readyMs = (yield* Effect.sync(() => Date.now())) - start; |
| 60 | return yield* HttpServerResponse.json({ readyMs }); |
| 61 | } |
| 62 | if (url.pathname === "/shutdown") { |
| 63 | yield* objectFor(variant).getByName(name).shutdown(); |
| 64 | return yield* HttpServerResponse.json({ ok: true }); |
| 65 | } |
| 66 | return HttpServerResponse.text("ok"); |
| 67 | }).pipe( |
| 68 | Effect.catch((err) => |
| 69 | Effect.succeed(HttpServerResponse.text(String(err), { status: 500 })), |
| 70 | ), |
| 71 | ), |
| 72 | }; |
| 73 | }), |
| 74 | ) {} |