|
| 1 | +import { rmSync } from 'node:fs' |
| 2 | +import { join } from 'node:path' |
| 3 | +import { afterAll, beforeAll, bench, describe } from 'vitest' |
| 4 | +import { |
| 5 | + createBenchOptions, |
| 6 | + createCliRunner, |
| 7 | + createConsoleSilencer, |
| 8 | + createTempDir, |
| 9 | + writeFile, |
| 10 | + writeJson, |
| 11 | + writePackage, |
| 12 | +} from './helpers.js' |
| 13 | + |
| 14 | +type LoadFixture = { |
| 15 | + root: string |
| 16 | + runner: ReturnType<typeof createCliRunner> |
| 17 | + workspaceRoot: string |
| 18 | +} |
| 19 | + |
| 20 | +const consoleSilencer = createConsoleSilencer() |
| 21 | +let fixture: LoadFixture | null = null |
| 22 | + |
| 23 | +function createFixture(): LoadFixture { |
| 24 | + const root = createTempDir('load') |
| 25 | + const workspaceRoot = createTempDir('load-workspace') |
| 26 | + |
| 27 | + writeLoadProject(root) |
| 28 | + writeLargeWorkspaceProject(workspaceRoot) |
| 29 | + |
| 30 | + return { |
| 31 | + root, |
| 32 | + runner: createCliRunner({ cwd: root }), |
| 33 | + workspaceRoot, |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +function writeLoadProject(root: string): void { |
| 38 | + writeJson(join(root, 'package.json'), { |
| 39 | + name: 'intent-load-benchmark', |
| 40 | + private: true, |
| 41 | + dependencies: { |
| 42 | + '@bench/query': '1.0.0', |
| 43 | + }, |
| 44 | + }) |
| 45 | + |
| 46 | + writePackage(join(root, 'node_modules'), '@bench/query', '1.0.0', { |
| 47 | + skills: ['query/core', 'query/cache', 'query/testing'], |
| 48 | + }) |
| 49 | + |
| 50 | + writeQueryCacheContent(join(root, 'node_modules', '@bench', 'query')) |
| 51 | +} |
| 52 | + |
| 53 | +function writeLargeWorkspaceProject(root: string): void { |
| 54 | + writeJson(join(root, 'package.json'), { |
| 55 | + name: 'intent-large-workspace-load-benchmark', |
| 56 | + private: true, |
| 57 | + workspaces: ['packages/*'], |
| 58 | + dependencies: { |
| 59 | + '@bench/query': '1.0.0', |
| 60 | + }, |
| 61 | + }) |
| 62 | + writeFile(join(root, 'pnpm-workspace.yaml'), "packages:\n - 'packages/*'\n") |
| 63 | + |
| 64 | + for (let index = 0; index < 120; index++) { |
| 65 | + writeJson(join(root, 'packages', `pkg-${index}`, 'package.json'), { |
| 66 | + name: `@bench/workspace-pkg-${index}`, |
| 67 | + version: '1.0.0', |
| 68 | + dependencies: |
| 69 | + index % 10 === 0 |
| 70 | + ? { |
| 71 | + '@bench/query': '1.0.0', |
| 72 | + } |
| 73 | + : undefined, |
| 74 | + }) |
| 75 | + } |
| 76 | + |
| 77 | + writePackage(join(root, 'node_modules'), '@bench/query', '1.0.0', { |
| 78 | + skills: ['query/core', 'query/cache', 'query/testing'], |
| 79 | + }) |
| 80 | + |
| 81 | + writeQueryCacheContent(join(root, 'node_modules', '@bench', 'query')) |
| 82 | +} |
| 83 | + |
| 84 | +function writeQueryCacheContent(packageRoot: string): void { |
| 85 | + writeFile( |
| 86 | + join(packageRoot, 'docs', 'cache-guide.md'), |
| 87 | + '# Cache guide\n\nUse the cache workflow for repeated queries.\n', |
| 88 | + ) |
| 89 | + writeFile( |
| 90 | + join(packageRoot, 'assets', 'cache.txt'), |
| 91 | + 'cache diagram placeholder\n', |
| 92 | + ) |
| 93 | + writeFile( |
| 94 | + join(packageRoot, 'skills', 'query', 'cache', 'setup.md'), |
| 95 | + '# Cache setup\n\nConfigure query cache defaults.\n', |
| 96 | + ) |
| 97 | + writeFile( |
| 98 | + join(packageRoot, 'skills', 'query', 'cache', 'SKILL.md'), |
| 99 | + [ |
| 100 | + '---', |
| 101 | + 'name: "query/cache"', |
| 102 | + 'description: "query/cache benchmark guidance"', |
| 103 | + 'type: "framework"', |
| 104 | + 'requires:', |
| 105 | + ' - "query"', |
| 106 | + '---', |
| 107 | + '', |
| 108 | + '# Query Cache', |
| 109 | + '', |
| 110 | + 'See [cache guide](../../../docs/cache-guide.md).', |
| 111 | + 'Use [local setup](setup.md#configure).', |
| 112 | + '', |
| 113 | + '', |
| 114 | + '```md', |
| 115 | + '[ignored code link](setup.md)', |
| 116 | + '```', |
| 117 | + '', |
| 118 | + ...Array.from( |
| 119 | + { length: 20 }, |
| 120 | + (_, index) => |
| 121 | + `${index + 1}. Keep cache guidance aligned with [setup](setup.md) and [guide](../../../docs/cache-guide.md#cache).`, |
| 122 | + ), |
| 123 | + '', |
| 124 | + ].join('\n'), |
| 125 | + ) |
| 126 | +} |
| 127 | + |
| 128 | +function getFixture(): LoadFixture { |
| 129 | + if (!fixture) { |
| 130 | + consoleSilencer.silence() |
| 131 | + try { |
| 132 | + fixture = createFixture() |
| 133 | + } catch (err) { |
| 134 | + consoleSilencer.restore() |
| 135 | + throw err |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + return fixture |
| 140 | +} |
| 141 | + |
| 142 | +async function setup(): Promise<void> { |
| 143 | + await getFixture().runner.setup() |
| 144 | +} |
| 145 | + |
| 146 | +function teardown(): void { |
| 147 | + if (fixture) { |
| 148 | + fixture.runner.teardown() |
| 149 | + rmSync(fixture.root, { recursive: true, force: true }) |
| 150 | + rmSync(fixture.workspaceRoot, { recursive: true, force: true }) |
| 151 | + fixture = null |
| 152 | + } |
| 153 | + |
| 154 | + consoleSilencer.restore() |
| 155 | +} |
| 156 | + |
| 157 | +async function runInCwd( |
| 158 | + cwd: string, |
| 159 | + callback: () => Promise<void>, |
| 160 | +): Promise<void> { |
| 161 | + const previousCwd = process.cwd() |
| 162 | + process.chdir(cwd) |
| 163 | + try { |
| 164 | + await callback() |
| 165 | + } finally { |
| 166 | + process.chdir(previousCwd) |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +describe('intent load', () => { |
| 171 | + beforeAll(setup) |
| 172 | + afterAll(teardown) |
| 173 | + |
| 174 | + bench( |
| 175 | + 'loads a direct dependency skill', |
| 176 | + async () => { |
| 177 | + const state = getFixture() |
| 178 | + for (let index = 0; index < 10; index++) { |
| 179 | + await state.runner.run(['load', '@bench/query#query/cache', '--path']) |
| 180 | + } |
| 181 | + }, |
| 182 | + createBenchOptions(setup, teardown), |
| 183 | + ) |
| 184 | + |
| 185 | + bench( |
| 186 | + 'loads direct dependency content as json', |
| 187 | + async () => { |
| 188 | + const state = getFixture() |
| 189 | + for (let index = 0; index < 10; index++) { |
| 190 | + await state.runner.run(['load', '@bench/query#query/cache', '--json']) |
| 191 | + } |
| 192 | + }, |
| 193 | + createBenchOptions(setup, teardown), |
| 194 | + ) |
| 195 | + |
| 196 | + bench( |
| 197 | + 'loads a direct dependency from a large workspace', |
| 198 | + async () => { |
| 199 | + const state = getFixture() |
| 200 | + await runInCwd(state.workspaceRoot, async () => { |
| 201 | + for (let index = 0; index < 10; index++) { |
| 202 | + await state.runner.run(['load', '@bench/query#query/cache', '--path']) |
| 203 | + } |
| 204 | + }) |
| 205 | + }, |
| 206 | + createBenchOptions(setup, teardown), |
| 207 | + ) |
| 208 | +}) |
0 commit comments