attachment-store-performance.test.mjs
68 lines 2.3 KB
Raw
sha256:b5f647cb9c409f563d4671fe3fc05ddea01fabfed9b41fc11cb923588e1c1baf mirror: GitHub Phase A durable MCP OAuth (#270) Human minor ⚠ breaking 10 days ago
1 /**
2 * Tier 6 — PERFORMANCE: list/get p95 budget on MAX fixture.
3 *
4 * @see docs/ATTACHMENT-STORE-CONTRACT-2F-b.md §9
5 */
6 import { describe, it, beforeEach, afterEach } from 'node:test';
7 import assert from 'node:assert/strict';
8 import fs from 'node:fs';
9 import path from 'node:path';
10 import { fileURLToPath } from 'node:url';
11 import { listAttachments, getAttachment, MAX_ATTACHMENT_SUMMARIES, deriveAttachmentId } from '../lib/attachments/attachment-store.mjs';
12
13 const __dirname = path.dirname(fileURLToPath(import.meta.url));
14 const tmpRoot = path.join(__dirname, 'fixtures', 'tmp-attachment-performance');
15
16 function percentile(values, p) {
17 const sorted = [...values].sort((a, b) => a - b);
18 const idx = Math.ceil((p / 100) * sorted.length) - 1;
19 return sorted[Math.max(0, idx)];
20 }
21
22 describe('Attachment store — performance', () => {
23 const dataDir = path.join(tmpRoot, 'data');
24 const vaultPath = path.join(tmpRoot, 'vault');
25 const vaultId = 'default';
26 let sampleId;
27
28 beforeEach(() => {
29 fs.rmSync(tmpRoot, { recursive: true, force: true });
30 fs.mkdirSync(path.join(vaultPath, 'media'), { recursive: true });
31 fs.mkdirSync(dataDir, { recursive: true });
32
33 for (let i = 0; i < MAX_ATTACHMENT_SUMMARIES; i += 1) {
34 const name = `perf_${String(i).padStart(5, '0')}.png`;
35 fs.writeFileSync(path.join(vaultPath, 'media', name), 'x');
36 if (i === 0) sampleId = deriveAttachmentId('file', `file:media/${name}`);
37 }
38 });
39
40 afterEach(() => {
41 fs.rmSync(tmpRoot, { recursive: true, force: true });
42 });
43
44 it('listAttachments and getAttachment stay within p95 budget', () => {
45 const listTimes = [];
46 for (let i = 0; i < 30; i += 1) {
47 const t0 = performance.now();
48 listAttachments(dataDir, vaultPath, vaultId, {
49 filterScopes: new Set(['personal', 'project', 'org']),
50 effectiveScope: 'org',
51 limit: MAX_ATTACHMENT_SUMMARIES,
52 });
53 listTimes.push(performance.now() - t0);
54 }
55
56 const getTimes = [];
57 for (let i = 0; i < 30; i += 1) {
58 const t0 = performance.now();
59 getAttachment(dataDir, vaultPath, vaultId, sampleId, {
60 visibleScopes: new Set(['personal', 'project', 'org']),
61 });
62 getTimes.push(performance.now() - t0);
63 }
64
65 assert.ok(percentile(listTimes, 95) < 2000);
66 assert.ok(percentile(getTimes, 95) < 2000);
67 });
68 });
File History 1 commit
sha256:b5f647cb9c409f563d4671fe3fc05ddea01fabfed9b41fc11cb923588e1c1baf mirror: GitHub Phase A durable MCP OAuth (#270) Human minor 10 days ago