|
1 | 1 | import { spawnSync } from "node:child_process"; |
2 | 2 | import { appendFileSync, existsSync } from "node:fs"; |
3 | | -import { copyFile, mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; |
| 3 | +import { copyFile, cp, mkdir, mkdtemp, readdir, readFile, rm, writeFile } from "node:fs/promises"; |
4 | 4 | import { tmpdir } from "node:os"; |
5 | 5 | import { join, resolve } from "node:path"; |
6 | 6 | import { performance } from "node:perf_hooks"; |
7 | 7 | import { pathToFileURL } from "node:url"; |
| 8 | +import { restoreFixtureNames } from "../src/fixture-names.js"; |
| 9 | + |
| 10 | +const exampleGroups = new Set(["agents", "codemods", "drift", "gallery", "starters", "upgrade"]); |
| 11 | +const EXAMPLE_BUDGET_MS = 5000; |
| 12 | + |
| 13 | +async function directories(root: string): Promise<string[]> { |
| 14 | + return (await readdir(root, { withFileTypes: true })) |
| 15 | + .filter((entry) => entry.isDirectory() && !entry.name.startsWith(".")) |
| 16 | + .map((entry) => entry.name) |
| 17 | + .sort(); |
| 18 | +} |
| 19 | + |
| 20 | +export async function exampleRepositories(root: string): Promise<string[]> { |
| 21 | + const examples: string[] = []; |
| 22 | + for (const name of await directories(root)) { |
| 23 | + if (exampleGroups.has(name)) { |
| 24 | + examples.push(...(await directories(join(root, name))).map((child) => `${name}/${child}`)); |
| 25 | + } else { |
| 26 | + examples.push(name); |
| 27 | + } |
| 28 | + } |
| 29 | + return examples; |
| 30 | +} |
| 31 | + |
| 32 | +function timeExample(root: string, name: string, cli: string): { seconds: string; passed: boolean } { |
| 33 | + const start = performance.now(); |
| 34 | + const result = spawnSync(process.execPath, [cli], { |
| 35 | + cwd: join(root, name), |
| 36 | + env: { ...process.env, CI: "true", OFFING_NO_CACHE: "1" }, |
| 37 | + encoding: "utf8", |
| 38 | + timeout: EXAMPLE_BUDGET_MS, |
| 39 | + }); |
| 40 | + const duration = performance.now() - start; |
| 41 | + const passed = result.status === 0 && result.stdout.startsWith("Weather: ") && duration < EXAMPLE_BUDGET_MS; |
| 42 | + if (!passed) console.error(`${name}: ${result.error?.message ?? result.stderr}`); |
| 43 | + return { seconds: (duration / 1000).toFixed(3), passed }; |
| 44 | +} |
| 45 | + |
| 46 | +async function timeExamples(): Promise<void> { |
| 47 | + const root = await mkdtemp(join(tmpdir(), "offing-examples-first-run-")); |
| 48 | + try { |
| 49 | + await cp("examples", root, { recursive: true }); |
| 50 | + await restoreFixtureNames(root); |
| 51 | + const cli = resolve("dist/src/cli.js"); |
| 52 | + const rows = ["| Example repository | First run | Under 5 s |", "|---|---:|---|"]; |
| 53 | + for (const name of await exampleRepositories(root)) { |
| 54 | + const { seconds, passed } = timeExample(root, name, cli); |
| 55 | + rows.push(`| ${name} | ${seconds} s | ${passed ? "Pass" : "Fail"} |`); |
| 56 | + if (!passed) process.exitCode = 1; |
| 57 | + } |
| 58 | + console.log(rows.join("\n")); |
| 59 | + } finally { |
| 60 | + await rm(root, { recursive: true, force: true }); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +export async function cachedTarballBytes(cache: string): Promise<{ tarballs: number; bytes: number }> { |
| 65 | + // shortcut: npm's HTTP cache index is not a public API; revisit if npm changes its layout. |
| 66 | + const index = join(cache, "_cacache", "index-v5"); |
| 67 | + const tarballs = new Map<string, number>(); |
| 68 | + for (const entry of await readdir(index, { recursive: true, withFileTypes: true })) { |
| 69 | + if (!entry.isFile()) continue; |
| 70 | + const contents = await readFile(join(entry.parentPath, entry.name), "utf8"); |
| 71 | + for (const line of contents.trim().split("\n")) { |
| 72 | + if (!line) continue; |
| 73 | + const { key, size } = JSON.parse(line.slice(line.indexOf("\t") + 1)) as { key: string; size?: number }; |
| 74 | + if (key.startsWith("make-fetch-happen:request-cache:") && key.endsWith(".tgz") && size !== undefined) { |
| 75 | + tarballs.set(key, size); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + return { tarballs: tarballs.size, bytes: [...tarballs.values()].reduce((sum, size) => sum + size, 0) }; |
| 80 | +} |
| 81 | + |
| 82 | +async function measureNpxDownload(): Promise<void> { |
| 83 | + const root = await mkdtemp(join(tmpdir(), "offing-npx-first-run-")); |
| 84 | + try { |
| 85 | + const cache = join(root, "cache"); |
| 86 | + const result = spawnSync("npx", ["--yes", "--ignore-scripts", "@velella/offing", "--version"], { |
| 87 | + cwd: root, |
| 88 | + encoding: "utf8", |
| 89 | + env: { ...process.env, npm_config_cache: cache, npm_config_registry: "https://registry.npmjs.org" }, |
| 90 | + shell: process.platform === "win32", |
| 91 | + timeout: 180_000, |
| 92 | + }); |
| 93 | + if (result.status !== 0) throw new Error(result.error?.message ?? result.stderr); |
| 94 | + const { bytes, tarballs } = await cachedTarballBytes(cache); |
| 95 | + if (tarballs === 0) throw new Error("No downloaded tarballs were recorded in the clean npm cache."); |
| 96 | + console.log(`@velella/offing ${result.stdout.trim()}: ${bytes} compressed bytes across ${tarballs} tarballs.`); |
| 97 | + } finally { |
| 98 | + await rm(root, { recursive: true, force: true }); |
| 99 | + } |
| 100 | +} |
8 | 101 |
|
9 | 102 | /** Target budget for time to first useful finding: 2 minutes = 120,000 ms. */ |
10 | 103 | export const TWO_MINUTE_BUDGET_MS = 120_000; |
@@ -247,6 +340,8 @@ function appendMarkdownSummary(summaryPath: string, result: FirstRunBenchmarkRes |
247 | 340 | } |
248 | 341 |
|
249 | 342 | export async function runCli(argv = process.argv.slice(2)): Promise<void> { |
| 343 | + if (argv.length === 0 || argv[0] === "--examples") return timeExamples(); |
| 344 | + if (argv[0] === "--npx-size") return measureNpxDownload(); |
250 | 345 | let packageSpec: string | undefined; |
251 | 346 | let targetDir: string | undefined; |
252 | 347 | let useNpx = false; |
|
0 commit comments