|
| 1 | +// debug-terraform-e2b.ts |
| 2 | +// |
| 3 | +// Run a 10k-null Terraform apply inside an E2B sandbox with |
| 4 | +// *no* stdout/stderr callbacks, just final output. |
| 5 | +// |
| 6 | +// Usage: |
| 7 | +// cd sandbox-sidecar |
| 8 | +// npx tsx scripts/debug-terraform-e2b.ts |
| 9 | +// |
| 10 | +// This isolates whether the slowness is: |
| 11 | +// A) E2B's virtualization/environment |
| 12 | +// B) Our sidecar's SDK callback overhead |
| 13 | +// C) Our log handling code |
| 14 | + |
| 15 | +import "dotenv/config"; |
| 16 | +import { Sandbox } from "@e2b/code-interpreter"; |
| 17 | + |
| 18 | +// Use our pre-built template with terraform + providers cached |
| 19 | +const TEMPLATE_ID = "terraform-1-5-7--tpl-0-2-2"; |
| 20 | +const WORK_DIR = "/home/user/benchmark"; |
| 21 | + |
| 22 | +// The benchmark Terraform config - 10k null resources |
| 23 | +const MAIN_TF = ` |
| 24 | +# Benchmark: 10,000 Null Resources |
| 25 | +# Purpose: Test performance with large number of resources |
| 26 | +
|
| 27 | +terraform { |
| 28 | + required_providers { |
| 29 | + null = { |
| 30 | + source = "hashicorp/null" |
| 31 | + version = "~> 3.0" |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | +
|
| 36 | +resource "null_resource" "massive" { |
| 37 | + count = 10000 |
| 38 | +
|
| 39 | + triggers = { |
| 40 | + index = count.index |
| 41 | + } |
| 42 | +} |
| 43 | +`; |
| 44 | + |
| 45 | +async function main() { |
| 46 | + const apiKey = process.env.E2B_API_KEY; |
| 47 | + if (!apiKey) { |
| 48 | + console.error("E2B_API_KEY environment variable is required"); |
| 49 | + console.error("Set it in .env or export E2B_API_KEY=..."); |
| 50 | + process.exit(1); |
| 51 | + } |
| 52 | + |
| 53 | + console.log("=".repeat(60)); |
| 54 | + console.log("E2B Terraform Performance Debug Script"); |
| 55 | + console.log("=".repeat(60)); |
| 56 | + console.log(`Template: ${TEMPLATE_ID}`); |
| 57 | + console.log(`Resources: 10,000 null_resource`); |
| 58 | + console.log("=".repeat(60)); |
| 59 | + |
| 60 | + console.log("\n[1/6] Creating E2B sandbox..."); |
| 61 | + const startCreate = Date.now(); |
| 62 | + |
| 63 | + const sandbox = await Sandbox.create(TEMPLATE_ID, { |
| 64 | + apiKey, |
| 65 | + timeoutMs: 30 * 60 * 1000, // 30 minutes |
| 66 | + }); |
| 67 | + |
| 68 | + const createTime = Date.now() - startCreate; |
| 69 | + console.log(`Sandbox created: ${sandbox.sandboxId}`); |
| 70 | + console.log(`Creation time: ${createTime}ms`); |
| 71 | + |
| 72 | + try { |
| 73 | + // 2) Create the benchmark directory and write main.tf |
| 74 | + console.log("\n[2/6] Creating benchmark terraform config..."); |
| 75 | + await sandbox.commands.run(`mkdir -p ${WORK_DIR}`); |
| 76 | + await sandbox.files.write(`${WORK_DIR}/main.tf`, MAIN_TF); |
| 77 | + |
| 78 | + let result = await sandbox.commands.run(`cat ${WORK_DIR}/main.tf`); |
| 79 | + console.log("Created main.tf:"); |
| 80 | + console.log(result.stdout); |
| 81 | + |
| 82 | + // 3) Run terraform init |
| 83 | + console.log("\n[3/6] Running terraform init..."); |
| 84 | + const startInit = Date.now(); |
| 85 | + result = await sandbox.commands.run( |
| 86 | + `cd ${WORK_DIR} && terraform init -input=false -no-color -plugin-dir=/usr/share/terraform/providers`, |
| 87 | + { timeoutMs: 300000 } |
| 88 | + ); |
| 89 | + const initTime = Date.now() - startInit; |
| 90 | + console.log(`Init time: ${initTime}ms`); |
| 91 | + console.log(result.stdout.slice(-500)); |
| 92 | + if (result.exitCode !== 0) { |
| 93 | + console.error("terraform init failed:", result.stderr); |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + // 4) TIME TEST: Run apply WITHOUT streaming callbacks |
| 98 | + console.log("\n[4/6] Running terraform apply (NO streaming callbacks)..."); |
| 99 | + console.log("This will take a while. No output until complete."); |
| 100 | + console.log("Started at:", new Date().toISOString()); |
| 101 | + |
| 102 | + const startApply = Date.now(); |
| 103 | + result = await sandbox.commands.run( |
| 104 | + `cd ${WORK_DIR} && terraform apply -auto-approve -input=false -no-color -parallelism=30`, |
| 105 | + { |
| 106 | + timeoutMs: 60 * 60 * 1000, // 1 hour |
| 107 | + // NO onStdout/onStderr callbacks! |
| 108 | + } |
| 109 | + ); |
| 110 | + const applyTime = Date.now() - startApply; |
| 111 | + |
| 112 | + console.log(`\nApply completed at: ${new Date().toISOString()}`); |
| 113 | + console.log(`Apply time: ${applyTime}ms (${(applyTime/1000).toFixed(1)}s) = ${(applyTime/60000).toFixed(2)} minutes`); |
| 114 | + console.log(`\nLast 500 chars of output:`); |
| 115 | + console.log(result.stdout.slice(-500)); |
| 116 | + |
| 117 | + if (result.exitCode !== 0) { |
| 118 | + console.error("terraform apply failed:", result.stderr); |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + // 5) Destroy and re-apply with output redirected to file |
| 123 | + console.log("\n[5/6] Destroying resources for second test..."); |
| 124 | + const startDestroy = Date.now(); |
| 125 | + await sandbox.commands.run( |
| 126 | + `cd ${WORK_DIR} && terraform destroy -auto-approve -input=false -no-color -parallelism=30 > /tmp/destroy.log 2>&1`, |
| 127 | + { timeoutMs: 60 * 60 * 1000 } |
| 128 | + ); |
| 129 | + const destroyTime = Date.now() - startDestroy; |
| 130 | + console.log(`Destroy time: ${destroyTime}ms (${(destroyTime/1000).toFixed(1)}s)`); |
| 131 | + |
| 132 | + // 6) Apply with file redirect - completely bypasses SDK stdout handling |
| 133 | + console.log("\n[6/6] Running apply with output redirected to file..."); |
| 134 | + console.log("(This tests if SDK stdout collection has overhead)"); |
| 135 | + const startApplyFile = Date.now(); |
| 136 | + result = await sandbox.commands.run( |
| 137 | + `cd ${WORK_DIR} && terraform apply -auto-approve -input=false -no-color -parallelism=30 > /tmp/apply.log 2>&1; echo "EXIT_CODE=$?"`, |
| 138 | + { timeoutMs: 60 * 60 * 1000 } |
| 139 | + ); |
| 140 | + const applyFileTime = Date.now() - startApplyFile; |
| 141 | + |
| 142 | + console.log(`Apply (file redirect) time: ${applyFileTime}ms (${(applyFileTime/1000).toFixed(1)}s) = ${(applyFileTime/60000).toFixed(2)} minutes`); |
| 143 | + |
| 144 | + // Read last part of log |
| 145 | + result = await sandbox.commands.run(`tail -10 /tmp/apply.log`); |
| 146 | + console.log("Last 10 lines of apply.log:"); |
| 147 | + console.log(result.stdout); |
| 148 | + |
| 149 | + // Summary |
| 150 | + console.log("\n" + "=".repeat(60)); |
| 151 | + console.log("SUMMARY"); |
| 152 | + console.log("=".repeat(60)); |
| 153 | + console.log(`Sandbox creation: ${createTime}ms (${(createTime/1000).toFixed(1)}s)`); |
| 154 | + console.log(`Terraform init: ${initTime}ms (${(initTime/1000).toFixed(1)}s)`); |
| 155 | + console.log(`Apply (SDK collects): ${applyTime}ms (${(applyTime/1000).toFixed(1)}s) = ${(applyTime/60000).toFixed(2)} min`); |
| 156 | + console.log(`Apply (file redirect): ${applyFileTime}ms (${(applyFileTime/1000).toFixed(1)}s) = ${(applyFileTime/60000).toFixed(2)} min`); |
| 157 | + console.log(`Destroy: ${destroyTime}ms (${(destroyTime/1000).toFixed(1)}s)`); |
| 158 | + console.log("=".repeat(60)); |
| 159 | + |
| 160 | + console.log("\nDIAGNOSIS:"); |
| 161 | + if (applyTime > 600000) { // > 10 minutes |
| 162 | + console.log("❌ Apply took > 10 minutes."); |
| 163 | + console.log(" The E2B environment itself is slow."); |
| 164 | + console.log(" This is NOT a sidecar code issue - it's E2B's VM performance."); |
| 165 | + console.log(" Options:"); |
| 166 | + console.log(" - Contact E2B about VM performance"); |
| 167 | + console.log(" - Try a different sandbox provider"); |
| 168 | + console.log(" - Accept this as the baseline for E2B"); |
| 169 | + } else if (applyTime > 300000) { // > 5 minutes |
| 170 | + console.log("⚠️ Apply took 5-10 minutes."); |
| 171 | + console.log(" This is similar to Spacelift's performance."); |
| 172 | + console.log(" E2B is comparable but not faster."); |
| 173 | + } else if (applyTime > 60000) { // > 1 minute |
| 174 | + console.log("🟡 Apply took 1-5 minutes."); |
| 175 | + console.log(" E2B is reasonably fast."); |
| 176 | + console.log(" Our sidecar overhead might be adding to this."); |
| 177 | + } else { |
| 178 | + console.log("✅ Apply took < 1 minute!"); |
| 179 | + console.log(" E2B is fast - any slowness is in our sidecar code."); |
| 180 | + } |
| 181 | + |
| 182 | + const sdkOverhead = applyTime - applyFileTime; |
| 183 | + if (Math.abs(sdkOverhead) > 10000) { // > 10 second difference |
| 184 | + console.log(`\n📊 SDK stdout collection overhead: ${(sdkOverhead/1000).toFixed(1)}s`); |
| 185 | + } |
| 186 | + |
| 187 | + } finally { |
| 188 | + console.log("\nKilling sandbox..."); |
| 189 | + await sandbox.kill(); |
| 190 | + console.log("Done."); |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +main().catch((err) => { |
| 195 | + console.error(err); |
| 196 | + process.exit(1); |
| 197 | +}); |
| 198 | + |
0 commit comments