|
| 1 | +import type {ILogger, OutputCleanContext, OutputCleanupDeclarations, OutputPlugin} from '../src/plugins/plugin-core' |
| 2 | +import * as fs from 'node:fs' |
| 3 | +import * as os from 'node:os' |
| 4 | +import * as path from 'node:path' |
| 5 | +import {performance} from 'node:perf_hooks' |
| 6 | +import glob from 'fast-glob' |
| 7 | + |
| 8 | +process.env['TNMSC_FORCE_NATIVE_BINDING'] = '1' |
| 9 | +delete process.env['VITEST'] |
| 10 | +delete process.env['VITEST_WORKER_ID'] |
| 11 | + |
| 12 | +const cleanupModule = await import('../src/commands/CleanupUtils') |
| 13 | +const fallbackModule = await import('../src/commands/CleanupUtils.fallback') |
| 14 | +const pluginCore = await import('../src/plugins/plugin-core') |
| 15 | + |
| 16 | +function createMockLogger(): ILogger { |
| 17 | + return { |
| 18 | + trace: () => {}, |
| 19 | + debug: () => {}, |
| 20 | + info: () => {}, |
| 21 | + warn: () => {}, |
| 22 | + error: () => {}, |
| 23 | + fatal: () => {} |
| 24 | + } as ILogger |
| 25 | +} |
| 26 | + |
| 27 | +function createCleanContext(workspaceDir: string): OutputCleanContext { |
| 28 | + return { |
| 29 | + logger: createMockLogger(), |
| 30 | + fs, |
| 31 | + path, |
| 32 | + glob, |
| 33 | + collectedOutputContext: { |
| 34 | + workspace: { |
| 35 | + directory: { |
| 36 | + pathKind: pluginCore.FilePathKind.Absolute, |
| 37 | + path: workspaceDir, |
| 38 | + getDirectoryName: () => path.basename(workspaceDir), |
| 39 | + getAbsolutePath: () => workspaceDir |
| 40 | + }, |
| 41 | + projects: Array.from({length: 40}, (_, index) => ({ |
| 42 | + dirFromWorkspacePath: { |
| 43 | + pathKind: pluginCore.FilePathKind.Relative, |
| 44 | + path: `project-${index}`, |
| 45 | + basePath: workspaceDir, |
| 46 | + getDirectoryName: () => `project-${index}`, |
| 47 | + getAbsolutePath: () => path.join(workspaceDir, `project-${index}`) |
| 48 | + } |
| 49 | + })) |
| 50 | + }, |
| 51 | + aindexDir: path.join(workspaceDir, 'aindex') |
| 52 | + } |
| 53 | + } as OutputCleanContext |
| 54 | +} |
| 55 | + |
| 56 | +function createBenchmarkPlugin(workspaceDir: string): OutputPlugin { |
| 57 | + return { |
| 58 | + type: pluginCore.PluginKind.Output, |
| 59 | + name: 'BenchmarkOutputPlugin', |
| 60 | + log: createMockLogger(), |
| 61 | + declarativeOutput: true, |
| 62 | + outputCapabilities: {}, |
| 63 | + async declareOutputFiles() { |
| 64 | + return Array.from({length: 40}, (_, projectIndex) => ([ |
| 65 | + {path: path.join(workspaceDir, `project-${projectIndex}`, 'AGENTS.md'), source: {}}, |
| 66 | + {path: path.join(workspaceDir, `project-${projectIndex}`, 'commands', 'AGENTS.md'), source: {}} |
| 67 | + ])).flat() |
| 68 | + }, |
| 69 | + async declareCleanupPaths(): Promise<OutputCleanupDeclarations> { |
| 70 | + return { |
| 71 | + delete: [{ |
| 72 | + kind: 'glob', |
| 73 | + path: path.join(workspaceDir, '.codex', 'skills', '*'), |
| 74 | + excludeBasenames: ['.system'] |
| 75 | + }, { |
| 76 | + kind: 'glob', |
| 77 | + path: path.join(workspaceDir, '.claude', '**', 'CLAUDE.md') |
| 78 | + }], |
| 79 | + protect: [{ |
| 80 | + kind: 'directory', |
| 81 | + path: path.join(workspaceDir, '.codex', 'skills', '.system'), |
| 82 | + protectionMode: 'recursive' |
| 83 | + }] |
| 84 | + } |
| 85 | + }, |
| 86 | + async convertContent() { |
| 87 | + return 'benchmark' |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +async function measure(label: string, iterations: number, run: () => Promise<void>): Promise<number> { |
| 93 | + const start = performance.now() |
| 94 | + for (let index = 0; index < iterations; index += 1) { |
| 95 | + await run() |
| 96 | + } |
| 97 | + const total = performance.now() - start |
| 98 | + const average = total / iterations |
| 99 | + process.stdout.write(`${label}: total=${total.toFixed(2)}ms avg=${average.toFixed(2)}ms\n`) |
| 100 | + return average |
| 101 | +} |
| 102 | + |
| 103 | +async function main(): Promise<void> { |
| 104 | + if (!cleanupModule.hasNativeCleanupBinding()) { |
| 105 | + throw new Error('Native cleanup binding is unavailable. Build the CLI NAPI module first.') |
| 106 | + } |
| 107 | + |
| 108 | + const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'tnmsc-benchmark-cleanup-')) |
| 109 | + const workspaceDir = path.join(tempDir, 'workspace') |
| 110 | + |
| 111 | + try { |
| 112 | + for (let projectIndex = 0; projectIndex < 40; projectIndex += 1) { |
| 113 | + const rootFile = path.join(workspaceDir, `project-${projectIndex}`, 'AGENTS.md') |
| 114 | + const childFile = path.join(workspaceDir, `project-${projectIndex}`, 'commands', 'AGENTS.md') |
| 115 | + fs.mkdirSync(path.dirname(childFile), {recursive: true}) |
| 116 | + fs.writeFileSync(rootFile, '# root', 'utf8') |
| 117 | + fs.writeFileSync(childFile, '# child', 'utf8') |
| 118 | + } |
| 119 | + |
| 120 | + const skillsDir = path.join(workspaceDir, '.codex', 'skills') |
| 121 | + fs.mkdirSync(path.join(skillsDir, '.system'), {recursive: true}) |
| 122 | + for (let index = 0; index < 80; index += 1) { |
| 123 | + const skillDir = path.join(skillsDir, `legacy-${index}`) |
| 124 | + fs.mkdirSync(skillDir, {recursive: true}) |
| 125 | + fs.writeFileSync(path.join(skillDir, 'SKILL.md'), '# stale', 'utf8') |
| 126 | + } |
| 127 | + |
| 128 | + for (let index = 0; index < 40; index += 1) { |
| 129 | + const claudeFile = path.join(workspaceDir, '.claude', `project-${index}`, 'CLAUDE.md') |
| 130 | + fs.mkdirSync(path.dirname(claudeFile), {recursive: true}) |
| 131 | + fs.writeFileSync(claudeFile, '# claude', 'utf8') |
| 132 | + } |
| 133 | + |
| 134 | + const plugin = createBenchmarkPlugin(workspaceDir) |
| 135 | + const cleanCtx = createCleanContext(workspaceDir) |
| 136 | + const iterations = 25 |
| 137 | + |
| 138 | + process.stdout.write(`cleanup benchmark iterations=${iterations}\n`) |
| 139 | + const fallbackAvg = await measure('fallback-plan', iterations, async () => { |
| 140 | + await fallbackModule.collectDeletionTargets([plugin], cleanCtx) |
| 141 | + }) |
| 142 | + const nativeAvg = await measure('native-plan', iterations, async () => { |
| 143 | + await cleanupModule.collectDeletionTargets([plugin], cleanCtx) |
| 144 | + }) |
| 145 | + |
| 146 | + const delta = nativeAvg - fallbackAvg |
| 147 | + process.stdout.write(`delta=${delta.toFixed(2)}ms (${((delta / fallbackAvg) * 100).toFixed(2)}%)\n`) |
| 148 | + } |
| 149 | + finally { |
| 150 | + fs.rmSync(tempDir, {recursive: true, force: true}) |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +await main() |
0 commit comments