Created
February 11, 2026 09:31
-
-
Save typeofweb/a74dc8695e090b1881565d733408386e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bun | |
| import { parseArgs } from "node:util"; | |
| import { type CommandFinished, Sandbox } from "@vercel/sandbox"; | |
| const SANDBOX_CONFIG = { | |
| timeout: 1000 * 60 * 5, // 5 minutes | |
| ports: [3000], | |
| resources: { vcpus: 4 }, | |
| projectId: process.env.PROJECT_ID_VERCEL, | |
| teamId: process.env.TEAM_ID_VERCEL, | |
| token: process.env.AUTH_BEARER_TOKEN_VERCEL, | |
| runtime: "node24" as const, | |
| }; | |
| const { values } = parseArgs({ | |
| args: Bun.argv, | |
| options: { | |
| "create-snapshot": { | |
| type: "boolean", | |
| }, | |
| benchmark: { | |
| type: "string", | |
| }, | |
| }, | |
| strict: false, | |
| allowPositionals: true, | |
| }); | |
| async function handleCmdResult(result: CommandFinished) { | |
| const [stdout, stderr] = await Promise.all([result.stdout(), result.stderr()]); | |
| if (result.exitCode !== 0) { | |
| console.error("Sandbox command failed", { exitCode: result.exitCode, stdout, stderr }); | |
| throw new Error(`Sandbox command failed: ${stderr}`); | |
| } | |
| return stdout; | |
| } | |
| async function createSnapshot() { | |
| console.log("Creating fresh sandbox…"); | |
| const sandbox = await Sandbox.create(SANDBOX_CONFIG); | |
| console.log("Sandbox created", { sandboxId: sandbox.sandboxId }); | |
| try { | |
| console.log("Installing bun…"); | |
| const installResult = await sandbox.runCommand({ | |
| cmd: "bash", | |
| args: ["-c", "npm install -g bun"], | |
| }); | |
| await handleCmdResult(installResult); | |
| console.log("Verifying bun…"); | |
| const verifyResult = await sandbox.runCommand({ | |
| cmd: "bash", | |
| args: ["-c", "bun -e 'console.log(\"hello\")'"], | |
| }); | |
| const output = await handleCmdResult(verifyResult); | |
| console.log("Bun verification output:", output.trim()); | |
| console.log("Creating snapshot…"); | |
| const snapshot = await sandbox.snapshot(); | |
| console.log("Snapshot created!", { snapshotId: snapshot.snapshotId }); | |
| console.log("\n========================================"); | |
| console.log(`Snapshot ID: ${snapshot.snapshotId}`); | |
| console.log("\nRun benchmark with:"); | |
| console.log(` bun run bin/sandbox/sandbox.ts --benchmark ${snapshot.snapshotId}`); | |
| console.log("========================================\n"); | |
| } catch (error) { | |
| await sandbox.stop().catch(() => {}); | |
| throw error; | |
| } | |
| } | |
| async function benchmark(snapshotId: string) { | |
| console.log("Starting benchmark…\n"); | |
| // Test A: Fresh sandbox + install bun | |
| console.log("Test A: Creating fresh sandbox + installing bun…"); | |
| const startA = performance.now(); | |
| const sandboxA = await Sandbox.create(SANDBOX_CONFIG); | |
| const installA = await sandboxA.runCommand({ | |
| cmd: "bash", | |
| args: ["-c", "npm install -g bun && bun -e 'console.log(\"hello\")'"], | |
| }); | |
| await handleCmdResult(installA); | |
| const timeFresh = (performance.now() - startA) / 1000; | |
| console.log(`Test A (fresh): ${timeFresh.toFixed(2)}s`); | |
| await sandboxA.stop().catch(() => {}); | |
| // Test B: Restore from snapshot | |
| console.log("Test B: Creating sandbox from snapshot…"); | |
| const startB = performance.now(); | |
| const sandboxB = await Sandbox.create({ | |
| ...SANDBOX_CONFIG, | |
| source: { type: "snapshot", snapshotId }, | |
| }); | |
| const verifyB = await sandboxB.runCommand({ | |
| cmd: "bash", | |
| args: ["-c", "bun -e 'console.log(\"hello\")'"], | |
| }); | |
| await handleCmdResult(verifyB); | |
| const timeSnapshot = (performance.now() - startB) / 1000; | |
| console.log(`Test B (snapshot): ${timeSnapshot.toFixed(2)}s`); | |
| await sandboxB.stop().catch(() => {}); | |
| // Results | |
| const diff = timeFresh - timeSnapshot; | |
| const speedup = timeFresh / timeSnapshot; | |
| console.log("\n========================================"); | |
| console.log("Benchmark Results"); | |
| console.log("========================================"); | |
| console.log(`Fresh: ${timeFresh.toFixed(2)}s`); | |
| console.log(`Snapshot: ${timeSnapshot.toFixed(2)}s`); | |
| console.log(`Diff: ${diff.toFixed(2)}s`); | |
| console.log(`Speedup: ${speedup.toFixed(2)}x`); | |
| console.log("========================================\n"); | |
| } | |
| if (values["create-snapshot"]) { | |
| await createSnapshot(); | |
| } else if (values.benchmark && typeof values.benchmark === "string") { | |
| await benchmark(values.benchmark); | |
| } else { | |
| console.error("Usage:"); | |
| console.error(" bun run bin/sandbox/sandbox.ts --create-snapshot"); | |
| console.error(" bun run bin/sandbox/sandbox.ts --benchmark <snapshotId>"); | |
| process.exit(1); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment