|
| 1 | +// Verifies the process probes: `ps` CPU/RSS on Unix, and Windows RSS via a CIM Win32_Process query. |
| 2 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 3 | + |
| 4 | +const spawnSyncMock = vi.hoisted(() => vi.fn()); |
| 5 | +vi.mock("node:child_process", () => ({ spawnSync: spawnSyncMock })); |
| 6 | + |
| 7 | +import { |
| 8 | + parseProcessRssKb, |
| 9 | + readProcessRssMb, |
| 10 | + readProcessTreeCpuMs, |
| 11 | +} from "../../scripts/lib/gateway-bench-probes.js"; |
| 12 | + |
| 13 | +const originalPlatform = process.platform; |
| 14 | + |
| 15 | +function setPlatform(platform: NodeJS.Platform): void { |
| 16 | + Object.defineProperty(process, "platform", { configurable: true, value: platform }); |
| 17 | +} |
| 18 | + |
| 19 | +function mockSpawn(result: { status: number | null; stdout: string }): void { |
| 20 | + spawnSyncMock.mockReturnValue(result); |
| 21 | +} |
| 22 | + |
| 23 | +afterEach(() => { |
| 24 | + Object.defineProperty(process, "platform", { configurable: true, value: originalPlatform }); |
| 25 | + spawnSyncMock.mockReset(); |
| 26 | +}); |
| 27 | + |
| 28 | +describe("parseProcessRssKb", () => { |
| 29 | + it("parses a positive integer", () => { |
| 30 | + expect(parseProcessRssKb(" 144080 \n")).toBe(144_080); |
| 31 | + }); |
| 32 | + |
| 33 | + it("rejects zero, negatives, and non-numeric values", () => { |
| 34 | + expect(parseProcessRssKb("0")).toBeNull(); |
| 35 | + expect(parseProcessRssKb("-5")).toBeNull(); |
| 36 | + expect(parseProcessRssKb("abc")).toBeNull(); |
| 37 | + expect(parseProcessRssKb("")).toBeNull(); |
| 38 | + }); |
| 39 | +}); |
| 40 | + |
| 41 | +describe("readProcessRssMb (Unix)", () => { |
| 42 | + it("converts `ps` RSS kilobytes to megabytes", () => { |
| 43 | + setPlatform("linux"); |
| 44 | + mockSpawn({ status: 0, stdout: "144080\n" }); |
| 45 | + expect(readProcessRssMb(1234)).toBeCloseTo(144_080 / 1024, 5); |
| 46 | + }); |
| 47 | + |
| 48 | + it("returns null when `ps` fails", () => { |
| 49 | + setPlatform("linux"); |
| 50 | + mockSpawn({ status: 1, stdout: "" }); |
| 51 | + expect(readProcessRssMb(1234)).toBeNull(); |
| 52 | + }); |
| 53 | + |
| 54 | + it("returns null for an invalid pid without spawning", () => { |
| 55 | + setPlatform("linux"); |
| 56 | + expect(readProcessRssMb(undefined)).toBeNull(); |
| 57 | + expect(readProcessRssMb(0)).toBeNull(); |
| 58 | + expect(spawnSyncMock).not.toHaveBeenCalled(); |
| 59 | + }); |
| 60 | +}); |
| 61 | + |
| 62 | +describe("readProcessTreeCpuMs (Unix)", () => { |
| 63 | + it("sums CPU time across the tree rooted at rootPid", () => { |
| 64 | + setPlatform("linux"); |
| 65 | + mockSpawn({ |
| 66 | + status: 0, |
| 67 | + stdout: ["100 1 00:10", "200 100 00:05", "300 100 01:00", "400 999 00:30"].join("\n"), |
| 68 | + }); |
| 69 | + // 100=10s + 200=5s + 300=60s; 400 belongs to a different parent and is excluded. |
| 70 | + expect(readProcessTreeCpuMs(100)).toBe(75_000); |
| 71 | + }); |
| 72 | + |
| 73 | + it("returns null when the root pid is absent", () => { |
| 74 | + setPlatform("linux"); |
| 75 | + mockSpawn({ status: 0, stdout: "200 100 00:05" }); |
| 76 | + expect(readProcessTreeCpuMs(999_999)).toBeNull(); |
| 77 | + }); |
| 78 | + |
| 79 | + it("returns null when `ps` fails", () => { |
| 80 | + setPlatform("linux"); |
| 81 | + mockSpawn({ status: 1, stdout: "" }); |
| 82 | + expect(readProcessTreeCpuMs(100)).toBeNull(); |
| 83 | + }); |
| 84 | +}); |
| 85 | + |
| 86 | +describe("readProcessRssMb (Windows)", () => { |
| 87 | + it("converts CIM WorkingSetSize bytes to megabytes", () => { |
| 88 | + setPlatform("win32"); |
| 89 | + mockSpawn({ status: 0, stdout: "144080896\n" }); |
| 90 | + expect(readProcessRssMb(1234)).toBeCloseTo(144_080_896 / (1024 * 1024), 5); |
| 91 | + }); |
| 92 | + |
| 93 | + it("returns null on empty or failed output", () => { |
| 94 | + setPlatform("win32"); |
| 95 | + mockSpawn({ status: 0, stdout: " " }); |
| 96 | + expect(readProcessRssMb(1234)).toBeNull(); |
| 97 | + mockSpawn({ status: 1, stdout: "" }); |
| 98 | + expect(readProcessRssMb(1234)).toBeNull(); |
| 99 | + }); |
| 100 | +}); |
| 101 | + |
| 102 | +describe("readProcessTreeCpuMs (Windows)", () => { |
| 103 | + it("is unavailable on Windows (startup CPU comes from the gateway ready trace)", () => { |
| 104 | + setPlatform("win32"); |
| 105 | + expect(readProcessTreeCpuMs(100)).toBeNull(); |
| 106 | + expect(spawnSyncMock).not.toHaveBeenCalled(); |
| 107 | + }); |
| 108 | +}); |
0 commit comments