|
| 1 | +const viteArgs = process.argv.slice(2); |
| 2 | +const host = "127.0.0.1"; |
| 3 | +const port = 3020; |
| 4 | +const start = performance.now(); |
| 5 | + |
| 6 | +const child = Bun.spawn({ |
| 7 | + cmd: ["bun", "run", "dev", "--", "--host", host, "--port", String(port), ...viteArgs], |
| 8 | + cwd: process.cwd(), |
| 9 | + stdin: "ignore", |
| 10 | + stdout: "pipe", |
| 11 | + stderr: "pipe", |
| 12 | + env: process.env, |
| 13 | +}); |
| 14 | + |
| 15 | +let ready = false; |
| 16 | +let stdout = ""; |
| 17 | +let stderr = ""; |
| 18 | + |
| 19 | +async function collect(readable: ReadableStream<Uint8Array>, target: "stdout" | "stderr") { |
| 20 | + const decoder = new TextDecoder(); |
| 21 | + |
| 22 | + for await (const chunk of readable) { |
| 23 | + const text = decoder.decode(chunk, { stream: true }); |
| 24 | + |
| 25 | + if (target === "stdout") { |
| 26 | + stdout += text; |
| 27 | + if (stdout.includes("Local:")) { |
| 28 | + ready = true; |
| 29 | + } |
| 30 | + } else { |
| 31 | + stderr += text; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + const tail = decoder.decode(); |
| 36 | + if (!tail) return; |
| 37 | + |
| 38 | + if (target === "stdout") { |
| 39 | + stdout += tail; |
| 40 | + if (stdout.includes("Local:")) { |
| 41 | + ready = true; |
| 42 | + } |
| 43 | + } else { |
| 44 | + stderr += tail; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +const stdoutTask = collect(child.stdout, "stdout"); |
| 49 | +const stderrTask = collect(child.stderr, "stderr"); |
| 50 | + |
| 51 | +async function waitForReady(timeoutMs: number) { |
| 52 | + const start = performance.now(); |
| 53 | + |
| 54 | + while (!ready) { |
| 55 | + if (child.exitCode !== null) { |
| 56 | + throw new Error(`vite dev exited early with code ${child.exitCode}`); |
| 57 | + } |
| 58 | + if (performance.now() - start > timeoutMs) { |
| 59 | + throw new Error("timed out waiting for vite dev"); |
| 60 | + } |
| 61 | + await Bun.sleep(100); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +async function request(pathname: string) { |
| 66 | + const requestStart = performance.now(); |
| 67 | + const response = await fetch(`http://${host}:${port}${pathname}`); |
| 68 | + await response.arrayBuffer(); |
| 69 | + |
| 70 | + return { |
| 71 | + pathname, |
| 72 | + status: response.status, |
| 73 | + ms: Number((performance.now() - requestStart).toFixed(2)), |
| 74 | + }; |
| 75 | +} |
| 76 | + |
| 77 | +function formatDuration(ms: number) { |
| 78 | + return `${(ms / 1000).toFixed(2)}s`; |
| 79 | +} |
| 80 | + |
| 81 | +function printTable( |
| 82 | + rows: Array<{ |
| 83 | + step: string; |
| 84 | + durationMs: number; |
| 85 | + status: string; |
| 86 | + }>, |
| 87 | +) { |
| 88 | + const headers = ["Step", "Duration", "Status"]; |
| 89 | + const body = rows.map((row) => [row.step, formatDuration(row.durationMs), row.status]); |
| 90 | + const widths = headers.map((header, index) => |
| 91 | + Math.max(header.length, ...body.map((row) => row[index].length)), |
| 92 | + ); |
| 93 | + |
| 94 | + const formatRow = (row: string[]) => |
| 95 | + row.map((cell, index) => cell.padEnd(widths[index])).join(" "); |
| 96 | + |
| 97 | + console.log(formatRow(headers)); |
| 98 | + console.log(widths.map((width) => "-".repeat(width)).join(" ")); |
| 99 | + for (const row of body) { |
| 100 | + console.log(formatRow(row)); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +try { |
| 105 | + await waitForReady(30_000); |
| 106 | + const readyMs = Number((performance.now() - start).toFixed(2)); |
| 107 | + |
| 108 | + const docs = await request("/docs/"); |
| 109 | + const totalToDocsMs = Number((performance.now() - start).toFixed(2)); |
| 110 | + const anotherPage = await request("/docs/support/dashboard"); |
| 111 | + const totalMs = Number((performance.now() - start).toFixed(2)); |
| 112 | + |
| 113 | + if (docs.status >= 400 || anotherPage.status >= 400) { |
| 114 | + console.error("vite dev benchmark hit an error response"); |
| 115 | + if (stdout) console.error(stdout.trim()); |
| 116 | + if (stderr) console.error(stderr.trim()); |
| 117 | + process.exitCode = 1; |
| 118 | + } |
| 119 | + |
| 120 | + printTable([ |
| 121 | + { step: "Dev ready", durationMs: readyMs, status: "-" }, |
| 122 | + { step: "First /docs/ request", durationMs: docs.ms, status: String(docs.status) }, |
| 123 | + { step: "Start -> first /docs/", durationMs: totalToDocsMs, status: String(docs.status) }, |
| 124 | + { step: "Second docs page", durationMs: anotherPage.ms, status: String(anotherPage.status) }, |
| 125 | + { step: "Full benchmark", durationMs: totalMs, status: "-" }, |
| 126 | + ]); |
| 127 | +} finally { |
| 128 | + child.kill("SIGTERM"); |
| 129 | + await child.exited; |
| 130 | + await stdoutTask; |
| 131 | + await stderrTask; |
| 132 | +} |
0 commit comments