import { afterAll, expect, test } from 'vitest' import { getEntityTypesSQL } from '../../../src' import { explainAnalyze } from '../../db/plan-guard' import { buildStressCatalog } from '../../db/stress-catalog' import { cleanupRoot, createTestDatabase } from '../../db/utils' /** * Benchmark: does `enrich` cost scale with the page size or with the schema size? * Only runs when PG_META_BENCH=1, e.g. * PG_META_BENCH=1 PG_META_BENCH_SIZES=500,2000,8000 pnpm vitest run entities-enrich-bench */ const RUN = process.env.PG_META_BENCH === '1' const SIZES = (process.env.PG_META_BENCH_SIZES ?? '500,2000,8000').split(',').map(Number) const RUNS = 5 afterAll(async () => { await cleanupRoot() }) const median = (xs: number[]) => [...xs].sort((a, b) => a - b)[Math.floor(xs.length / 2)] test.skipIf(!RUN).each(SIZES)( 'enrich cost vs schema size (%i tables)', async (tableCount) => { const db = await createTestDatabase() try { await buildStressCatalog(db, tableCount) await db.executeQuery('analyze;') const time = async (limit: number, page: number, enrich: boolean) => { const sql = getEntityTypesSQL({ schemas: ['stress'], sort: 'alphabetical', filterTypes: ['r', 'v', 'm', 'f', 'p'], limit, page, enrich, }) await explainAnalyze(db, sql) // warm const runs: number[] = [] for (let i = 0; i < RUNS; i++) runs.push((await explainAnalyze(db, sql)).executionTimeMs) return median(runs) } const rows: Record[] = [] for (const [limit, page] of [ [50, 0], [50, 5], [200, 0], ] as const) { const off = await time(limit, page, false) const on = await time(limit, page, true) rows.push({ tables: tableCount, limit, page, 'off ms': +off.toFixed(1), 'on ms': +on.toFixed(1), 'delta ms': +(on - off).toFixed(1), }) } console.table(rows) expect(rows.length).toBeGreaterThan(0) } finally { await db.cleanup() } }, 600_000 )