|
| 1 | +import { spawnSync } from "node:child_process"; |
| 2 | +import { |
| 3 | + copyFileSync, |
| 4 | + mkdirSync, |
| 5 | + mkdtempSync, |
| 6 | + readFileSync, |
| 7 | + writeFileSync, |
| 8 | +} from "node:fs"; |
| 9 | +import { tmpdir } from "node:os"; |
| 10 | +import { join } from "node:path"; |
| 11 | +import { afterEach, describe, expect, it } from "vitest"; |
| 12 | +import { removeWithRetry } from "./helpers/remove-with-retry.js"; |
| 13 | + |
| 14 | +const tempRoots: string[] = []; |
| 15 | +const scriptPath = "scripts/benchmark-runtime-path.mjs"; |
| 16 | + |
| 17 | +afterEach(async () => { |
| 18 | + while (tempRoots.length > 0) { |
| 19 | + const root = tempRoots.pop(); |
| 20 | + if (root) { |
| 21 | + await removeWithRetry(root, { recursive: true, force: true }); |
| 22 | + } |
| 23 | + } |
| 24 | +}); |
| 25 | + |
| 26 | +function createRuntimeBenchmarkFixture(): { |
| 27 | + fixtureRoot: string; |
| 28 | + scriptCopy: string; |
| 29 | +} { |
| 30 | + const fixtureRoot = mkdtempSync(join(tmpdir(), "runtime-bench-fixture-")); |
| 31 | + tempRoots.push(fixtureRoot); |
| 32 | + |
| 33 | + const scriptsDir = join(fixtureRoot, "scripts"); |
| 34 | + const distRequestDir = join(fixtureRoot, "dist", "lib", "request"); |
| 35 | + const distHelpersDir = join(distRequestDir, "helpers"); |
| 36 | + const distLibDir = join(fixtureRoot, "dist", "lib"); |
| 37 | + |
| 38 | + mkdirSync(scriptsDir, { recursive: true }); |
| 39 | + mkdirSync(distHelpersDir, { recursive: true }); |
| 40 | + mkdirSync(distLibDir, { recursive: true }); |
| 41 | + |
| 42 | + const scriptCopy = join(scriptsDir, "benchmark-runtime-path.mjs"); |
| 43 | + copyFileSync(join(process.cwd(), scriptPath), scriptCopy); |
| 44 | + |
| 45 | + writeFileSync( |
| 46 | + join(distRequestDir, "request-transformer.js"), |
| 47 | + "export function filterInput(input) { return Array.isArray(input) ? input : []; }\n", |
| 48 | + "utf8", |
| 49 | + ); |
| 50 | + writeFileSync( |
| 51 | + join(distHelpersDir, "tool-utils.js"), |
| 52 | + "export function cleanupToolDefinitions(tools) { return Array.isArray(tools) ? tools : []; }\n", |
| 53 | + "utf8", |
| 54 | + ); |
| 55 | + writeFileSync( |
| 56 | + join(distLibDir, "accounts.js"), |
| 57 | + [ |
| 58 | + "export class AccountManager {", |
| 59 | + " constructor(_, storage) { this.storage = storage; }", |
| 60 | + " getCurrentOrNextForFamilyHybrid() { return this.storage.accounts[0] ?? null; }", |
| 61 | + "}", |
| 62 | + ].join("\n"), |
| 63 | + "utf8", |
| 64 | + ); |
| 65 | + |
| 66 | + return { fixtureRoot, scriptCopy }; |
| 67 | +} |
| 68 | + |
| 69 | +describe("benchmark runtime path script", () => { |
| 70 | + it("writes a benchmark payload with the expected result entries", () => { |
| 71 | + const { fixtureRoot, scriptCopy } = createRuntimeBenchmarkFixture(); |
| 72 | + const outputPath = join(fixtureRoot, "runtime-benchmark.json"); |
| 73 | + |
| 74 | + const result = spawnSync( |
| 75 | + process.execPath, |
| 76 | + [scriptCopy, "--iterations=1", `--output=${outputPath}`], |
| 77 | + { encoding: "utf8" }, |
| 78 | + ); |
| 79 | + |
| 80 | + expect(result.status).toBe(0); |
| 81 | + expect(result.stdout).toContain("Runtime benchmark written:"); |
| 82 | + |
| 83 | + const payload = JSON.parse(readFileSync(outputPath, "utf8")) as { |
| 84 | + iterations: number; |
| 85 | + results: Array<{ name: string }>; |
| 86 | + }; |
| 87 | + expect(payload.iterations).toBe(1); |
| 88 | + expect(payload.results.map((entry) => entry.name)).toEqual([ |
| 89 | + "filterInput_small", |
| 90 | + "filterInput_large", |
| 91 | + "cleanupToolDefinitions_medium", |
| 92 | + "cleanupToolDefinitions_large", |
| 93 | + "accountHybridSelection_200", |
| 94 | + ]); |
| 95 | + }); |
| 96 | +}); |
0 commit comments