#!/usr/bin/env node /** * MetonaEditor 性能基准 — 解析器吞吐测试 * 用法: npm run build && npm run bench */ const MeEditor = require('../dist/metona-editor.cjs'); const { parseMarkdown } = MeEditor; const buildSample = (kb) => { const chunk = [ '# 性能基准文档', '', '这是一个 **粗体** 与 *斜体* 的段落,包含 `inline code` 和 [链接](https://example.com)。', '', '- 列表项一', '- 列表项二', ' - 嵌套项', ' - 嵌套项二', '- 列表项三', '', '1. 有序一', '2. 有序二', '', '> 引用内容', '> 继续引用', '', '```js', 'function hello(name) {', ' const msg = `Hello, ${name}!`;', ' return msg;', '}', '```', '', '| 列一 | 列二 | 列三 |', '| --- | :---: | ---: |', '| A | B | C |', '| D | E | F |', '', '---', '', '引用链接:[文档][ref]', '', '[ref]: https://example.com/docs "参考"', '', '脚注示例[^1]', '', '[^1]: 脚注内容', '', '## 二级标题', '', '$$E = mc^2$$', '', 'H~2~O 与 x^2^,还有 :rocket: emoji。', '', ].join('\n'); const repeat = Math.max(1, Math.ceil((kb * 1024) / chunk.length)); return chunk.repeat(repeat); }; const median = (arr) => { const s = [...arr].sort((a, b) => a - b); const mid = Math.floor(s.length / 2); return s.length % 2 ? s[mid] : (s[mid - 1] + s[mid]) / 2; }; const benchmark = (label, fn, iterations = 5) => { // warmup fn(); const times = []; for (let i = 0; i < iterations; i++) { const t0 = process.hrtime.bigint(); fn(); const t1 = process.hrtime.bigint(); times.push(Number(t1 - t0) / 1e6); } const ms = median(times); return { label, ms: ms.toFixed(1), opsPerSec: (1000 / ms).toFixed(1) }; }; console.log('MetonaEditor 性能基准'); console.log('====================='); console.log(`版本: ${MeEditor.VERSION}`); console.log(''); for (const size of [64, 256, 1024]) { const sample = buildSample(size); const r = benchmark(`parseMarkdown ${size}KB (${sample.length.toLocaleString()} chars)`, () => { const html = parseMarkdown(sample); if (!html) throw new Error('empty output'); }); console.log(` ${r.label}: ${r.ms} ms (${r.opsPerSec} ops/s)`); } console.log(''); // 语法高亮基准 try { const code = Array.from({ length: 200 }, (_, i) => `function f${i}(a, b) { const s = "str${i}"; // comment\n return a + b + ${i}; }`, ).join('\n'); const r = benchmark('highlight 200 functions (js)', () => { MeEditor.highlight(code, 'js'); }); console.log(` ${r.label}: ${r.ms} ms (${r.opsPerSec} ops/s)`); } catch (_) { console.log(' (highlight benchmark skipped)'); } console.log(''); console.log('完成。');