mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-03 17:51:58 +01:00
refactor(perf): reuse database test setup for profile benchmark (#18683)
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - Tool profiles govern the tools that agents can use. > - Profile listing and app attention shared nested selector scans that consumed the server CPU. > - #18681 and #18673 replaced those scans with indexes for each computation. > - Their performance benchmarks need consistent setup and reproducible per-service measurements. > - This pull request uses the shared helper entry point and retains the exact measurement harness and results. > - Reviewers can inspect both the CPU savings and the limits of the evidence. ## Linked Issues or Issue Description Refs #18681 and #18673. **What existing behavior does this improve?** The consistency of benchmark setup and the reproducibility of evidence for the two merged CPU fixes. **Current behavior** The profile-review benchmark imports the database starter directly. The matching benchmark uses the shared server test-helper entry point. On the current base, that helper re-exports the database starter, which already supports `PAPERCLIP_TEST_POSTGRES_ADMIN_URL`. The previous committed benchmark measures both services together; the per-service measurements and separate HTTP/Inspector runs lacked a committed harness. **Proposed behavior** Use the same test-helper entry point in both benchmarks. Retain the compile, per-service timing, Inspector, and HTTP-probe programs with their exact commands and raw samples. Preserve the existing embedded and admin-server database behavior. **Reason and benefit** A shared entry point keeps benchmark setup consistent. The committed harness lets reviewers reproduce and compare each real owning service before and after the merged fixes. The failure-path checks verify that result-write errors still release the database and probe resources. **Breaking changes** None. This change affects benchmark setup and documentation. ## What Changed - Import the database starter from the repository test helper in `benchmark-profile-review-projection.ts`. - Record separate CPU and elapsed measurements for profile listing and app attention. - Retain the actual compile, per-service measurement, Inspector capture, and separate-process HTTP probe harness. - Always tear down the test database, HTTP server, and probe even when result writing fails. - Retain all timing and responsiveness samples as sanitized JSON. - State the runtime, fixture, source revisions, and limits of the local comparison. ## Verification - 24 focused tests passed across selector matching, pending projection, review-service freshness, and effective assignments. - Four selected real database-backed service/route tests passed for summaries, review changes, auto-allow behavior, and company isolation. The other 494 tests in that file were not selected. - Injected an `EISDIR` result-write failure into both profiling programs on an owned local admin server. Each child exited with the expected error and removed its disposable database. The responsiveness child also terminated its probe/server handles. - The changed benchmark completed with a configured local PostgreSQL admin URL. Its full responses matched the baseline, and query/row counts matched. - The exact documented compile, benchmark, and responsiveness commands completed from the committed harness at `ca458baddc6461fb6c888984246e329e2bb4bdfe`. The JSON records that build revision and the four owner source hashes. - Five alternating samples per service and implementation used Node 24.22.2 with compiled ES modules. No runtime TypeScript loader ran in those measurements. - Median process CPU from the committed harness: profile listing 3,164 ms to 385 ms; app attention 2,466 ms to 221 ms. Both versions returned identical ordered results and made seven queries per service. - Separate Inspector profiles confirmed that the repeated scope and summary scans were removed. Three separate-process HTTP probe trials per service showed less event-loop blocking. These are local synthetic service results, not hosted page-load acceptance. - Initial local setup failed on missing embedded PostgreSQL library aliases and an unbuilt plugin SDK. Restoring local aliases and building the SDK resolved those setup failures. The original failed setup logs were retained; the 28 selected tests then passed. - The merged summary fix CI run [41535304968](https://github.com/paperclipai/paperclip/actions/runs/41535304968) hit two 15-second timeouts in the unchanged attention-retention history cases. The server shard passed unchanged on one failed-jobs-only retry. Original and retry logs are retained. The compared runs used different runners and synthetic merge trees, so the original timeout cause is not established. No timeout or assertion was changed. - Full repository typecheck, test matrix, and build passed in CI on final head `2f484ed4284f3fa632d8e73d9be084827d1c987e`: 54 successful check runs, three expected skips, one non-blocking Paperclip review, and a successful Snyk status. No failures or pending checks remain. Those broad commands were not rerun locally for this benchmark-tooling follow-up; the 28 selected local regressions and the actual profiling/failure-path programs ran locally. - Fresh Greptile is 5/5 on that exact head. Both review P2 threads are resolved. The Paperclip review's current-base documentation note is corrected in this description; its optional tooling suggestions remain outside this bounded change. ## Risks - The test helper creates a unique disposable database on the configured admin server and removes it afterward. The operator must use a database server where those test operations are appropriate. - Synthetic data and shared-host activity affect absolute timings. The measurements do not establish a production latency percentage or capacity limit. - Production behavior is covered by the two linked merged fixes. This follow-up changes only benchmark setup and documentation. ## Model Used OpenAI GPT-6 through Codex, with code execution and repository tools. The exact runtime model revision and context window were not exposed in this session. The original linked performance fixes record their own model details. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Paperclip <noreply@paperclip.ing>
This commit is contained in:
1 parent
3f8ebfff8a
commit
00fea0da52
8 files changed
+875
-1
No files matched your search
@@ -1,7 +1,8 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { isDeepStrictEqual } from "node:util";
|
||||
import { pathToFileURL } from "node:url";
|
||||
import { createDb, startEmbeddedPostgresTestDatabase, type Db } from "@paperclipai/db";
|
||||
import { createDb, type Db } from "@paperclipai/db";
|
||||
import { startEmbeddedPostgresTestDatabase } from "../src/__tests__/helpers/embedded-postgres.js";
|
||||
import { createToolProfileService } from "../src/services/tool-access/profiles/service.js";
|
||||
import { createToolAppAttention } from "../src/services/tool-access/catalog/attention.js";
|
||||
import { createProfileProjectionFixture } from "../src/__tests__/helpers/tool-profile-projection-fixture.js";
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { writeFile, mkdir, readFile } from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
import { Session } from 'node:inspector';
|
||||
import { createDb } from '@paperclipai/db';
|
||||
import { startEmbeddedPostgresTestDatabase } from '../../dist/__tests__/helpers/embedded-postgres.js';
|
||||
import { createProfileProjectionFixture } from '../../dist/__tests__/helpers/tool-profile-projection-fixture.js';
|
||||
const option = (name, fallback) => { const index = process.argv.indexOf(name); return index < 0 ? fallback : process.argv[index + 1] ?? fallback; };
|
||||
const output = path.resolve(option('--output-dir', '/tmp/paperclip-tool-projection'));
|
||||
const repeats = Number(option('--repeat', '5'));
|
||||
assert.ok(Number.isInteger(repeats) && repeats > 0, '--repeat must be a positive integer');
|
||||
assert.equal(typeof global.gc, 'function', 'Run Node with --expose-gc');
|
||||
const build = JSON.parse(await readFile(new URL('../../dist/.profile-projection-build.json', import.meta.url), 'utf8'));
|
||||
await mkdir(output, { recursive: true });
|
||||
const variants = {};
|
||||
for (const label of ['baseline', 'pending', 'combined']) {
|
||||
const p = label === 'combined' ? './service.js' : `./.${label}-service.js`;
|
||||
const a = label === 'combined' ? './attention.js' : `./.${label}-attention.js`;
|
||||
variants[label] = {
|
||||
profileFactory: (await import(new URL(`../../dist/services/tool-access/profiles/${p}`, import.meta.url))).createToolProfileService,
|
||||
attentionFactory: (await import(new URL(`../../dist/services/tool-access/catalog/${a}`, import.meta.url))).createToolAppAttention,
|
||||
};
|
||||
}
|
||||
const database = await startEmbeddedPostgresTestDatabase('paperclip-hotpaths-owned-');
|
||||
const db = createDb(database.connectionString, { maxConnections: 1 });
|
||||
let queries = 0, rows = 0;
|
||||
const measuredDb = new Proxy(db, { get(target, key) {
|
||||
if (key !== 'select') return Reflect.get(target, key);
|
||||
return (...fields) => {
|
||||
const builder = target.select(...fields), from = builder.from.bind(builder);
|
||||
builder.from = (...args) => {
|
||||
const selection = from(...args), execute = selection.execute.bind(selection);
|
||||
selection.execute = async () => { const result = await execute(); queries++; rows += result.length; return result; };
|
||||
return selection;
|
||||
};
|
||||
return builder;
|
||||
};
|
||||
} });
|
||||
const records = [];
|
||||
const emit = record => { records.push(record); console.log(JSON.stringify(record)); };
|
||||
try {
|
||||
const fixture = await createProfileProjectionFixture(db);
|
||||
const now = () => new Date('2026-02-01T00:00:00Z');
|
||||
for (const variant of Object.values(variants)) {
|
||||
const profiles = variant.profileFactory(measuredDb, now);
|
||||
const attention = variant.attentionFactory(measuredDb, now);
|
||||
variant.profiles = () => profiles.listProfiles(fixture.company.id);
|
||||
variant.attention = () => attention(fixture.company.id);
|
||||
}
|
||||
emit({ kind: 'environment', node: process.version, platform: process.platform, arch: process.arch,
|
||||
build: 'esbuild transpiled ES modules, node24 target; no runtime TypeScript loader',
|
||||
...build, forcedGc: true,
|
||||
fixture: { profiles: 200, activeTools: 1900, removedTools: 695, rules: 76000, agents: 12 }, repeats,
|
||||
caveat: 'Local synthetic service workload, shared developer host; not hosted page-load acceptance' });
|
||||
for (const owner of ['profiles', 'attention']) {
|
||||
const expected = await variants.baseline[owner]();
|
||||
for (const label of ['pending', 'combined']) assert.deepEqual(await variants[label][owner](), expected);
|
||||
emit({ kind: 'correctness', owner, completeOutputEquality: true });
|
||||
}
|
||||
for (let round = 0; round < repeats; round++) {
|
||||
for (const owner of ['profiles', 'attention']) {
|
||||
const order = round % 2 ? ['combined', 'pending', 'baseline'] : ['baseline', 'pending', 'combined'];
|
||||
for (const label of order) {
|
||||
global.gc?.(); queries = 0; rows = 0;
|
||||
const started = performance.now(), cpu = process.cpuUsage();
|
||||
const result = await variants[label][owner]();
|
||||
const used = process.cpuUsage(cpu);
|
||||
emit({ kind: 'timing', round, owner, label, wallMs: performance.now() - started, cpuMs: (used.user + used.system) / 1000,
|
||||
queries, rows, resultCount: owner === 'profiles' ? result.length : result.items?.length });
|
||||
}
|
||||
}
|
||||
}
|
||||
for (const owner of ['profiles', 'attention']) {
|
||||
for (const label of ['baseline', 'pending', 'combined']) {
|
||||
global.gc?.();
|
||||
const session = new Session(); session.connect();
|
||||
const post = (method, args = {}) => new Promise((resolve, reject) => session.post(method, args, (error, value) => error ? reject(error) : resolve(value)));
|
||||
try {
|
||||
await post('Profiler.enable'); await post('Profiler.setSamplingInterval', { interval: 1000 });
|
||||
await post('Profiler.start');
|
||||
await variants[label][owner]();
|
||||
const { profile } = await post('Profiler.stop');
|
||||
await writeFile(`${output}/${owner}-${label}.cpuprofile`, JSON.stringify(profile));
|
||||
emit({ kind: 'profile', owner, label, durationMs: (profile.endTime - profile.startTime) / 1000, samples: profile.samples?.length });
|
||||
} finally { session.disconnect(); }
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
try {
|
||||
await writeFile(`${output}/measurements.json`, JSON.stringify(records, null, 2));
|
||||
} finally {
|
||||
try { await db.$client.end({ timeout: 1 }); }
|
||||
finally { await database.cleanup(); }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
import { build, transform } from 'esbuild';
|
||||
import { readdir, mkdir, writeFile, cp, readFile } from 'node:fs/promises';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { createHash } from 'node:crypto';
|
||||
import { execFileSync } from 'node:child_process';
|
||||
import path from 'node:path';
|
||||
const root = fileURLToPath(new URL("../../..", import.meta.url));
|
||||
process.chdir(root);
|
||||
async function sources(dir) {
|
||||
const out = [];
|
||||
for (const entry of await readdir(dir, { withFileTypes: true })) {
|
||||
const file = path.join(dir, entry.name);
|
||||
if (entry.isDirectory() && entry.name !== '__tests__') out.push(...await sources(file));
|
||||
else if (entry.isFile() && /\.tsx?$/.test(entry.name) && !/\.d\.ts$|\.test\.tsx?$/.test(entry.name)) out.push(file);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
async function assets(dir, out) {
|
||||
for (const entry of await readdir(dir, { withFileTypes: true })) {
|
||||
const file = path.join(dir, entry.name), target = path.join(out, entry.name);
|
||||
if (entry.isDirectory() && entry.name !== '__tests__') await assets(file, target);
|
||||
else if (entry.isFile() && entry.name.endsWith('.json')) { await mkdir(out, { recursive: true }); await cp(file, target); }
|
||||
}
|
||||
}
|
||||
for (const pkg of ['packages/shared', 'packages/db', 'server']) {
|
||||
const src = path.join(root, pkg, 'src');
|
||||
await build({ entryPoints: await sources(src), outbase: src, outdir: path.join(root, pkg, 'dist'),
|
||||
format: 'esm', platform: 'node', target: 'node24', sourcemap: true, logLevel: 'warning' });
|
||||
await assets(src, path.join(root, pkg, 'dist'));
|
||||
}
|
||||
await cp('packages/db/src/migrations', 'packages/db/dist/migrations', { recursive: true });
|
||||
await cp('packages/shared/src/app-definitions', 'packages/shared/dist/app-definitions', { recursive: true });
|
||||
await mkdir('server/dist/__tests__/helpers', { recursive: true });
|
||||
await build({ entryPoints: ['server/src/__tests__/helpers/tool-profile-projection-fixture.ts', 'server/src/__tests__/helpers/embedded-postgres.ts'],
|
||||
outbase: 'server/src/__tests__/helpers', outdir: 'server/dist/__tests__/helpers', format: 'esm', platform: 'node', target: 'node24', sourcemap: true });
|
||||
for (const [label, sha] of [['baseline', '1f8f2e195f4897cd1c237bdc068de95780ab0c3c'], ['pending', 'fd59c0183ce64c44696f79604516f4d51ca5662b']]) {
|
||||
for (const [name, file] of [['projection', 'profiles/projection'], ['service', 'profiles/service'], ['attention', 'catalog/attention']]) {
|
||||
const relative = `server/src/services/tool-access/${file}.ts`;
|
||||
let code = execFileSync('git', ['show', `${sha}:${relative}`], { encoding: 'utf8', maxBuffer: 5e6 });
|
||||
if (name === 'service') code = code.replace('from "./projection.js"', `from "./.${label}-projection.js"`);
|
||||
if (name === 'attention') code = code.replace('from "../profiles/projection.js"', `from "../profiles/.${label}-projection.js"`);
|
||||
const outfile = `server/dist/services/tool-access/${path.dirname(file)}/.${label}-${name}.js`;
|
||||
const result = await transform(code, { loader: 'ts', format: 'esm', platform: 'node', target: 'node24', sourcefile: relative, sourcemap: 'inline' });
|
||||
await writeFile(outfile, result.code);
|
||||
}
|
||||
}
|
||||
const owners = ['profiles/projection', 'profiles/service', 'profiles/resolution', 'catalog/attention'];
|
||||
const manifest = {
|
||||
baseline: '1f8f2e195f4897cd1c237bdc068de95780ab0c3c',
|
||||
pending: 'fd59c0183ce64c44696f79604516f4d51ca5662b',
|
||||
treatmentHead: execFileSync('git', ['rev-parse', 'HEAD'], { encoding: 'utf8' }).trim(),
|
||||
sourceSha256: Object.fromEntries(await Promise.all(owners.map(async file => [file,
|
||||
createHash('sha256').update(await readFile(`server/src/services/tool-access/${file}.ts`)).digest('hex')]))),
|
||||
};
|
||||
await writeFile('server/dist/.profile-projection-build.json', JSON.stringify(manifest, null, 2));
|
||||
console.log('Compiled baseline, pending-only, and current owning service modules; no runtime TypeScript loader.');
|
||||
@@ -0,0 +1,20 @@
|
||||
import http from 'node:http';
|
||||
const agent = new http.Agent({ keepAlive: true });
|
||||
let active = false, pending, times = [], port;
|
||||
function ping() {
|
||||
if (!active || pending) return;
|
||||
pending = new Promise(resolve => {
|
||||
const start = performance.now();
|
||||
const request = http.get({ host: '127.0.0.1', port, path: '/api/health', agent }, response => {
|
||||
response.resume(); response.on('end', () => { times.push(performance.now() - start); resolve(); });
|
||||
});
|
||||
request.setTimeout(20000, () => request.destroy(new Error('Probe timeout')));
|
||||
request.on('error', error => { times.push({ error: error.message }); resolve(); });
|
||||
}).finally(() => { pending = null; });
|
||||
}
|
||||
const interval = setInterval(ping, 20);
|
||||
process.on('message', async message => {
|
||||
if (message.kind === 'arm') { port = message.port; times = []; active = true; process.send({ kind: 'armed' }); }
|
||||
if (message.kind === 'stop') { active = false; await pending; process.send({ kind: 'stopped', times }); }
|
||||
if (message.kind === 'close') { clearInterval(interval); agent.destroy(); process.disconnect(); }
|
||||
});
|
||||
@@ -0,0 +1,13 @@
|
||||
import { registerHooks } from 'node:module';
|
||||
import { pathToFileURL, fileURLToPath } from 'node:url';
|
||||
import path from 'node:path';
|
||||
const root = fileURLToPath(new URL("../../..", import.meta.url));
|
||||
registerHooks({ resolve(specifier, context, next) {
|
||||
for (const [name, dir] of [['@paperclipai/db', 'packages/db'], ['@paperclipai/shared', 'packages/shared']]) {
|
||||
if (specifier === name || specifier.startsWith(name + '/')) {
|
||||
const file = specifier === name ? 'index' : specifier.slice(name.length + 1);
|
||||
return next(pathToFileURL(path.join(root, dir, 'dist', `${file}.js`)).href, context);
|
||||
}
|
||||
}
|
||||
return next(specifier, context);
|
||||
} });
|
||||
@@ -0,0 +1,74 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import path from 'node:path';
|
||||
import { createServer } from 'node:http';
|
||||
import { fork } from 'node:child_process';
|
||||
import { once } from 'node:events';
|
||||
import { writeFile, mkdir } from 'node:fs/promises';
|
||||
import { createDb } from '@paperclipai/db';
|
||||
import { startEmbeddedPostgresTestDatabase } from '../../dist/__tests__/helpers/embedded-postgres.js';
|
||||
import { createProfileProjectionFixture } from '../../dist/__tests__/helpers/tool-profile-projection-fixture.js';
|
||||
const index = process.argv.indexOf('--output-dir');
|
||||
const output = path.resolve(index < 0 ? '/tmp/paperclip-tool-projection' : process.argv[index + 1]);
|
||||
assert.equal(typeof global.gc, 'function', 'Run Node with --expose-gc');
|
||||
await mkdir(output, { recursive: true });
|
||||
const database = await startEmbeddedPostgresTestDatabase('paperclip-hotpaths-http-owned-');
|
||||
const db = createDb(database.connectionString, { maxConnections: 1 });
|
||||
const server = createServer((_request, response) => { response.writeHead(200, { 'Content-Type': 'application/json' }); response.end('{"status":"ok"}'); });
|
||||
let probe;
|
||||
const control = async message => {
|
||||
const reply = once(probe, 'message', { signal: AbortSignal.timeout(30000) });
|
||||
probe.send(message);
|
||||
return (await reply)[0];
|
||||
};
|
||||
const records = [];
|
||||
try {
|
||||
server.listen(0, '127.0.0.1'); await once(server, 'listening');
|
||||
probe = fork(new URL('./probe.mjs', import.meta.url), { execArgv: [], stdio: ['ignore', 'inherit', 'inherit', 'ipc'] });
|
||||
const fixture = await createProfileProjectionFixture(db);
|
||||
const variants = {}, now = () => new Date('2026-02-01T00:00:00Z');
|
||||
for (const label of ['baseline', 'combined']) {
|
||||
const p = label === 'combined' ? 'service' : '.baseline-service';
|
||||
const a = label === 'combined' ? 'attention' : '.baseline-attention';
|
||||
const profiles = (await import(new URL(`../../dist/services/tool-access/profiles/${p}.js`, import.meta.url))).createToolProfileService(db, now);
|
||||
const attention = (await import(new URL(`../../dist/services/tool-access/catalog/${a}.js`, import.meta.url))).createToolAppAttention(db, now);
|
||||
variants[label] = { profiles: () => profiles.listProfiles(fixture.company.id), attention: () => attention(fixture.company.id) };
|
||||
}
|
||||
for (const variant of Object.values(variants)) for (const owner of ['profiles', 'attention']) await variant[owner]();
|
||||
for (let round = 0; round < 3; round++) {
|
||||
for (const owner of ['profiles', 'attention']) {
|
||||
for (const label of round % 2 ? ['combined', 'baseline'] : ['baseline', 'combined']) {
|
||||
global.gc?.();
|
||||
await control({ kind: 'arm', port: server.address().port });
|
||||
const start = performance.now();
|
||||
await variants[label][owner]();
|
||||
const operationMs = performance.now() - start;
|
||||
const result = await control({ kind: 'stop' });
|
||||
const good = result.times.filter(value => typeof value === 'number');
|
||||
const record = { kind: 'responsiveness', round, owner, label, operationMs,
|
||||
completedHealthRequests: good.length, maxHealthMs: good.length ? Math.max(...good) : null,
|
||||
errors: result.times.filter(value => typeof value !== 'number'),
|
||||
scope: 'Separate-process HTTP probe to a minimal liveness handler in the same Node event loop as the real service; not the full application health route' };
|
||||
records.push(record); console.log(JSON.stringify(record));
|
||||
}
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
try {
|
||||
await writeFile(path.join(output, 'responsiveness.json'), JSON.stringify(records, null, 2));
|
||||
} finally {
|
||||
try {
|
||||
if (probe && probe.exitCode === null && probe.signalCode === null) {
|
||||
const exited = once(probe, 'exit');
|
||||
if (probe.connected) probe.send({ kind: 'close' });
|
||||
else probe.kill();
|
||||
await exited;
|
||||
}
|
||||
} finally {
|
||||
try { await new Promise(resolve => server.close(resolve)); }
|
||||
finally {
|
||||
try { await db.$client.end({ timeout: 1 }); }
|
||||
finally { await database.cleanup(); }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user