diff --git a/test/unit/security-master-persist-benchmark-doc.test.ts b/test/unit/security-master-persist-benchmark-doc.test.ts new file mode 100644 index 00000000..2042fc23 --- /dev/null +++ b/test/unit/security-master-persist-benchmark-doc.test.ts @@ -0,0 +1,61 @@ +import { describe, expect, test } from 'bun:test'; +import { readFileSync } from 'node:fs'; +import { join } from 'node:path'; + +// Guards the recorded persistSecurityMaster benchmark baseline against drift: the README and the +// persist.ts module doc must quote the same measurement, and each quoted rows/s figure must equal +// rows / elapsed seconds for the documented workload. +const repoRoot = join(import.meta.dir, '..', '..'); +const readmePath = join(repoRoot, 'apps/stock/data-pipeline/README.md'); +const persistPath = join(repoRoot, 'apps/stock/data-pipeline/src/security-master/persist.ts'); +const benchmarkTestPath = join(repoRoot, 'test/postgres/security-master-persist.test.ts'); + +const CORE_ROWS = 100_000; // 20,000 instruments + 20,000 listings + 60,000 mappings +const TOTAL_ROWS = 140_000; // core + 20,000 identifier history + 20,000 symbol history + +function toNumber(text: string): number { + return Number(text.replace(/,/g, '')); +} + +function parseBaseline(doc: string) { + const wall = doc.match(/\(`?([\d.]+)ms/); + const core = doc.match(/([\d,]+)`? core rows\/s/); + const total = doc.match(/([\d,]+)`? total rows\/s/); + expect(wall).not.toBeNull(); + expect(core).not.toBeNull(); + expect(total).not.toBeNull(); + return { + elapsedMs: toNumber(wall![1]!), + coreRowsPerSec: toNumber(core![1]!), + totalRowsPerSec: toNumber(total![1]!), + }; +} + +describe('security-master persist benchmark baseline documentation', () => { + const readme = readFileSync(readmePath, 'utf8'); + const persistDoc = readFileSync(persistPath, 'utf8').split('*/')[0]!; + + test('README rows/s figures equal documented rows divided by recorded wall time', () => { + const { elapsedMs, coreRowsPerSec, totalRowsPerSec } = parseBaseline(readme); + + expect(coreRowsPerSec).toBe(Math.round(CORE_ROWS / (elapsedMs / 1000))); + expect(totalRowsPerSec).toBe(Math.round(TOTAL_ROWS / (elapsedMs / 1000))); + }); + + test('persist.ts module doc quotes the same baseline as the README', () => { + const fromReadme = parseBaseline(readme); + const fromDoc = parseBaseline(persistDoc); + + expect(fromDoc).toEqual(fromReadme); + }); + + test('benchmark result line is written to stdout, not the muted console.info', () => { + const source = readFileSync(benchmarkTestPath, 'utf8'); + const lineIndex = source.indexOf('[security-master-persist benchmark]'); + expect(lineIndex).toBeGreaterThan(-1); + + const preceding = source.slice(Math.max(0, lineIndex - 200), lineIndex); + expect(preceding).toContain('process.stdout.write('); + expect(preceding).not.toContain('console.info('); + }); +});