GitNexus/gitnexus/test/integration/data-route-table-benchmark.test.ts
azizur100389 7f0ab16ffe
Some checks are pending
CodeQL / Analyze (javascript-typescript) (push) Waiting to run
CodeQL / Analyze (python) (push) Waiting to run
Gitleaks / gitleaks (push) Waiting to run
Publish / RC guard (marker + release-PR skip) (push) Blocked by required conditions
Publish / Classify release event (push) Waiting to run
Publish / ci (push) Blocked by required conditions
Publish / Publish to npm (push) Blocked by required conditions
Publish / Build & Push RC Docker images (push) Blocked by required conditions
Scorecard / Scorecard analysis (push) Waiting to run
Trivy Image Scan / Trivy (gitnexus-cli) (push) Waiting to run
Trivy Image Scan / Trivy (gitnexus-web) (push) Waiting to run
feat(routes): support JS data route tables (#2972)
2026-08-18 04:39:45 +01:00

68 lines
2.4 KiB
TypeScript

/**
* JavaScript data-route-table handler-stability scaling benchmark.
*
* The scanner used to walk the complete AST once for every route handler,
* making extraction quadratic as a route table grew. This benchmark parses
* outside the timed region and co-scales unique handlers and route entries so
* the measured work isolates scanDataRouteTables.
*
* Run: GITNEXUS_BENCH=1 npx vitest run test/integration/data-route-table-benchmark.test.ts
*/
import { describe, expect, it } from 'vitest';
import Parser from 'tree-sitter';
import JavaScript from 'tree-sitter-javascript';
import { scanDataRouteTables } from '../../src/core/ingestion/route-extractors/data-route-table.js';
const BENCH_ENABLED = process.env.GITNEXUS_BENCH === '1';
const parser = new Parser();
parser.setLanguage(JavaScript);
interface BenchResult {
routes: number;
elapsedMs: number;
}
function fixture(routeCount: number): string {
const handlers = Array.from({ length: routeCount }, (_, i) => `function handler${i}() {}`).join(
'\n',
);
const entries = Array.from(
{ length: routeCount },
(_, i) => ` { path: '/route-${i}', method: 'GET', handler: handler${i} },`,
).join('\n');
return `${handlers}
const routes = [
${entries}
];
for (const route of routes) {
if (route.path === request.path && route.method === request.method) route.handler();
}`;
}
function benchmark(routeCount: number): BenchResult {
const tree = parser.parse(fixture(routeCount));
const started = performance.now();
const routes = scanDataRouteTables(tree);
const elapsedMs = performance.now() - started;
expect(routes).toHaveLength(routeCount);
return { routes: routeCount, elapsedMs };
}
describe.skipIf(!BENCH_ENABLED)('data-route-table scanner benchmark', () => {
it('scales sub-quadratically as handlers and route entries grow together', () => {
benchmark(10);
const small = benchmark(50);
const large = benchmark(200);
const routeRatio = large.routes / small.routes;
console.log('\nJavaScript data-route-table scanner benchmark');
console.log(` routes=${small.routes} wall=${small.elapsedMs.toFixed(2)}ms`);
console.log(` routes=${large.routes} wall=${large.elapsedMs.toFixed(2)}ms`);
if (small.elapsedMs >= 5) {
expect(large.elapsedMs / small.elapsedMs).toBeLessThan(Math.pow(routeRatio, 1.5));
}
expect(large.elapsedMs).toBeLessThan(5_000);
}, 120_000);
});