resolver-pool-sizing.test.ts 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 keeps a 2-worker pool (the pooled-synthesis 2×)', () => {
  28. expect(size({ availableParallelism: 2, memoryBudget: 6 * GB })).toBe(2);
  29. });
  30. it('kernel-scale DB in a 7GB container: memory term shrinks the pool below the OOM line', () => {
  31. // 4.6GB DB → ~940MB/worker estimate; 5.5GB headroom × 0.7 ≈ 3.85GB → 4 workers.
  32. const s = size({ availableParallelism: 8, memoryBudget: 5.5 * GB, dbSizeBytes: 4.6 * GB });
  33. expect(s).toBe(4);
  34. expect(s!).toBeLessThan(6);
  35. });
  36. it('per-worker estimate is floored (small DBs) and capped (huge DBs)', () => {
  37. // Small DB: floor 256MB/worker — memory cap = 16GB*0.7/256MB = 43 → CPU wins.
  38. expect(size({ dbSizeBytes: 10 * MB })).toBe(6);
  39. // Monster DB: cap 1.5GB/worker — 16GB*0.7/1.5GB = 7 → CPU still wins at 6.
  40. expect(size({ dbSizeBytes: 40 * GB })).toBe(6);
  41. // Same monster DB, tight memory: 4GB*0.7/1.5GB = 1 → below 2 → no pool.
  42. expect(size({ dbSizeBytes: 40 * GB, memoryBudget: 4 * GB })).toBeNull();
  43. });
  44. it('starved memory disables the pool entirely', () => {
  45. expect(size({ memoryBudget: 512 * MB, dbSizeBytes: 4 * GB })).toBeNull();
  46. });
  47. it('CODEGRAPH_RESOLVE_WORKERS overrides everything: 0 disables, values clamp at 16', () => {
  48. expect(size({ explicit: '0' })).toBeNull();
  49. expect(size({ explicit: '3', memoryBudget: 512 * MB })).toBe(3); // override skips the memory term
  50. expect(size({ explicit: '64' })).toBe(16);
  51. expect(size({ explicit: 'nonsense' })).toBe(6); // unparseable → computed path
  52. });
  53. });
  54. describe('memory budget helpers', () => {
  55. it('memoryBudgetBytes is positive and finite on every platform', () => {
  56. const b = memoryBudgetBytes();
  57. expect(b).toBeGreaterThan(0);
  58. expect(Number.isFinite(b)).toBe(true);
  59. });
  60. it('cgroupMemoryAvailable is null when uncontained (non-Linux) and never throws', () => {
  61. const v = cgroupMemoryAvailable();
  62. if (process.platform !== 'linux') {
  63. expect(v).toBeNull();
  64. } else {
  65. // Containerized CI: either uncontained (null) or a sane byte count.
  66. expect(v === null || (v >= 0 && Number.isFinite(v))).toBe(true);
  67. }
  68. });
  69. });