db-perf.test.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**
  2. * DB Performance / Correctness Tests
  3. *
  4. * Regression tests for three changes:
  5. * 1. Batch `getNodesByIds` collapses graph-traversal N+1 reads.
  6. * 2. `insertNode` invalidates the LRU cache so INSERT OR REPLACE
  7. * doesn't serve a stale cached row on next `getNodeById`.
  8. * 3. `runMaintenance` runs `PRAGMA optimize` + `wal_checkpoint(PASSIVE)`
  9. * after indexAll/sync without throwing.
  10. * 4. `insertEdges` validates endpoints from the DB, not stale node cache.
  11. */
  12. import { describe, it, expect, beforeEach, afterEach } from 'vitest';
  13. import * as fs from 'fs';
  14. import * as path from 'path';
  15. import * as os from 'os';
  16. import { DatabaseConnection } from '../src/db';
  17. import { QueryBuilder } from '../src/db/queries';
  18. import { Node } from '../src/types';
  19. function makeNode(id: string, name = id): Node {
  20. return {
  21. id,
  22. kind: 'function',
  23. name,
  24. qualifiedName: name,
  25. filePath: 'a.ts',
  26. language: 'typescript',
  27. startLine: 1,
  28. endLine: 1,
  29. startColumn: 0,
  30. endColumn: 0,
  31. updatedAt: Date.now(),
  32. };
  33. }
  34. describe('getNodesByIds (batch lookup)', () => {
  35. let dir: string;
  36. let db: DatabaseConnection;
  37. let q: QueryBuilder;
  38. beforeEach(() => {
  39. dir = fs.mkdtempSync(path.join(os.tmpdir(), 'db-perf-batch-'));
  40. db = DatabaseConnection.initialize(path.join(dir, 'test.db'));
  41. q = new QueryBuilder(db.getDb());
  42. });
  43. afterEach(() => {
  44. db.close();
  45. if (fs.existsSync(dir)) fs.rmSync(dir, { recursive: true, force: true });
  46. });
  47. it('returns a Map keyed by id, with one entry per existing node', () => {
  48. q.insertNodes([makeNode('n1'), makeNode('n2'), makeNode('n3')]);
  49. const out = q.getNodesByIds(['n1', 'n2', 'n3']);
  50. expect(out.size).toBe(3);
  51. expect(out.get('n1')!.name).toBe('n1');
  52. expect(out.get('n3')!.name).toBe('n3');
  53. });
  54. it('omits missing IDs from the result map (no nulls, no exceptions)', () => {
  55. q.insertNodes([makeNode('n1'), makeNode('n2')]);
  56. const out = q.getNodesByIds(['n1', 'missing', 'n2']);
  57. expect(out.size).toBe(2);
  58. expect(out.has('missing')).toBe(false);
  59. expect(out.has('n1')).toBe(true);
  60. expect(out.has('n2')).toBe(true);
  61. });
  62. it('handles an empty input array', () => {
  63. expect(q.getNodesByIds([]).size).toBe(0);
  64. });
  65. it('handles batches over the SQLite parameter limit (chunking)', () => {
  66. // Insert 1500 nodes; the helper chunks at 500 internally.
  67. const nodes = Array.from({ length: 1500 }, (_, i) => makeNode(`n${i}`));
  68. q.insertNodes(nodes);
  69. const ids = nodes.map((n) => n.id);
  70. const out = q.getNodesByIds(ids);
  71. expect(out.size).toBe(1500);
  72. // Spot-check a few from the first / middle / last chunk.
  73. expect(out.has('n0')).toBe(true);
  74. expect(out.has('n750')).toBe(true);
  75. expect(out.has('n1499')).toBe(true);
  76. });
  77. it('serves cache hits from memory and queries only the misses', () => {
  78. q.insertNodes([makeNode('n1'), makeNode('n2'), makeNode('n3')]);
  79. // Warm the cache for n1 only.
  80. q.getNodeById('n1');
  81. // Replace the underlying row to make a miss-vs-cache-hit detectable.
  82. db.getDb().prepare('UPDATE nodes SET name = ? WHERE id = ?').run('changed', 'n1');
  83. const out = q.getNodesByIds(['n1', 'n2']);
  84. // The cached n1 (still 'n1', not 'changed') must be returned.
  85. expect(out.get('n1')!.name).toBe('n1');
  86. expect(out.get('n2')!.name).toBe('n2');
  87. });
  88. });
  89. describe('insertNode cache invalidation', () => {
  90. let dir: string;
  91. let db: DatabaseConnection;
  92. let q: QueryBuilder;
  93. beforeEach(() => {
  94. dir = fs.mkdtempSync(path.join(os.tmpdir(), 'db-perf-cache-'));
  95. db = DatabaseConnection.initialize(path.join(dir, 'test.db'));
  96. q = new QueryBuilder(db.getDb());
  97. });
  98. afterEach(() => {
  99. db.close();
  100. if (fs.existsSync(dir)) fs.rmSync(dir, { recursive: true, force: true });
  101. });
  102. it('does not serve a stale cached node after INSERT OR REPLACE', () => {
  103. // Regression: insertNode (which uses INSERT OR REPLACE) used to skip
  104. // cache invalidation, so the next getNodeById returned the pre-replace
  105. // version until LRU eviction.
  106. const original = makeNode('n1', 'oldName');
  107. q.insertNode(original);
  108. const beforeReplace = q.getNodeById('n1');
  109. expect(beforeReplace!.name).toBe('oldName');
  110. // Replace via insertNode (the bug path).
  111. q.insertNode({ ...original, name: 'newName', updatedAt: Date.now() });
  112. const afterReplace = q.getNodeById('n1');
  113. expect(afterReplace!.name).toBe('newName');
  114. });
  115. });
  116. describe('insertEdges endpoint validation', () => {
  117. let dir: string;
  118. let db: DatabaseConnection;
  119. let q: QueryBuilder;
  120. beforeEach(() => {
  121. dir = fs.mkdtempSync(path.join(os.tmpdir(), 'db-perf-edges-'));
  122. db = DatabaseConnection.initialize(path.join(dir, 'test.db'));
  123. q = new QueryBuilder(db.getDb());
  124. });
  125. afterEach(() => {
  126. db.close();
  127. if (fs.existsSync(dir)) fs.rmSync(dir, { recursive: true, force: true });
  128. });
  129. it('skips edges with missing endpoints instead of failing the whole batch', () => {
  130. q.insertNodes([makeNode('source'), makeNode('target'), makeNode('other')]);
  131. expect(() =>
  132. q.insertEdges([
  133. { source: 'source', target: 'target', kind: 'calls' },
  134. { source: 'source', target: 'missing-target', kind: 'calls' },
  135. { source: 'missing-source', target: 'other', kind: 'references' },
  136. ])
  137. ).not.toThrow();
  138. const edges = q.getOutgoingEdges('source');
  139. expect(edges).toHaveLength(1);
  140. expect(edges[0]).toMatchObject({ source: 'source', target: 'target', kind: 'calls' });
  141. });
  142. it('does not trust stale cached nodes when validating edge endpoints', () => {
  143. q.insertNodes([makeNode('source'), makeNode('target')]);
  144. expect(q.getNodeById('target')!.id).toBe('target');
  145. db.getDb().prepare('DELETE FROM nodes WHERE id = ?').run('target');
  146. expect(() =>
  147. q.insertEdges([{ source: 'source', target: 'target', kind: 'calls' }])
  148. ).not.toThrow();
  149. expect(q.getOutgoingEdges('source')).toEqual([]);
  150. });
  151. });
  152. describe('runMaintenance', () => {
  153. let dir: string;
  154. let db: DatabaseConnection;
  155. beforeEach(() => {
  156. dir = fs.mkdtempSync(path.join(os.tmpdir(), 'db-perf-maint-'));
  157. db = DatabaseConnection.initialize(path.join(dir, 'test.db'));
  158. });
  159. afterEach(() => {
  160. db.close();
  161. if (fs.existsSync(dir)) fs.rmSync(dir, { recursive: true, force: true });
  162. });
  163. it('runs without throwing on a fresh database', () => {
  164. expect(() => db.runMaintenance()).not.toThrow();
  165. });
  166. it('runs without throwing after writes', () => {
  167. const q = new QueryBuilder(db.getDb());
  168. q.insertNodes([makeNode('n1'), makeNode('n2')]);
  169. expect(() => db.runMaintenance()).not.toThrow();
  170. });
  171. it('swallows failures rather than propagating (best-effort)', () => {
  172. // Close the DB so the underlying handle would normally throw on any
  173. // exec(). runMaintenance must still not propagate.
  174. db.close();
  175. expect(() => db.runMaintenance()).not.toThrow();
  176. });
  177. });