resolver-pool-sizing.test.ts 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * Resolver-pool sizing (§7a.1 P1.2): cgroup-honest CPU term + memory-aware
  3. * cap + the CODEGRAPH_RESOLVE_WORKERS override. resolvePoolSize is pure —
  4. * these pin the whole decision matrix, including the two failure modes the
  5. * measurement round exposed: os.cpus() cpuset-blindness (6 workers inside a
  6. * 2-CPU container) and memory-blind sizing (six ~1GB workers OOM-killing a
  7. * 7GB container at true 8-core concurrency).
  8. */
  9. import { describe, it, expect } from 'vitest';
  10. import { ResolverPool } from '../src/resolution/resolver-pool';
  11. import { cgroupMemoryAvailable, memoryBudgetBytes } from '../src/resolution/memory-budget';
  12. const GB = 1024 * 1024 * 1024;
  13. const MB = 1024 * 1024;
  14. function size(opts: Partial<Parameters<typeof ResolverPool.resolvePoolSize>[0]>): number | null {
  15. return ResolverPool.resolvePoolSize({
  16. availableParallelism: 8,
  17. memoryBudget: 16 * GB,
  18. dbSizeBytes: 200 * MB,
  19. ...opts,
  20. });
  21. }
  22. describe('ResolverPool.resolvePoolSize', () => {
  23. it('big dev box: CPU-capped at the long-standing 6', () => {
  24. expect(size({})).toBe(6);
  25. expect(size({ availableParallelism: 11 })).toBe(6);
  26. });
  27. it('true 2-core box gets NO pool — sequential measured faster there (§7a.1: 853s vs 1150s)', () => {
  28. expect(size({ availableParallelism: 2, memoryBudget: 6 * GB })).toBeNull();
  29. expect(size({ availableParallelism: 3, memoryBudget: 6 * GB })).toBe(2);
  30. });
  31. it('kernel-scale DB in a 7GB container: memory term shrinks the pool below the OOM line', () => {
  32. // 4.6GB DB → ~940MB/worker estimate; 5.5GB headroom × 0.7 ≈ 3.85GB → 4 workers.
  33. const s = size({ availableParallelism: 8, memoryBudget: 5.5 * GB, dbSizeBytes: 4.6 * GB });
  34. expect(s).toBe(4);
  35. expect(s!).toBeLessThan(6);
  36. });
  37. it('per-worker estimate is floored (small DBs) and capped (huge DBs)', () => {
  38. // Small DB: floor 256MB/worker — memory cap = 16GB*0.7/256MB = 43 → CPU wins.
  39. expect(size({ dbSizeBytes: 10 * MB })).toBe(6);
  40. // Monster DB: cap 1.5GB/worker — 16GB*0.7/1.5GB = 7 → CPU still wins at 6.
  41. expect(size({ dbSizeBytes: 40 * GB })).toBe(6);
  42. // Same monster DB, tight memory: 4GB*0.7/1.5GB = 1 → below 2 → no pool.
  43. expect(size({ dbSizeBytes: 40 * GB, memoryBudget: 4 * GB })).toBeNull();
  44. });
  45. it('starved memory disables the pool entirely', () => {
  46. expect(size({ memoryBudget: 512 * MB, dbSizeBytes: 4 * GB })).toBeNull();
  47. });
  48. it('CODEGRAPH_RESOLVE_WORKERS overrides everything: 0 disables, values clamp at 16', () => {
  49. expect(size({ explicit: '0' })).toBeNull();
  50. expect(size({ explicit: '3', memoryBudget: 512 * MB })).toBe(3); // override skips the memory term
  51. expect(size({ explicit: '64' })).toBe(16);
  52. expect(size({ explicit: 'nonsense' })).toBe(6); // unparseable → computed path
  53. });
  54. });
  55. describe('memory budget helpers', () => {
  56. it('memoryBudgetBytes is positive and finite on every platform', () => {
  57. const b = memoryBudgetBytes();
  58. expect(b).toBeGreaterThan(0);
  59. expect(Number.isFinite(b)).toBe(true);
  60. });
  61. it('cgroupMemoryAvailable is null when uncontained (non-Linux) and never throws', () => {
  62. const v = cgroupMemoryAvailable();
  63. if (process.platform !== 'linux') {
  64. expect(v).toBeNull();
  65. } else {
  66. // Containerized CI: either uncontained (null) or a sane byte count.
  67. expect(v === null || (v >= 0 && Number.isFinite(v))).toBe(true);
  68. }
  69. });
  70. });