Files
2026-11-27 06:03:56 +00:00

125 lines
6.8 KiB
JavaScript

// 真实插件缓存、编译器查询和图索引计时;仅替换不可在 CLI 中加载的 VS Code 宿主。
const assert = require('node:assert/strict');
const fs = require('node:fs/promises');
const path = require('node:path');
const Module = require('node:module');
const { createHash } = require('node:crypto');
const { performance } = require('node:perf_hooks');
class EventEmitter {
callbacks = new Set();
event = callback => { this.callbacks.add(callback); return { dispose: () => this.callbacks.delete(callback) }; };
fire(value) { for (const callback of this.callbacks) callback(value); }
dispose() { this.callbacks.clear(); }
}
async function main() {
const [compiler, project, source, changed, destination, symbol] = process.argv.slice(2).map((value, index) => index === 5 ? value : path.resolve(value));
assert.ok(symbol, 'compiler project source changed-source output-directory symbol');
await fs.mkdir(destination);
const original = await fs.readFile(source);
const replacement = await fs.readFile(changed);
assert.notDeepEqual(original, replacement);
const saved = new EventEmitter();
const disposable = () => ({ dispose() {} });
const quiet = () => ({ appendLine() {}, show() {}, hide() {}, dispose() {} });
const vscode = {
EventEmitter, Uri: { file: fsPath => ({ fsPath }) },
Range: class { constructor(start, end) { Object.assign(this, { start, end }); } },
DiagnosticSeverity: { Error: 0 },
Diagnostic: class { constructor(range, message) { Object.assign(this, { range, message }); } },
Location: class { constructor(uri, range) { Object.assign(this, { uri, range }); } },
DiagnosticRelatedInformation: class { constructor(location, message) { Object.assign(this, { location, message }); } },
workspace: {
isTrusted: true, textDocuments: [],
getConfiguration: () => ({ get: () => [compiler] }),
onDidChangeTextDocument: () => disposable(), onDidCloseTextDocument: () => disposable(),
onDidChangeConfiguration: () => disposable(), onDidSaveTextDocument: saved.event,
createFileSystemWatcher: () => ({ dispose() {}, onDidChange: disposable, onDidCreate: disposable, onDidDelete: disposable }),
},
window: { createOutputChannel: quiet, createStatusBarItem: quiet },
languages: { createDiagnosticCollection: () => ({ set() {}, clear() {}, dispose() {} }) },
};
const requireOriginal = Module._load;
Module._load = function(name, ...args) { return name === 'vscode' ? vscode : requireOriginal.call(this, name, ...args); };
const { createInspection } = require('../../editor/vscode/inspection.cjs');
Module._load = requireOriginal;
const { graph } = require('../../editor/vscode/graph.cjs');
const { workflow } = require('../../editor/vscode/workflow.cjs');
const context = { subscriptions: [] };
const inspection = createInspection(context);
const sha = data => createHash('sha256').update(data).digest('hex');
const report = { rounds: 7, warmups: 1, compiler, compiler_sha256: sha(await fs.readFile(compiler)),
project, source, symbol, node: process.version, source_sha256: sha(original), changed_sha256: sha(replacement),
scope: 'Actual inspection.cjs cache and saved-source invalidation; real compiler subprocess. Cold/changed include compiler, transport and JSON parse. Serialization, adjacency and Mermaid text generation are measured separately; no browser layout or VS Code host timing claim.',
samples: [] };
report.plugin_sources = {};
for (const name of ['inspection.cjs', 'graph.cjs', 'workflow.cjs']) {
const data = await fs.readFile(path.resolve(__dirname, '../../editor/vscode', name));
report.plugin_sources[name] = sha(data);
await fs.writeFile(path.join(destination, name), data);
}
await fs.copyFile(__filename, path.join(destination, 'editor.cjs'));
await fs.writeFile(path.join(destination, 'original-source.txt'), original);
await fs.writeFile(path.join(destination, 'changed-source.txt'), replacement);
const save = () => fs.writeFile(path.join(destination, 'results.json'), JSON.stringify(report, null, 2) + '\n');
async function measure(phase, repeat, call) {
const started = performance.now();
const value = await call();
report.samples.push({ phase, repeat, milliseconds: performance.now() - started, memory: process.memoryUsage() });
return value;
}
function check(index, expected) {
assert.deepEqual(index.diagnostics, []);
assert.ok(index.symbols.some(item => item.name === symbol), symbol);
assert.equal(index.files.find(file => file.path === source).sha256, sha(expected));
}
try {
for (let repeat = -1; repeat < 7; repeat++) {
await fs.writeFile(source, original);
saved.fire({ languageId: 'dever', uri: { fsPath: source } });
const index = await measure('cold-query', repeat, () => inspection.load(project));
check(index, original);
const cached = await measure('cached-query', repeat, () => inspection.load(project));
assert.equal(cached, index);
const adjacency = await measure('adjacency', repeat, () => graph(index));
assert.equal(adjacency.symbols.size, index.symbols.length);
assert.equal(await measure('cached-adjacency', repeat, () => graph(index)), adjacency);
const encoded = await measure('serialize-index', repeat, () => JSON.stringify(index));
const decoded = await measure('parse-index', repeat, () => JSON.parse(encoded));
assert.deepEqual(decoded, index);
const flow = await measure('selected-flow', repeat, () => inspection.load(project, symbol));
check(flow, original);
assert.ok(flow.flows.length > 0);
const markdown = await measure('workflow-text', repeat, () => workflow(flow, project));
assert.ok(markdown.includes('flowchart TD'));
await fs.writeFile(source, replacement);
saved.fire({ languageId: 'dever', uri: { fsPath: source } });
const updated = await measure('changed-query', repeat, () => inspection.load(project));
check(updated, replacement);
assert.notEqual(updated, index);
assert.notEqual(graph(updated), adjacency);
report.samples.at(-1).index_bytes = Buffer.byteLength(encoded);
report.samples.at(-1).symbols = index.symbols.length;
await save();
}
report.correct = true;
report.medians_ms = {};
for (const phase of new Set(report.samples.map(row => row.phase))) {
const values = report.samples.filter(row => row.phase === phase && row.repeat >= 0).map(row => row.milliseconds).sort((a, b) => a - b);
report.medians_ms[phase] = values[3];
}
await save();
console.log(JSON.stringify(report.medians_ms));
} catch (error) {
report.error = error.stack;
await save();
throw error;
} finally {
await fs.writeFile(source, original);
for (const item of context.subscriptions) item.dispose();
}
}
main().catch(error => { console.error(error); process.exitCode = 1; });