Files
new-markdown/tests/perf.test.ts

57 lines
2.0 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 性能基准(阶段 6):默认跳过,PERF_BENCH=1 时运行。
// 口径:jsdom + Node(无真实浏览器渲染管线),只测「解析建模 + 序列化」耗时,
// 不是 WebView2 真机输入延迟;真机口径见 docs/10 §3 的 Windows 清单。
declare const process: { env: Record<string, string | undefined> }
import { describe, it, expect } from 'vitest'
import { createMarkdownEditor, getMarkdown } from '../src/editor/editor'
const BLOCK = `## 性能小节标题
这是一个包含**粗体**、*斜体*、[链接](https://example.com) 与 \`行内代码\` 的段落,
用来模拟真实写作内容。第二句继续填充文本,让段落长度接近真实文档。
- 列表项一
- 列表项二
- 任务 [x] 已完成
\`\`\`ts
const answer: number = 42
console.log(answer)
\`\`\`
| 列 A | 列 B |
| --- | --- |
| 1 | 2 |
`
function makeDoc(targetBytes: number): string {
let out = '# 性能基准文档\n\n'
while (new TextEncoder().encode(out).length < targetBytes) out += BLOCK
return out
}
const enabled = process.env.PERF_BENCH === '1'
describe.skipIf(!enabled)('性能基准(PERF_BENCH=1)', () => {
it.each([100 * 1024, 1024 * 1024])('打开并序列化 %i 字节文档', async (size) => {
const md = makeDoc(size)
const host = document.createElement('div')
document.body.appendChild(host)
const t0 = performance.now()
let serialized = ''
const editor = createMarkdownEditor(host, md, {
onMarkdown: (text) => { serialized = text },
})
await editor.create()
const createMs = performance.now() - t0
const t1 = performance.now()
const out = getMarkdown(editor)
const serializeMs = performance.now() - t1
console.table([{ 目标大小KB: Math.round(size / 1024), 实际字符: md.length, 建模ms: Math.round(createMs), 序列化ms: Math.round(serializeMs), 首轮回调字符: serialized.length }])
expect(out.length).toBeGreaterThan(md.length * 0.9)
await editor.destroy()
host.remove()
}, 300_000)
})