| 1 | import assert from "node:assert/strict" |
| 2 | import { spawnSync } from "node:child_process" |
| 3 | import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs" |
| 4 | import { tmpdir } from "node:os" |
| 5 | import { join } from "node:path" |
| 6 | import { describe, it } from "node:test" |
| 7 | import { effectDir, workerPath } from "../utils.mts" |
| 8 | |
| 9 | describe("runtimeperf worker", () => { |
| 10 | it("measures one calibrated batch per minimum iteration and validates the final sink", () => { |
| 11 | const root = mkdtempSync(join(tmpdir(), "effect-runtimeperf-worker-")) |
| 12 | try { |
| 13 | const fixturePath = join(root, "fixture.mjs") |
| 14 | const validationsPath = join(root, "validations.txt") |
| 15 | writeFileSync( |
| 16 | fixturePath, |
| 17 | `import { appendFileSync } from "node:fs" |
| 18 | let count = 0 |
| 19 | export const runtimeCase = () => ({ |
| 20 | run: () => { |
| 21 | count++ |
| 22 | const end = process.hrtime.bigint() + 2_000_000n |
| 23 | while (process.hrtime.bigint() < end) {} |
| 24 | return count |
| 25 | }, |
| 26 | validate: (value) => appendFileSync(${JSON.stringify(validationsPath)}, \`\${value}\\n\`) |
| 27 | }) |
| 28 | ` |
| 29 | ) |
| 30 | const result = spawnSync(process.execPath, [ |
| 31 | workerPath, |
| 32 | "--mode", |
| 33 | "measure", |
| 34 | "--fixture", |
| 35 | fixturePath, |
| 36 | "--export", |
| 37 | "runtimeCase", |
| 38 | "--batch-size", |
| 39 | "3", |
| 40 | "--time-ms", |
| 41 | "1", |
| 42 | "--warmup-time-ms", |
| 43 | "1" |
| 44 | ], { |
| 45 | cwd: effectDir, |
| 46 | encoding: "utf8" |
| 47 | }) |
| 48 | assert.equal(result.status, 0, result.stderr) |
| 49 | const output = JSON.parse(result.stdout) |
| 50 | assert.equal(output.runs, 1) |
| 51 | assert.equal(output.batchSize, 3) |
| 52 | assert.deepEqual( |
| 53 | readFileSync(validationsPath, "utf8").trim().split("\n").map(Number), |
| 54 | [1, 7] |
| 55 | ) |
| 56 | } finally { |
| 57 | rmSync(root, { recursive: true, force: true }) |
| 58 | } |
| 59 | }) |
| 60 | }) |