|
1 | | -import { describe, it, expect, vi } from "vitest"; |
| 1 | +import { cpus, totalmem } from "os"; |
| 2 | +import { getHeapStatistics } from "v8"; |
| 3 | +import { afterEach, describe, it, expect, vi } from "vitest"; |
2 | 4 | import { |
3 | 5 | calculateOptimalWorkers, |
4 | 6 | computeWorkerSizing, |
@@ -128,7 +130,43 @@ describe("calculateOptimalWorkers", () => { |
128 | 130 | }); |
129 | 131 | }); |
130 | 132 |
|
| 133 | +vi.mock("os", async (importOriginal) => { |
| 134 | + const os = await importOriginal<typeof import("os")>(); |
| 135 | + return { ...os, default: os, cpus: vi.fn(os.cpus), totalmem: vi.fn(os.totalmem) }; |
| 136 | +}); |
| 137 | +vi.mock("v8", async (importOriginal) => { |
| 138 | + const v8 = await importOriginal<typeof import("v8")>(); |
| 139 | + return { ...v8, default: v8, getHeapStatistics: vi.fn(v8.getHeapStatistics) }; |
| 140 | +}); |
| 141 | + |
131 | 142 | describe("computeWorkerSizing", () => { |
| 143 | + afterEach(() => { |
| 144 | + vi.mocked(cpus).mockRestore(); |
| 145 | + vi.mocked(totalmem).mockRestore(); |
| 146 | + vi.mocked(getHeapStatistics).mockRestore(); |
| 147 | + }); |
| 148 | + |
| 149 | + // The field case: a 24GB 14-core Mac, Node's default ~4GB heap, a long 1080x1920 project. |
| 150 | + it("never auto-picks more workers than the V8 heap can feed", () => { |
| 151 | + vi.mocked(cpus).mockReturnValue( |
| 152 | + Array.from({ length: 14 }, () => ({}) as ReturnType<typeof cpus>[number]), |
| 153 | + ); |
| 154 | + vi.mocked(totalmem).mockReturnValue(24 * 1024 ** 3); |
| 155 | + vi.mocked(getHeapStatistics).mockReturnValue({ |
| 156 | + heap_size_limit: 4192 * 1024 ** 2, |
| 157 | + } as ReturnType<typeof getHeapStatistics>); |
| 158 | + const sizing = computeWorkerSizing(1300, undefined, { concurrency: "auto" }); |
| 159 | + expect(sizing.heapBasedWorkers).toBe(4); |
| 160 | + expect(sizing.workers).toBe(4); |
| 161 | + expect(sizing.boundBy).toBe("heap"); |
| 162 | + expect(sizing.exceedsHeapAdvisory).toBe(false); |
| 163 | + expect(computeWorkerSizing(1300, 6, { concurrency: "auto" }).workers).toBe(6); |
| 164 | + vi.mocked(getHeapStatistics).mockReturnValue({ |
| 165 | + heap_size_limit: 8192 * 1024 ** 2, |
| 166 | + } as ReturnType<typeof getHeapStatistics>); |
| 167 | + expect(computeWorkerSizing(1300, undefined, { concurrency: "auto" }).workers).toBe(5); |
| 168 | + }); |
| 169 | + |
132 | 170 | it("matches calculateOptimalWorkers and reports every constraint", () => { |
133 | 171 | const config = { concurrency: "auto" as const }; |
134 | 172 | const sizing = computeWorkerSizing(900, undefined, config); |
|
0 commit comments