| 1 | import assert from "node:assert/strict" |
| 2 | import type { Profiler, Runtime } from "node:inspector" |
| 3 | import { describe, it } from "node:test" |
| 4 | import { analyzeProfile, classify, shareInterval } from "../suites/schema-construction/profile.mts" |
| 5 | |
| 6 | const frame = (functionName: string, file = ""): Runtime.CallFrame => ({ |
| 7 | functionName, |
| 8 | url: file ? `file:///repo/packages/effect/${file}` : "", |
| 9 | scriptId: "1", |
| 10 | lineNumber: 0, |
| 11 | columnNumber: 0 |
| 12 | }) |
| 13 | const make = frame("make", "src/internal/schema/make.ts") |
| 14 | const sync = frame("make", "src/SchemaParser.ts") |
| 15 | const effect = frame("makeEffect", "src/SchemaParser.ts") |
| 16 | const option = frame("makeOption", "src/SchemaParser.ts") |
| 17 | const ast = frame("Objects", "src/SchemaAST.ts") |
| 18 | |
| 19 | describe("schema construction attribution", () => { |
| 20 | it("attributes the type traversal to the maker, without counting it again as AST construction", () => { |
| 21 | assert.equal(classify([ast, sync, make]), "type-projection") |
| 22 | assert.equal(classify([frame("", "src/Function.ts"), sync, make]), "type-projection") |
| 23 | assert.equal(classify([ast, make]), "type-projection") |
| 24 | assert.equal(classify([frame("", "src/Function.ts"), make]), "type-projection") |
| 25 | assert.equal(classify([ast, frame("Struct", "src/Schema.ts")]), "ast") |
| 26 | assert.equal(classify([sync, make]), "make-sync") |
| 27 | assert.equal(classify([frame("makeConstructorSync", "src/SchemaParser.ts"), sync, make]), "make-sync") |
| 28 | }) |
| 29 | it("keeps the second effect maker inside the Option adapter", () => { |
| 30 | assert.equal(classify([effect, option, make]), "make-option") |
| 31 | assert.equal(classify([effect, make]), "make-effect") |
| 32 | assert.equal(classify([make]), "schema-object") |
| 33 | }) |
| 34 | it("excludes profiler setup and includes GC only between construction samples", () => { |
| 35 | const node = (id: number, callFrame: Runtime.CallFrame, children: Array<number> = []): Profiler.ProfileNode => ({ |
| 36 | id, |
| 37 | callFrame, |
| 38 | children |
| 39 | }) |
| 40 | const profile: Profiler.Profile = { |
| 41 | startTime: 1000, |
| 42 | endTime: 2000, |
| 43 | nodes: [ |
| 44 | node(1, frame("(root)"), [2, 3, 4]), |
| 45 | node(2, frame("dispatch")), |
| 46 | node(3, frame("constructBatch", "runtimeperf/suites/schema-construction/worker.mts"), [5]), |
| 47 | node(4, frame("(garbage collector)")), |
| 48 | node(5, make) |
| 49 | ], |
| 50 | samples: [2, 4, 5, 4, 5, 2, 4], |
| 51 | timeDeltas: [100, 100, 100, 100, 100, 100, 100] |
| 52 | } |
| 53 | const result = analyzeProfile(profile) |
| 54 | assert.equal(result.totalSamples, 3) |
| 55 | assert.equal(result.excludedSamples, 4) |
| 56 | assert.equal(result.spanUs, 200) |
| 57 | assert.equal(result.counts["schema-object"], 2) |
| 58 | assert.equal(result.counts.gc, 1) |
| 59 | assert.equal(Object.values(result.counts).reduce((a, b) => a + b, 0), result.totalSamples) |
| 60 | assert.equal(analyzeProfile({ ...profile, samples: [2], timeDeltas: [100] }).totalSamples, 0) |
| 61 | }) |
| 62 | it("retains zero shares and uses reproducible process-level intervals", () => { |
| 63 | assert.deepEqual(shareInterval([0, 0], 100), { mean: 0, low: 0, high: 0 }) |
| 64 | assert.deepEqual(shareInterval([0.2, 0.5, 0.8], 100), shareInterval([0.2, 0.5, 0.8], 100)) |
| 65 | assert.equal(shareInterval([0, 1], 100).mean, 0.5) |
| 66 | assert.throws(() => shareInterval([]), /assert|false/i) |
| 67 | }) |
| 68 | }) |