From 566c8ba10e45102bac4ecb17fb4510d8e5270caf Mon Sep 17 00:00:00 2001 From: hunter-dev Date: Mon, 24 Aug 2026 23:50:29 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E5=A4=A7=E5=BA=93=E6=80=A7=E8=83=BD?= =?UTF-8?q?=E5=8E=8B=E6=B5=8B=EF=BC=881000=20=E7=AF=87=EF=BC=8Ctree/search?= =?UTF-8?q?/graph=20sub-second=EF=BC=9BSTRESS=3D1=20opt-in=EF=BC=8C?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E5=A5=97=E4=BB=B6=E8=B7=B3=E8=BF=87=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/desktop/package.json | 1 + apps/desktop/tests/e2e/stress.spec.ts | 65 +++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 apps/desktop/tests/e2e/stress.spec.ts diff --git a/apps/desktop/package.json b/apps/desktop/package.json index d5a22f6..d55149b 100644 --- a/apps/desktop/package.json +++ b/apps/desktop/package.json @@ -30,6 +30,7 @@ "build": "electron-vite build", "typecheck": "tsc -b", "test:e2e": "electron-vite build && playwright test", + "test:stress": "electron-vite build && cross-env STRESS=1 playwright test stress", "dist": "electron-vite build && cross-env ELECTRON_MIRROR=https://npmmirror.com/mirrors/electron/ ELECTRON_BUILDER_BINARIES_MIRROR=https://npmmirror.com/mirrors/electron-builder-binaries/ electron-builder --win" }, "dependencies": { diff --git a/apps/desktop/tests/e2e/stress.spec.ts b/apps/desktop/tests/e2e/stress.spec.ts new file mode 100644 index 0000000..b0c7a18 --- /dev/null +++ b/apps/desktop/tests/e2e/stress.spec.ts @@ -0,0 +1,65 @@ +import { test, expect } from '@playwright/test'; +import { _electron as electron } from 'playwright'; +import { promises as fs } from 'node:fs'; +import nodePath from 'node:path'; +import os from 'node:os'; + +// 大库性能压测:1000 篇文档,测 treeList/search/graph 耗时(opt-in via STRESS=1) +const MAIN_JS = nodePath.join(process.cwd(), 'dist-electron/main/index.js'); +const N = 1000; +const STRESS = process.env.STRESS === '1'; +test.skip(!STRESS, '需 STRESS=1 启用大库压测'); + +async function launch(userData: string) { + const app = await electron.launch({ args: ['--user-data-dir=' + userData, MAIN_JS] }); + const page = await app.firstWindow(); + await page.waitForLoadState('domcontentloaded'); + const invoke = (ch: string, args: any) => page.evaluate(({ ch, args }) => (globalThis as any).dsh.invoke(ch, args), { ch, args }); + return { app, page, invoke }; +} + +test(`压测:${N} 篇文档 tree/search/graph 耗时`, async () => { + const tmp = await fs.mkdtemp(nodePath.join(os.tmpdir(), 'hn-stress-')); + const userData = nodePath.join(tmp, 'userdata'); + const vault = nodePath.join(tmp, 'vault'); + await fs.mkdir(userData, { recursive: true }); + await fs.mkdir(vault, { recursive: true }); + + // 直接落盘生成 N 篇(带 frontmatter + 串联 links) + const pad = (i: number) => String(i).padStart(4, '0'); + const gen0 = Date.now(); + await Promise.all(Array.from({ length: N }, (_, i) => { + const id = pad(i + 1); + const next = pad((i + 1) % N + 1); + const body = '# doc-' + id + '\n\n' + Array.from({ length: 20 }, (_, j) => '性能压测正文第' + j + '段。').join('\n'); + const raw = '---\ntitle: doc-' + id + '\nlinks: [doc-' + next + '.md]\n---\n' + body + '\n'; + return fs.writeFile(nodePath.join(vault, 'doc-' + id + '.md'), raw, 'utf8'); + })); + console.log('[stress] 生成 ' + N + ' 篇耗时 ' + (Date.now() - gen0) + 'ms'); + + const a = await launch(userData); + await a.invoke('vault:switch', { root: vault }); + + const t0 = Date.now(); + const tree: { path: string; type: string }[] = await a.invoke('tree:list', null); + const tTree = Date.now() - t0; + console.log('[stress] treeList ' + tree.length + ' 项 耗时 ' + tTree + 'ms'); + expect(tree.length).toBe(N); + + const s0 = Date.now(); + const hits: unknown[] = await a.invoke('search', { query: '压测' }); + const tSearch = Date.now() - s0; + console.log('[stress] search 命中 ' + hits.length + ' 耗时 ' + tSearch + 'ms'); + expect(hits.length).toBeGreaterThan(0); + + const g0 = Date.now(); + const gd: { nodes: unknown[]; edges: unknown[] } = await a.invoke('graph', null); + const tGraph = Date.now() - g0; + console.log('[stress] graph 节点 ' + gd.nodes.length + ' 边 ' + gd.edges.length + ' 耗时 ' + tGraph + 'ms'); + expect(gd.nodes.length).toBe(N); + + // 宽松阈值(机器相关):确保不病态 + expect(tTree).toBeLessThan(15000); + expect(tGraph).toBeLessThan(30000); + await a.app.close(); +});