|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Incremental build benchmark — measures build tiers and import resolution. |
| 5 | + * |
| 6 | + * Measures full build, no-op rebuild, and single-file rebuild for both |
| 7 | + * native and WASM engines. Also benchmarks import resolution throughput: |
| 8 | + * native batch vs JS fallback. |
| 9 | + * |
| 10 | + * Usage: node scripts/incremental-benchmark.js > result.json |
| 11 | + */ |
| 12 | + |
| 13 | +import fs from 'node:fs'; |
| 14 | +import path from 'node:path'; |
| 15 | +import { performance } from 'node:perf_hooks'; |
| 16 | +import { fileURLToPath, pathToFileURL } from 'node:url'; |
| 17 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 18 | +const root = path.resolve(__dirname, '..'); |
| 19 | + |
| 20 | +const pkg = JSON.parse(fs.readFileSync(path.join(root, 'package.json'), 'utf8')); |
| 21 | +const dbPath = path.join(root, '.codegraph', 'graph.db'); |
| 22 | + |
| 23 | +const { buildGraph } = await import(pathToFileURL(path.join(root, 'src', 'builder.js')).href); |
| 24 | +const { statsData } = await import(pathToFileURL(path.join(root, 'src', 'queries.js')).href); |
| 25 | +const { resolveImportPath, resolveImportsBatch, resolveImportPathJS } = await import( |
| 26 | + pathToFileURL(path.join(root, 'src', 'resolve.js')).href |
| 27 | +); |
| 28 | +const { isNativeAvailable } = await import( |
| 29 | + pathToFileURL(path.join(root, 'src', 'native.js')).href |
| 30 | +); |
| 31 | + |
| 32 | +// Redirect console.log to stderr so only JSON goes to stdout |
| 33 | +const origLog = console.log; |
| 34 | +console.log = (...args) => console.error(...args); |
| 35 | + |
| 36 | +const RUNS = 3; |
| 37 | +const PROBE_FILE = path.join(root, 'src', 'queries.js'); |
| 38 | + |
| 39 | +function median(arr) { |
| 40 | + const sorted = [...arr].sort((a, b) => a - b); |
| 41 | + const mid = Math.floor(sorted.length / 2); |
| 42 | + return sorted.length % 2 ? sorted[mid] : (sorted[mid - 1] + sorted[mid]) / 2; |
| 43 | +} |
| 44 | + |
| 45 | +function round1(n) { |
| 46 | + return Math.round(n * 10) / 10; |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Benchmark build tiers for a given engine. |
| 51 | + */ |
| 52 | +async function benchmarkBuildTiers(engine) { |
| 53 | + // Full build (delete DB first) |
| 54 | + const fullTimings = []; |
| 55 | + for (let i = 0; i < RUNS; i++) { |
| 56 | + if (fs.existsSync(dbPath)) fs.unlinkSync(dbPath); |
| 57 | + const start = performance.now(); |
| 58 | + await buildGraph(root, { engine, incremental: false }); |
| 59 | + fullTimings.push(performance.now() - start); |
| 60 | + } |
| 61 | + const fullBuildMs = Math.round(median(fullTimings)); |
| 62 | + |
| 63 | + // No-op rebuild (nothing changed) |
| 64 | + const noopTimings = []; |
| 65 | + for (let i = 0; i < RUNS; i++) { |
| 66 | + const start = performance.now(); |
| 67 | + await buildGraph(root, { engine, incremental: true }); |
| 68 | + noopTimings.push(performance.now() - start); |
| 69 | + } |
| 70 | + const noopRebuildMs = Math.round(median(noopTimings)); |
| 71 | + |
| 72 | + // 1-file change rebuild |
| 73 | + const original = fs.readFileSync(PROBE_FILE, 'utf8'); |
| 74 | + let oneFileRebuildMs; |
| 75 | + try { |
| 76 | + const oneFileTimings = []; |
| 77 | + for (let i = 0; i < RUNS; i++) { |
| 78 | + fs.writeFileSync(PROBE_FILE, original + `\n// probe-${i}\n`); |
| 79 | + const start = performance.now(); |
| 80 | + await buildGraph(root, { engine, incremental: true }); |
| 81 | + oneFileTimings.push(performance.now() - start); |
| 82 | + } |
| 83 | + oneFileRebuildMs = Math.round(median(oneFileTimings)); |
| 84 | + } finally { |
| 85 | + fs.writeFileSync(PROBE_FILE, original); |
| 86 | + // One final incremental build to restore DB state |
| 87 | + await buildGraph(root, { engine, incremental: true }); |
| 88 | + } |
| 89 | + |
| 90 | + return { fullBuildMs, noopRebuildMs, oneFileRebuildMs }; |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * Collect all import pairs by scanning source files for ES import statements. |
| 95 | + */ |
| 96 | +function collectImportPairs() { |
| 97 | + const srcDir = path.join(root, 'src'); |
| 98 | + const files = fs.readdirSync(srcDir).filter((f) => f.endsWith('.js')); |
| 99 | + const importRe = /(?:^|\n)\s*import\s+.*?\s+from\s+['"]([^'"]+)['"]/g; |
| 100 | + |
| 101 | + const pairs = []; |
| 102 | + for (const file of files) { |
| 103 | + const absFile = path.join(srcDir, file); |
| 104 | + const content = fs.readFileSync(absFile, 'utf8'); |
| 105 | + let match; |
| 106 | + while ((match = importRe.exec(content)) !== null) { |
| 107 | + pairs.push({ fromFile: absFile, importSource: match[1] }); |
| 108 | + } |
| 109 | + } |
| 110 | + return pairs; |
| 111 | +} |
| 112 | + |
| 113 | +/** |
| 114 | + * Benchmark import resolution: native batch vs JS fallback. |
| 115 | + */ |
| 116 | +function benchmarkResolve(inputs) { |
| 117 | + const aliases = null; // codegraph itself has no path aliases |
| 118 | + |
| 119 | + // Native batch |
| 120 | + let nativeBatchMs = null; |
| 121 | + let perImportNativeMs = null; |
| 122 | + if (isNativeAvailable()) { |
| 123 | + const timings = []; |
| 124 | + for (let i = 0; i < RUNS; i++) { |
| 125 | + const start = performance.now(); |
| 126 | + resolveImportsBatch(inputs, root, aliases); |
| 127 | + timings.push(performance.now() - start); |
| 128 | + } |
| 129 | + nativeBatchMs = round1(median(timings)); |
| 130 | + perImportNativeMs = inputs.length > 0 ? round1(nativeBatchMs / inputs.length) : 0; |
| 131 | + } |
| 132 | + |
| 133 | + // JS fallback (call the exported JS implementation) |
| 134 | + const jsTimings = []; |
| 135 | + for (let i = 0; i < RUNS; i++) { |
| 136 | + const start = performance.now(); |
| 137 | + for (const { fromFile, importSource } of inputs) { |
| 138 | + resolveImportPathJS(fromFile, importSource, root, aliases); |
| 139 | + } |
| 140 | + jsTimings.push(performance.now() - start); |
| 141 | + } |
| 142 | + const jsFallbackMs = round1(median(jsTimings)); |
| 143 | + const perImportJsMs = inputs.length > 0 ? round1(jsFallbackMs / inputs.length) : 0; |
| 144 | + |
| 145 | + return { |
| 146 | + imports: inputs.length, |
| 147 | + nativeBatchMs, |
| 148 | + jsFallbackMs, |
| 149 | + perImportNativeMs, |
| 150 | + perImportJsMs, |
| 151 | + }; |
| 152 | +} |
| 153 | + |
| 154 | +// ── Run benchmarks ─────────────────────────────────────────────────────── |
| 155 | + |
| 156 | +console.error('Benchmarking WASM engine...'); |
| 157 | +const wasm = await benchmarkBuildTiers('wasm'); |
| 158 | +console.error(` full=${wasm.fullBuildMs}ms noop=${wasm.noopRebuildMs}ms 1-file=${wasm.oneFileRebuildMs}ms`); |
| 159 | + |
| 160 | +// Get file count from the WASM-built graph |
| 161 | +const stats = statsData(dbPath); |
| 162 | +const files = stats.files.total; |
| 163 | + |
| 164 | +let native = null; |
| 165 | +if (isNativeAvailable()) { |
| 166 | + console.error('Benchmarking native engine...'); |
| 167 | + native = await benchmarkBuildTiers('native'); |
| 168 | + console.error(` full=${native.fullBuildMs}ms noop=${native.noopRebuildMs}ms 1-file=${native.oneFileRebuildMs}ms`); |
| 169 | +} else { |
| 170 | + console.error('Native engine not available — skipping native build benchmark'); |
| 171 | +} |
| 172 | + |
| 173 | +// Import resolution benchmark (uses existing graph) |
| 174 | +console.error('Benchmarking import resolution...'); |
| 175 | +const inputs = collectImportPairs(); |
| 176 | +console.error(` ${inputs.length} import pairs collected`); |
| 177 | +const resolve = benchmarkResolve(inputs); |
| 178 | +console.error(` native=${resolve.nativeBatchMs}ms js=${resolve.jsFallbackMs}ms`); |
| 179 | + |
| 180 | +// Restore console.log for JSON output |
| 181 | +console.log = origLog; |
| 182 | + |
| 183 | +const result = { |
| 184 | + version: pkg.version, |
| 185 | + date: new Date().toISOString().slice(0, 10), |
| 186 | + files, |
| 187 | + wasm: { |
| 188 | + fullBuildMs: wasm.fullBuildMs, |
| 189 | + noopRebuildMs: wasm.noopRebuildMs, |
| 190 | + oneFileRebuildMs: wasm.oneFileRebuildMs, |
| 191 | + }, |
| 192 | + native: native |
| 193 | + ? { |
| 194 | + fullBuildMs: native.fullBuildMs, |
| 195 | + noopRebuildMs: native.noopRebuildMs, |
| 196 | + oneFileRebuildMs: native.oneFileRebuildMs, |
| 197 | + } |
| 198 | + : null, |
| 199 | + resolve, |
| 200 | +}; |
| 201 | + |
| 202 | +console.log(JSON.stringify(result, null, 2)); |
0 commit comments