test: 大库性能压测(1000 篇,tree/search/graph sub-second;STRESS=1 opt-in,默认套件跳过)

This commit is contained in:
hunter-dev
2026-08-24 23:50:29 +08:00
parent 49c174851e
commit 566c8ba10e
2 changed files with 66 additions and 0 deletions
+1
View File
@@ -30,6 +30,7 @@
"build": "electron-vite build", "build": "electron-vite build",
"typecheck": "tsc -b", "typecheck": "tsc -b",
"test:e2e": "electron-vite build && playwright test", "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" "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": { "dependencies": {
+65
View File
@@ -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();
});