pgserve 2.1.2 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +86 -0
- package/README.md +105 -1
- package/bin/autopg-wrapper.cjs +16 -0
- package/bin/pgserve-wrapper.cjs +31 -6
- package/bin/postgres-server.js +80 -7
- package/console/README.md +131 -0
- package/console/api.js +173 -0
- package/console/app.jsx +483 -0
- package/console/colors_and_type.css +227 -0
- package/console/components.jsx +167 -0
- package/console/console.css +1666 -0
- package/console/data.jsx +350 -0
- package/console/index.html +31 -0
- package/console/screens/databases.jsx +5 -0
- package/console/screens/health.jsx +5 -0
- package/console/screens/ingress.jsx +5 -0
- package/console/screens/optimizer.jsx +5 -0
- package/console/screens/rlm-sim.jsx +5 -0
- package/console/screens/rlm-trace.jsx +5 -0
- package/console/screens/security.jsx +5 -0
- package/console/screens/settings.jsx +611 -0
- package/console/screens/sql.jsx +5 -0
- package/console/screens/sync.jsx +5 -0
- package/console/screens/tables.jsx +5 -0
- package/console/tweaks-panel.jsx +425 -0
- package/package.json +11 -1
- package/src/cli-config.cjs +310 -0
- package/src/cli-install.cjs +98 -11
- package/src/cli-restart.cjs +228 -0
- package/src/cli-ui.cjs +580 -0
- package/src/cluster.js +43 -38
- package/src/postgres.js +141 -19
- package/src/settings-loader.cjs +235 -0
- package/src/settings-migrate.cjs +212 -0
- package/src/settings-pg-args.cjs +146 -0
- package/src/settings-schema.cjs +422 -0
- package/src/settings-validator.cjs +416 -0
- package/src/settings-writer.cjs +288 -0
- package/.claude/context/windows-debug.md +0 -119
- package/.genie/AGENTS.md +0 -15
- package/.genie/agents/README.md +0 -110
- package/.genie/agents/analyze.md +0 -176
- package/.genie/agents/forge.md +0 -290
- package/.genie/agents/garbage-cleaner.md +0 -324
- package/.genie/agents/garbage-collector.md +0 -596
- package/.genie/agents/github-issue-gc.md +0 -618
- package/.genie/agents/review.md +0 -380
- package/.genie/agents/semantic-analyzer/find-duplicates.md +0 -90
- package/.genie/agents/semantic-analyzer/find-orphans.md +0 -99
- package/.genie/agents/semantic-analyzer.md +0 -101
- package/.genie/agents/update.md +0 -182
- package/.genie/agents/wish.md +0 -357
- package/.genie/brainstorms/pgserve-v2/DESIGN.md +0 -174
- package/.genie/code/AGENTS.md +0 -694
- package/.genie/code/agents/audit/risk.md +0 -173
- package/.genie/code/agents/audit/security.md +0 -189
- package/.genie/code/agents/audit.md +0 -145
- package/.genie/code/agents/challenge.md +0 -230
- package/.genie/code/agents/change-reviewer.md +0 -295
- package/.genie/code/agents/code-garbage-collector.md +0 -425
- package/.genie/code/agents/code-quality.md +0 -410
- package/.genie/code/agents/commit-suggester.md +0 -255
- package/.genie/code/agents/commit.md +0 -124
- package/.genie/code/agents/consensus.md +0 -204
- package/.genie/code/agents/daily-standup.md +0 -722
- package/.genie/code/agents/docgen.md +0 -48
- package/.genie/code/agents/explore.md +0 -79
- package/.genie/code/agents/fix.md +0 -100
- package/.genie/code/agents/git/commit-advisory.md +0 -219
- package/.genie/code/agents/git/workflows/issue.md +0 -244
- package/.genie/code/agents/git/workflows/pr.md +0 -179
- package/.genie/code/agents/git/workflows/release.md +0 -460
- package/.genie/code/agents/git/workflows/report.md +0 -342
- package/.genie/code/agents/git.md +0 -432
- package/.genie/code/agents/implementor.md +0 -161
- package/.genie/code/agents/install.md +0 -515
- package/.genie/code/agents/issue-creator.md +0 -344
- package/.genie/code/agents/polish.md +0 -116
- package/.genie/code/agents/qa.md +0 -653
- package/.genie/code/agents/refactor.md +0 -294
- package/.genie/code/agents/release.md +0 -1129
- package/.genie/code/agents/roadmap.md +0 -885
- package/.genie/code/agents/tests.md +0 -557
- package/.genie/code/agents/tracer.md +0 -50
- package/.genie/code/agents/update/upstream-update.md +0 -85
- package/.genie/code/agents/update/versions/generic-update.md +0 -305
- package/.genie/code/agents/vibe.md +0 -1317
- package/.genie/code/spells/agent-configuration.md +0 -58
- package/.genie/code/spells/automated-rc-publishing.md +0 -106
- package/.genie/code/spells/branch-tracker-guidance.md +0 -28
- package/.genie/code/spells/debug.md +0 -320
- package/.genie/code/spells/emoji-naming-convention.md +0 -303
- package/.genie/code/spells/evidence-storage.md +0 -26
- package/.genie/code/spells/file-naming-rules.md +0 -35
- package/.genie/code/spells/forge-code-blueprints.md +0 -195
- package/.genie/code/spells/genie-integration.md +0 -153
- package/.genie/code/spells/publishing-protocol.md +0 -61
- package/.genie/code/spells/team-consultation-protocol.md +0 -284
- package/.genie/code/spells/tool-requirements.md +0 -20
- package/.genie/code/spells/triad-maintenance-protocol.md +0 -154
- package/.genie/code/teams/tech-council/council.md +0 -328
- package/.genie/code/teams/tech-council/jt.md +0 -352
- package/.genie/code/teams/tech-council/nayr.md +0 -305
- package/.genie/code/teams/tech-council/oettam.md +0 -375
- package/.genie/neurons/README.md +0 -193
- package/.genie/neurons/forge.md +0 -106
- package/.genie/neurons/genie.md +0 -63
- package/.genie/neurons/review.md +0 -106
- package/.genie/neurons/wish.md +0 -104
- package/.genie/product/README.md +0 -20
- package/.genie/product/cli-automation.md +0 -359
- package/.genie/product/environment.md +0 -60
- package/.genie/product/mission.md +0 -60
- package/.genie/product/roadmap.md +0 -44
- package/.genie/product/tech-stack.md +0 -34
- package/.genie/product/templates/context-template.md +0 -218
- package/.genie/product/templates/qa-done-report-template.md +0 -68
- package/.genie/product/templates/review-report-template.md +0 -89
- package/.genie/product/templates/wish-template.md +0 -120
- package/.genie/scripts/helpers/analyze-commit.js +0 -195
- package/.genie/scripts/helpers/bullet-counter.js +0 -194
- package/.genie/scripts/helpers/bullet-find.js +0 -289
- package/.genie/scripts/helpers/bullet-id.js +0 -244
- package/.genie/scripts/helpers/check-secrets.js +0 -237
- package/.genie/scripts/helpers/count-tokens.js +0 -200
- package/.genie/scripts/helpers/create-frontmatter.js +0 -456
- package/.genie/scripts/helpers/detect-markers.js +0 -293
- package/.genie/scripts/helpers/detect-todos.js +0 -267
- package/.genie/scripts/helpers/detect-unlabeled-blocks.js +0 -135
- package/.genie/scripts/helpers/embeddings.js +0 -344
- package/.genie/scripts/helpers/find-empty-sections.js +0 -158
- package/.genie/scripts/helpers/index.js +0 -319
- package/.genie/scripts/helpers/validate-frontmatter.js +0 -578
- package/.genie/scripts/helpers/validate-links.js +0 -207
- package/.genie/scripts/helpers/validate-paths.js +0 -373
- package/.genie/spells/README.md +0 -9
- package/.genie/spells/ace-protocol.md +0 -118
- package/.genie/spells/ask-one-at-a-time.md +0 -175
- package/.genie/spells/backup-analyzer.md +0 -542
- package/.genie/spells/blocker.md +0 -12
- package/.genie/spells/break-things-move-fast.md +0 -56
- package/.genie/spells/context-candidates.md +0 -72
- package/.genie/spells/context-critic.md +0 -51
- package/.genie/spells/defer-to-expertise.md +0 -278
- package/.genie/spells/delegate-dont-do.md +0 -292
- package/.genie/spells/error-investigation-protocol.md +0 -328
- package/.genie/spells/evidence-based-completion.md +0 -273
- package/.genie/spells/experiment.md +0 -65
- package/.genie/spells/file-creation-protocol.md +0 -229
- package/.genie/spells/forge-integration.md +0 -281
- package/.genie/spells/forge-orchestration.md +0 -514
- package/.genie/spells/gather-context.md +0 -18
- package/.genie/spells/global-health-check.md +0 -34
- package/.genie/spells/global-noop-roundtrip.md +0 -25
- package/.genie/spells/install-genie.md +0 -1232
- package/.genie/spells/install.md +0 -82
- package/.genie/spells/investigate-before-commit.md +0 -112
- package/.genie/spells/know-yourself.md +0 -288
- package/.genie/spells/learn.md +0 -828
- package/.genie/spells/mcp-diagnostic-protocol.md +0 -246
- package/.genie/spells/mcp-first.md +0 -124
- package/.genie/spells/multi-step-execution.md +0 -67
- package/.genie/spells/orchestration-boundary-protocol.md +0 -256
- package/.genie/spells/orchestrator-not-implementor.md +0 -189
- package/.genie/spells/prompt.md +0 -746
- package/.genie/spells/reflect.md +0 -404
- package/.genie/spells/routing-decision-matrix.md +0 -368
- package/.genie/spells/run-in-parallel.md +0 -12
- package/.genie/spells/session-state-updater-example.md +0 -196
- package/.genie/spells/session-state-updater.md +0 -220
- package/.genie/spells/track-long-running-tasks.md +0 -133
- package/.genie/spells/troubleshoot-infrastructure.md +0 -176
- package/.genie/spells/upgrade-genie.md +0 -415
- package/.genie/spells/url-presentation-protocol.md +0 -301
- package/.genie/spells/wish-initiation.md +0 -158
- package/.genie/spells/wish-issue-linkage.md +0 -410
- package/.genie/spells/wish-lifecycle.md +0 -100
- package/.genie/state/provider-status.json +0 -3
- package/.genie/state/version.json +0 -16
- package/.genie/wishes/canonical-pgserve-pm2-supervision/WISH.md +0 -290
- package/.genie/wishes/pgserve-v2/BRIEF-from-genie-pgserve.md +0 -99
- package/.genie/wishes/pgserve-v2/WISH.md +0 -442
- package/.genie/wishes/release-system-genie-pattern/WISH.md +0 -268
- package/.genie/wishes/release-system-genie-pattern/validation.md +0 -205
- package/.gitguardian.yaml +0 -29
- package/.gitguardianignore +0 -16
- package/.github/workflows/ci.yml +0 -122
- package/.github/workflows/release.yml +0 -289
- package/.github/workflows/version.yml +0 -228
- package/.husky/pre-commit +0 -2
- package/AGENTS.md +0 -433
- package/CLAUDE.md +0 -1
- package/Makefile +0 -285
- package/assets/icon.ico +0 -0
- package/bun.lock +0 -435
- package/bunfig.toml +0 -28
- package/ecosystem.config.cjs +0 -23
- package/eslint.config.js +0 -63
- package/examples/multi-tenant-demo.js +0 -104
- package/install.sh +0 -123
- package/knip.json +0 -9
- package/scripts/test-bun-self-heal.sh +0 -163
- package/scripts/test-npx.sh +0 -60
- package/tests/audit.test.js +0 -189
- package/tests/backpressure.test.js +0 -167
- package/tests/benchmarks/runner.js +0 -1197
- package/tests/benchmarks/vector-generator.js +0 -368
- package/tests/cli-install.test.js +0 -322
- package/tests/control-db.test.js +0 -285
- package/tests/daemon-control.test.js +0 -171
- package/tests/daemon-fingerprint-integration.test.js +0 -111
- package/tests/daemon-pr24-regression.test.js +0 -198
- package/tests/fingerprint.test.js +0 -263
- package/tests/fixtures/240-orphan-seed.sql +0 -30
- package/tests/multi-tenant.test.js +0 -374
- package/tests/orphan-cleanup.test.js +0 -390
- package/tests/pg-version-regex.test.js +0 -129
- package/tests/quick-bench.js +0 -135
- package/tests/router-handshake-retry.test.js +0 -119
- package/tests/router-handshake-watchdog.test.js +0 -110
- package/tests/sdk.test.js +0 -71
- package/tests/stale-postmaster-pid.test.js +0 -85
- package/tests/stress-test.js +0 -439
- package/tests/sync-perf-test.js +0 -150
- package/tests/tcp-listen.test.js +0 -368
- package/tests/tenancy.test.js +0 -403
- package/tests/wrapper-supervision.test.js +0 -107
|
@@ -1,390 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Group 5 — orphan cleanup harness.
|
|
3
|
-
*
|
|
4
|
-
* Boots a real pgserve daemon (no GC triggers — we drive sweeps manually so
|
|
5
|
-
* we can assert exact counts and latency), applies the 240-orphan SQL
|
|
6
|
-
* fixture, creates 240 matching empty databases, runs one `gcSweep`, then
|
|
7
|
-
* asserts:
|
|
8
|
-
* - all 240 rows gone from pgserve_meta
|
|
9
|
-
* - all 240 user databases gone from pg_database
|
|
10
|
-
* - audit log emitted 240 `db_reaped_*` events
|
|
11
|
-
*
|
|
12
|
-
* Plus the auxiliary cases the wish demands:
|
|
13
|
-
* - persist=true row is exempt (audited as db_persist_honored, never reaped)
|
|
14
|
-
* - live liveness_pid + stale last_connection_at slides the window forward
|
|
15
|
-
* instead of reaping
|
|
16
|
-
* - on-connect sweep listener returns under 50ms P99 (sweep is detached;
|
|
17
|
-
* accept must not block on it)
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
import {
|
|
21
|
-
describe,
|
|
22
|
-
test,
|
|
23
|
-
expect,
|
|
24
|
-
beforeAll,
|
|
25
|
-
afterAll,
|
|
26
|
-
beforeEach,
|
|
27
|
-
} from 'bun:test';
|
|
28
|
-
import fs from 'fs';
|
|
29
|
-
import os from 'os';
|
|
30
|
-
import path from 'path';
|
|
31
|
-
|
|
32
|
-
import {
|
|
33
|
-
PgserveDaemon,
|
|
34
|
-
resolveControlSocketPath,
|
|
35
|
-
resolvePidLockPath,
|
|
36
|
-
resolveLibpqCompatPath,
|
|
37
|
-
} from '../src/daemon.js';
|
|
38
|
-
import { _setPeerCredImpl, initFingerprintFfi } from '../src/fingerprint.js';
|
|
39
|
-
import { configureAudit, AUDIT_EVENTS } from '../src/audit.js';
|
|
40
|
-
import { gcSweep, installSweepTriggers } from '../src/gc.js';
|
|
41
|
-
import { createLogger } from '../src/logger.js';
|
|
42
|
-
|
|
43
|
-
const FIXTURE_PATH = path.join(__dirname, 'fixtures', '240-orphan-seed.sql');
|
|
44
|
-
const ORPHAN_COUNT = 240;
|
|
45
|
-
|
|
46
|
-
let scratchDir;
|
|
47
|
-
let auditFile;
|
|
48
|
-
let savedAuditDefaults;
|
|
49
|
-
let daemon;
|
|
50
|
-
let adminClient;
|
|
51
|
-
|
|
52
|
-
beforeAll(async () => {
|
|
53
|
-
await initFingerprintFfi();
|
|
54
|
-
_setPeerCredImpl(() => ({
|
|
55
|
-
pid: process.pid,
|
|
56
|
-
uid: process.getuid(),
|
|
57
|
-
gid: process.getgid(),
|
|
58
|
-
}));
|
|
59
|
-
|
|
60
|
-
scratchDir = fs.mkdtempSync(path.join(os.tmpdir(), 'pgserve-gc-test-'));
|
|
61
|
-
const controlSocketDir = path.join(scratchDir, 'sock');
|
|
62
|
-
fs.mkdirSync(controlSocketDir, { recursive: true });
|
|
63
|
-
auditFile = path.join(scratchDir, 'audit.log');
|
|
64
|
-
|
|
65
|
-
savedAuditDefaults = {
|
|
66
|
-
logFile: path.join(os.homedir(), '.pgserve', 'audit.log'),
|
|
67
|
-
target: process.env.PGSERVE_AUDIT_TARGET || 'file',
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
daemon = new PgserveDaemon({
|
|
71
|
-
controlSocketDir,
|
|
72
|
-
controlSocketPath: resolveControlSocketPath(controlSocketDir),
|
|
73
|
-
pidLockPath: resolvePidLockPath(controlSocketDir),
|
|
74
|
-
libpqCompatPath: resolveLibpqCompatPath(controlSocketDir, 5432),
|
|
75
|
-
auditLogFile: auditFile,
|
|
76
|
-
auditTarget: 'file',
|
|
77
|
-
pgPort: 16720,
|
|
78
|
-
logger: createLogger({ level: process.env.LOG_LEVEL || 'warn' }),
|
|
79
|
-
// Tests drive sweeps explicitly — disable the auto-installed boot
|
|
80
|
-
// sweep + hourly timer + on-connect listener.
|
|
81
|
-
gcEnabled: false,
|
|
82
|
-
});
|
|
83
|
-
await daemon.start();
|
|
84
|
-
adminClient = daemon._adminClient;
|
|
85
|
-
}, 90_000);
|
|
86
|
-
|
|
87
|
-
afterAll(async () => {
|
|
88
|
-
try {
|
|
89
|
-
if (adminClient) {
|
|
90
|
-
const r = await adminClient.query(`
|
|
91
|
-
SELECT datname FROM pg_database
|
|
92
|
-
WHERE datname LIKE 'app_%' AND datistemplate = false
|
|
93
|
-
`);
|
|
94
|
-
for (const row of r.rows) {
|
|
95
|
-
try {
|
|
96
|
-
await adminClient.query(
|
|
97
|
-
`SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = $1`,
|
|
98
|
-
[row.datname],
|
|
99
|
-
);
|
|
100
|
-
await adminClient.query(`DROP DATABASE IF EXISTS "${row.datname}"`);
|
|
101
|
-
} catch { /* swallow */ }
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
} catch { /* swallow */ }
|
|
105
|
-
try { await daemon?.stop(); } catch { /* swallow */ }
|
|
106
|
-
_setPeerCredImpl(null);
|
|
107
|
-
if (savedAuditDefaults) configureAudit(savedAuditDefaults);
|
|
108
|
-
try { fs.rmSync(scratchDir, { recursive: true, force: true }); } catch { /* swallow */ }
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
beforeEach(async () => {
|
|
112
|
-
// Reset audit log so each test sees only its own events.
|
|
113
|
-
try { fs.writeFileSync(auditFile, '', { mode: 0o600 }); } catch { /* swallow */ }
|
|
114
|
-
// Reset pgserve_meta + drop any leftover app_* DBs from prior tests.
|
|
115
|
-
await adminClient.query('TRUNCATE pgserve_meta');
|
|
116
|
-
const r = await adminClient.query(`
|
|
117
|
-
SELECT datname FROM pg_database
|
|
118
|
-
WHERE datname LIKE 'app_%' AND datistemplate = false
|
|
119
|
-
`);
|
|
120
|
-
for (const row of r.rows) {
|
|
121
|
-
try {
|
|
122
|
-
await adminClient.query(
|
|
123
|
-
`SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = $1`,
|
|
124
|
-
[row.datname],
|
|
125
|
-
);
|
|
126
|
-
await adminClient.query(`DROP DATABASE IF EXISTS "${row.datname}"`);
|
|
127
|
-
} catch { /* swallow */ }
|
|
128
|
-
}
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
function readAudit() {
|
|
132
|
-
if (!fs.existsSync(auditFile)) return [];
|
|
133
|
-
return fs.readFileSync(auditFile, 'utf8')
|
|
134
|
-
.split('\n')
|
|
135
|
-
.filter(Boolean)
|
|
136
|
-
.map((l) => JSON.parse(l));
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
async function applyFixture() {
|
|
140
|
-
const sql = fs.readFileSync(FIXTURE_PATH, 'utf8');
|
|
141
|
-
await adminClient.query(sql);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
async function createSeededDatabases() {
|
|
145
|
-
// The fixture writes 240 deterministic database_name values. Read them
|
|
146
|
-
// back and materialise the matching empty databases. Run in batches so
|
|
147
|
-
// the embedded PG admin pool isn't swamped (default max=2).
|
|
148
|
-
const r = await adminClient.query(
|
|
149
|
-
`SELECT database_name FROM pgserve_meta ORDER BY database_name`,
|
|
150
|
-
);
|
|
151
|
-
const names = r.rows.map((row) => row.database_name);
|
|
152
|
-
const batchSize = 8;
|
|
153
|
-
for (let i = 0; i < names.length; i += batchSize) {
|
|
154
|
-
const slice = names.slice(i, i + batchSize);
|
|
155
|
-
await Promise.all(slice.map((dbName) =>
|
|
156
|
-
adminClient.query(`CREATE DATABASE "${dbName}"`).catch((err) => {
|
|
157
|
-
// 42P04 = duplicate_database — tolerated, the DB already exists
|
|
158
|
-
// from a prior test run that bailed before cleanup.
|
|
159
|
-
if (!(err?.code === '42P04' || /already exists/i.test(err?.message || ''))) {
|
|
160
|
-
throw err;
|
|
161
|
-
}
|
|
162
|
-
}),
|
|
163
|
-
));
|
|
164
|
-
}
|
|
165
|
-
return names;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
async function countMetaRows() {
|
|
169
|
-
const r = await adminClient.query(`SELECT count(*)::int AS n FROM pgserve_meta`);
|
|
170
|
-
return r.rows[0].n;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
async function countUserDatabases() {
|
|
174
|
-
const r = await adminClient.query(`
|
|
175
|
-
SELECT count(*)::int AS n FROM pg_database
|
|
176
|
-
WHERE datname LIKE 'app_orphan_%' AND datistemplate = false
|
|
177
|
-
`);
|
|
178
|
-
return r.rows[0].n;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
describe('gcSweep: 240-orphan fixture', () => {
|
|
182
|
-
test('one sweep reaps all 240 ephemeral orphans', async () => {
|
|
183
|
-
await applyFixture();
|
|
184
|
-
await createSeededDatabases();
|
|
185
|
-
|
|
186
|
-
expect(await countMetaRows()).toBe(ORPHAN_COUNT);
|
|
187
|
-
expect(await countUserDatabases()).toBe(ORPHAN_COUNT);
|
|
188
|
-
|
|
189
|
-
const result = await gcSweep({
|
|
190
|
-
adminClient,
|
|
191
|
-
pgManager: daemon.pgManager,
|
|
192
|
-
now: new Date(),
|
|
193
|
-
logger: daemon.logger,
|
|
194
|
-
});
|
|
195
|
-
|
|
196
|
-
expect(result.examined).toBe(ORPHAN_COUNT);
|
|
197
|
-
expect(result.reaped).toBe(ORPHAN_COUNT);
|
|
198
|
-
expect(result.kept).toBe(0);
|
|
199
|
-
|
|
200
|
-
// pgserve_meta empty.
|
|
201
|
-
expect(await countMetaRows()).toBe(0);
|
|
202
|
-
// pg_database has no app_orphan_* rows left.
|
|
203
|
-
expect(await countUserDatabases()).toBe(0);
|
|
204
|
-
|
|
205
|
-
const events = readAudit();
|
|
206
|
-
const reapEvents = events.filter(
|
|
207
|
-
(e) => e.event === AUDIT_EVENTS.DB_REAPED_TTL ||
|
|
208
|
-
e.event === AUDIT_EVENTS.DB_REAPED_LIVENESS,
|
|
209
|
-
);
|
|
210
|
-
expect(reapEvents.length).toBe(ORPHAN_COUNT);
|
|
211
|
-
|
|
212
|
-
// Fixture splits 50/50 between liveness_pid=NULL and a dead pid →
|
|
213
|
-
// both audit code paths fire.
|
|
214
|
-
const ttl = events.filter((e) => e.event === AUDIT_EVENTS.DB_REAPED_TTL);
|
|
215
|
-
const liveness = events.filter((e) => e.event === AUDIT_EVENTS.DB_REAPED_LIVENESS);
|
|
216
|
-
expect(ttl.length).toBe(120);
|
|
217
|
-
expect(liveness.length).toBe(120);
|
|
218
|
-
}, 120_000);
|
|
219
|
-
});
|
|
220
|
-
|
|
221
|
-
describe('gcSweep: persist + liveness exemptions', () => {
|
|
222
|
-
test('persist=true row is never reaped, even past TTL', async () => {
|
|
223
|
-
// Seed one persist=true row past TTL plus one ephemeral past TTL.
|
|
224
|
-
await adminClient.query(`
|
|
225
|
-
INSERT INTO pgserve_meta (
|
|
226
|
-
database_name, fingerprint, peer_uid, last_connection_at, liveness_pid, persist
|
|
227
|
-
) VALUES
|
|
228
|
-
('app_persist_aaaaaaaaaaaa', 'aaaaaaaaaaaa', 1000, now() - interval '48 hours', NULL, true),
|
|
229
|
-
('app_orphan_bbbbbbbbbbbb', 'bbbbbbbbbbbb', 1000, now() - interval '48 hours', NULL, false)
|
|
230
|
-
`);
|
|
231
|
-
await adminClient.query(`CREATE DATABASE "app_persist_aaaaaaaaaaaa"`);
|
|
232
|
-
await adminClient.query(`CREATE DATABASE "app_orphan_bbbbbbbbbbbb"`);
|
|
233
|
-
|
|
234
|
-
const result = await gcSweep({
|
|
235
|
-
adminClient,
|
|
236
|
-
pgManager: daemon.pgManager,
|
|
237
|
-
now: new Date(),
|
|
238
|
-
logger: daemon.logger,
|
|
239
|
-
});
|
|
240
|
-
|
|
241
|
-
// The persist=true row never appears via forEachReapable (the SQL
|
|
242
|
-
// filter excludes it), so result.examined == 1 (only the orphan).
|
|
243
|
-
expect(result.reaped).toBe(1);
|
|
244
|
-
expect(result.reapedNames).toEqual(['app_orphan_bbbbbbbbbbbb']);
|
|
245
|
-
|
|
246
|
-
const remaining = await adminClient.query(
|
|
247
|
-
`SELECT database_name, persist FROM pgserve_meta ORDER BY database_name`,
|
|
248
|
-
);
|
|
249
|
-
expect(remaining.rows).toEqual([
|
|
250
|
-
{ database_name: 'app_persist_aaaaaaaaaaaa', persist: true },
|
|
251
|
-
]);
|
|
252
|
-
|
|
253
|
-
const persistDb = await adminClient.query(
|
|
254
|
-
`SELECT 1 FROM pg_database WHERE datname = 'app_persist_aaaaaaaaaaaa'`,
|
|
255
|
-
);
|
|
256
|
-
expect(persistDb.rows.length).toBe(1);
|
|
257
|
-
}, 60_000);
|
|
258
|
-
|
|
259
|
-
test('live liveness_pid + stale last_connection_at slides window, no reap', async () => {
|
|
260
|
-
const livePid = process.pid; // self — guaranteed alive
|
|
261
|
-
await adminClient.query(`
|
|
262
|
-
INSERT INTO pgserve_meta (
|
|
263
|
-
database_name, fingerprint, peer_uid, last_connection_at, liveness_pid, persist
|
|
264
|
-
) VALUES
|
|
265
|
-
('app_live_cccccccccccc', 'cccccccccccc', 1000, now() - interval '48 hours', $1, false)
|
|
266
|
-
`, [livePid]);
|
|
267
|
-
await adminClient.query(`CREATE DATABASE "app_live_cccccccccccc"`);
|
|
268
|
-
|
|
269
|
-
const before = await adminClient.query(
|
|
270
|
-
`SELECT last_connection_at FROM pgserve_meta WHERE database_name = $1`,
|
|
271
|
-
['app_live_cccccccccccc'],
|
|
272
|
-
);
|
|
273
|
-
const beforeMs = before.rows[0].last_connection_at.getTime();
|
|
274
|
-
|
|
275
|
-
const result = await gcSweep({
|
|
276
|
-
adminClient,
|
|
277
|
-
pgManager: daemon.pgManager,
|
|
278
|
-
now: new Date(),
|
|
279
|
-
logger: daemon.logger,
|
|
280
|
-
});
|
|
281
|
-
|
|
282
|
-
expect(result.reaped).toBe(0);
|
|
283
|
-
expect(result.aliveSkipped).toBe(1);
|
|
284
|
-
|
|
285
|
-
const after = await adminClient.query(
|
|
286
|
-
`SELECT last_connection_at FROM pgserve_meta WHERE database_name = $1`,
|
|
287
|
-
['app_live_cccccccccccc'],
|
|
288
|
-
);
|
|
289
|
-
expect(after.rows.length).toBe(1);
|
|
290
|
-
const afterMs = after.rows[0].last_connection_at.getTime();
|
|
291
|
-
// Slid forward: new timestamp > old by at least the staleness gap.
|
|
292
|
-
expect(afterMs).toBeGreaterThan(beforeMs + 24 * 60 * 60 * 1000);
|
|
293
|
-
}, 60_000);
|
|
294
|
-
});
|
|
295
|
-
|
|
296
|
-
describe('installSweepTriggers: on-connect sweep is non-blocking', () => {
|
|
297
|
-
test('emit("accept") returns under 50ms P99 even with always-sample rate', async () => {
|
|
298
|
-
// Use a stub admin client that simulates a slow GC (artificially long
|
|
299
|
-
// pgserve_meta query). If the listener weren't detached, every emit()
|
|
300
|
-
// would wait on this — the test would time out at 200ms × N samples.
|
|
301
|
-
let sweepCount = 0;
|
|
302
|
-
const slowAdmin = {
|
|
303
|
-
async query(_text, _params) {
|
|
304
|
-
await new Promise((resolve) => setTimeout(resolve, 200));
|
|
305
|
-
sweepCount += 1;
|
|
306
|
-
return { rows: [], rowCount: 0 };
|
|
307
|
-
},
|
|
308
|
-
};
|
|
309
|
-
// Force "always sample" by passing getDbCount = 1 (so N = max(1,1/10) = 1)
|
|
310
|
-
// and dbCount low enough that the rate is 1/1 = always.
|
|
311
|
-
const handle = installSweepTriggers(daemon, {
|
|
312
|
-
adminClient: slowAdmin,
|
|
313
|
-
intervalMs: 0,
|
|
314
|
-
bootSweep: false,
|
|
315
|
-
getDbCount: () => 1,
|
|
316
|
-
});
|
|
317
|
-
try {
|
|
318
|
-
const samples = [];
|
|
319
|
-
for (let i = 0; i < 100; i++) {
|
|
320
|
-
const t0 = process.hrtime.bigint();
|
|
321
|
-
daemon.emit('accept', { fingerprint: 'aaaaaaaaaaaa', socket: {} });
|
|
322
|
-
const t1 = process.hrtime.bigint();
|
|
323
|
-
samples.push(Number(t1 - t0) / 1e6); // ns → ms
|
|
324
|
-
}
|
|
325
|
-
samples.sort((a, b) => a - b);
|
|
326
|
-
const p99 = samples[Math.floor(samples.length * 0.99) - 1];
|
|
327
|
-
expect(p99).toBeLessThan(50);
|
|
328
|
-
} finally {
|
|
329
|
-
await handle.stop();
|
|
330
|
-
}
|
|
331
|
-
// Sanity: at least the boot=false branch ran, so sweepCount may be 0
|
|
332
|
-
// if the rate decided not to sample, but the latency check is the
|
|
333
|
-
// load-bearing assertion.
|
|
334
|
-
expect(sweepCount).toBeGreaterThanOrEqual(0);
|
|
335
|
-
}, 60_000);
|
|
336
|
-
});
|
|
337
|
-
|
|
338
|
-
describe('installSweepTriggers: boot sweep logs summary', () => {
|
|
339
|
-
test('boot sweep runs once and reports counts via logger.info', async () => {
|
|
340
|
-
// Seed three rows: two reapable, one persist.
|
|
341
|
-
await adminClient.query(`
|
|
342
|
-
INSERT INTO pgserve_meta (
|
|
343
|
-
database_name, fingerprint, peer_uid, last_connection_at, liveness_pid, persist
|
|
344
|
-
) VALUES
|
|
345
|
-
('app_boot_aaaaaaaaaaaa', 'aaaaaaaaaaaa', 1000, now() - interval '48 hours', NULL, false),
|
|
346
|
-
('app_boot_bbbbbbbbbbbb', 'bbbbbbbbbbbb', 1000, now() - interval '48 hours', NULL, false),
|
|
347
|
-
('app_boot_cccccccccccc', 'cccccccccccc', 1000, now() - interval '48 hours', NULL, true)
|
|
348
|
-
`);
|
|
349
|
-
await adminClient.query(`CREATE DATABASE "app_boot_aaaaaaaaaaaa"`);
|
|
350
|
-
await adminClient.query(`CREATE DATABASE "app_boot_bbbbbbbbbbbb"`);
|
|
351
|
-
await adminClient.query(`CREATE DATABASE "app_boot_cccccccccccc"`);
|
|
352
|
-
|
|
353
|
-
const calls = [];
|
|
354
|
-
const captureLogger = {
|
|
355
|
-
info: (...args) => calls.push({ level: 'info', args }),
|
|
356
|
-
warn: () => {},
|
|
357
|
-
error: () => {},
|
|
358
|
-
debug: () => {},
|
|
359
|
-
};
|
|
360
|
-
const stubDaemon = Object.assign(Object.create(daemon), {
|
|
361
|
-
logger: captureLogger,
|
|
362
|
-
});
|
|
363
|
-
// Object.create copies prototype, so emitter methods are inherited.
|
|
364
|
-
|
|
365
|
-
const handle = installSweepTriggers(stubDaemon, {
|
|
366
|
-
adminClient,
|
|
367
|
-
intervalMs: 0,
|
|
368
|
-
bootSweep: true,
|
|
369
|
-
});
|
|
370
|
-
try {
|
|
371
|
-
// Wait for setImmediate-scheduled boot sweep.
|
|
372
|
-
const deadline = Date.now() + 5000;
|
|
373
|
-
while (Date.now() < deadline) {
|
|
374
|
-
const summary = calls.find((c) =>
|
|
375
|
-
typeof c.args[1] === 'string' && c.args[1].includes('boot sweep complete'),
|
|
376
|
-
);
|
|
377
|
-
if (summary) break;
|
|
378
|
-
await new Promise((r) => setTimeout(r, 50));
|
|
379
|
-
}
|
|
380
|
-
const summary = calls.find((c) =>
|
|
381
|
-
typeof c.args[1] === 'string' && c.args[1].includes('boot sweep complete'),
|
|
382
|
-
);
|
|
383
|
-
expect(summary).toBeDefined();
|
|
384
|
-
expect(summary.args[0].reaped).toBe(2);
|
|
385
|
-
expect(summary.args[0].persist_skipped).toBe(0); // forEachReapable filter excludes persist=true rows from `examined` entirely
|
|
386
|
-
} finally {
|
|
387
|
-
await handle.stop();
|
|
388
|
-
}
|
|
389
|
-
}, 60_000);
|
|
390
|
-
});
|
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Regression test for pgvector auto-installer PG-major detection.
|
|
3
|
-
*
|
|
4
|
-
* `postgres --version` prints `postgres (PostgreSQL) 18.2`, so the regex that
|
|
5
|
-
* extracts the major version must tolerate the closing `)` between the
|
|
6
|
-
* product name and the number. An earlier pattern `/PostgreSQL (\d+)/`
|
|
7
|
-
* expected a digit immediately after `PostgreSQL ` and silently fell back to
|
|
8
|
-
* a hard-coded `'17'` default on PG14+, causing the wrong pgvector .deb to be
|
|
9
|
-
* downloaded and a later "incompatible library version mismatch" when
|
|
10
|
-
* `CREATE EXTENSION vector` was executed against a PG18 server.
|
|
11
|
-
*
|
|
12
|
-
* This test pins the corrected regex so the regression can't sneak back in.
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
import { test, expect, describe } from 'bun:test';
|
|
16
|
-
import { pgvectorMetaMatches } from '../src/postgres.js';
|
|
17
|
-
|
|
18
|
-
// Keep this in sync with `_detectPgMajor()` in src/postgres.js
|
|
19
|
-
const PG_VERSION_REGEX = /PostgreSQL\)?\s+(\d+)/;
|
|
20
|
-
|
|
21
|
-
function detectMajor(versionString) {
|
|
22
|
-
const match = versionString.match(PG_VERSION_REGEX);
|
|
23
|
-
return match ? match[1] : null;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
describe('PG major version detection for pgvector auto-install', () => {
|
|
27
|
-
test('parses "postgres (PostgreSQL) X.Y" format (actual postgres --version output)', () => {
|
|
28
|
-
expect(detectMajor('postgres (PostgreSQL) 18.2')).toBe('18');
|
|
29
|
-
expect(detectMajor('postgres (PostgreSQL) 17.4')).toBe('17');
|
|
30
|
-
expect(detectMajor('postgres (PostgreSQL) 16.0')).toBe('16');
|
|
31
|
-
expect(detectMajor('postgres (PostgreSQL) 14.11')).toBe('14');
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
test('parses pre-release labels', () => {
|
|
35
|
-
expect(detectMajor('postgres (PostgreSQL) 18.2-beta.1')).toBe('18');
|
|
36
|
-
expect(detectMajor('postgres (PostgreSQL) 18devel')).toBe('18');
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
test('parses bare "PostgreSQL X" format (no parentheses)', () => {
|
|
40
|
-
expect(detectMajor('PostgreSQL 18.2')).toBe('18');
|
|
41
|
-
expect(detectMajor('PostgreSQL 17')).toBe('17');
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
test('returns null on unparseable input so caller can fail loudly', () => {
|
|
45
|
-
expect(detectMajor('')).toBeNull();
|
|
46
|
-
expect(detectMajor('not postgres')).toBeNull();
|
|
47
|
-
expect(detectMajor('mysql 8.0')).toBeNull();
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Staleness detection tests for the pgvector auto-heal path.
|
|
53
|
-
*
|
|
54
|
-
* `pgvectorMetaMatches` decides whether an already-present vector.so on
|
|
55
|
-
* disk can be trusted (return true → reuse) or must be torn down and
|
|
56
|
-
* reinstalled (return false → heal). Getting this wrong in either
|
|
57
|
-
* direction is a production bug:
|
|
58
|
-
*
|
|
59
|
-
* - False positive (matches when it shouldn't) → stale PG17 .so stays on
|
|
60
|
-
* disk, CREATE EXTENSION dies with "incompatible library version" on
|
|
61
|
-
* PG18, brain-ingest blows up mid-run.
|
|
62
|
-
* - False negative (doesn't match when it should) → pgserve re-downloads
|
|
63
|
-
* pgvector on every start, wasting bandwidth and triggering
|
|
64
|
-
* apt.postgresql.org rate limits.
|
|
65
|
-
*
|
|
66
|
-
* These tests pin the exact matching semantics so the auto-heal doesn't
|
|
67
|
-
* silently regress.
|
|
68
|
-
*/
|
|
69
|
-
describe('pgvectorMetaMatches — pgvector install staleness detection', () => {
|
|
70
|
-
const RUNTIME = {
|
|
71
|
-
pgMajor: '18',
|
|
72
|
-
postgresPath: '/home/user/.pgserve/bin/linux-x64/bin/postgres',
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
test('matches when metadata pgMajor and postgresPath agree with runtime', () => {
|
|
76
|
-
const meta = {
|
|
77
|
-
pgMajor: '18',
|
|
78
|
-
pgvectorVersion: '0.8.1-2',
|
|
79
|
-
postgresPath: '/home/user/.pgserve/bin/linux-x64/bin/postgres',
|
|
80
|
-
installedAt: '2026-04-10T18:00:00.000Z',
|
|
81
|
-
};
|
|
82
|
-
expect(pgvectorMetaMatches(meta, RUNTIME)).toBe(true);
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
test('matches when postgresPath is absent (older metadata format)', () => {
|
|
86
|
-
const meta = { pgMajor: '18', pgvectorVersion: '0.8.1-2' };
|
|
87
|
-
expect(pgvectorMetaMatches(meta, RUNTIME)).toBe(true);
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
test('rejects when pgMajor differs — this is the PG17→PG18 regression we are healing', () => {
|
|
91
|
-
const stalePg17Meta = {
|
|
92
|
-
pgMajor: '17',
|
|
93
|
-
pgvectorVersion: '0.8.1-2',
|
|
94
|
-
postgresPath: '/home/user/.pgserve/bin/linux-x64/bin/postgres',
|
|
95
|
-
};
|
|
96
|
-
expect(pgvectorMetaMatches(stalePg17Meta, RUNTIME)).toBe(false);
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
test('rejects when postgresPath points at a different binary (pgserve upgraded)', () => {
|
|
100
|
-
const meta = {
|
|
101
|
-
pgMajor: '18',
|
|
102
|
-
postgresPath: '/opt/old-pgserve/bin/postgres',
|
|
103
|
-
};
|
|
104
|
-
expect(pgvectorMetaMatches(meta, RUNTIME)).toBe(false);
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
test('rejects null metadata (pre-auto-heal install without sidecar)', () => {
|
|
108
|
-
// This is the case that heals every existing broken deployment: they
|
|
109
|
-
// have vector.so on disk but no vector.meta.json, so match returns
|
|
110
|
-
// false → reinstall fires.
|
|
111
|
-
expect(pgvectorMetaMatches(null, RUNTIME)).toBe(false);
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
test('rejects non-object metadata (corrupted sidecar)', () => {
|
|
115
|
-
expect(pgvectorMetaMatches('18', RUNTIME)).toBe(false);
|
|
116
|
-
expect(pgvectorMetaMatches(42, RUNTIME)).toBe(false);
|
|
117
|
-
expect(pgvectorMetaMatches([], RUNTIME)).toBe(false);
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
test('rejects metadata missing pgMajor field', () => {
|
|
121
|
-
expect(pgvectorMetaMatches({ pgvectorVersion: '0.8.1' }, RUNTIME)).toBe(false);
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
test('rejects metadata where pgMajor is not a string', () => {
|
|
125
|
-
// JSON could hand us a number — match must be strict about type to
|
|
126
|
-
// avoid `18 == '18'` false positives masking a corrupted file.
|
|
127
|
-
expect(pgvectorMetaMatches({ pgMajor: 18 }, RUNTIME)).toBe(false);
|
|
128
|
-
});
|
|
129
|
-
});
|
package/tests/quick-bench.js
DELETED
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env bun
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Quick Benchmark - Run against external pgserve instance
|
|
5
|
-
* Usage: bun tests/quick-bench.js [port] [duration_seconds]
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import pg from 'pg';
|
|
9
|
-
const { Pool } = pg;
|
|
10
|
-
|
|
11
|
-
const PORT = parseInt(process.argv[2]) || 8433;
|
|
12
|
-
const DURATION_SEC = parseInt(process.argv[3]) || 30; // Run for 30 seconds by default
|
|
13
|
-
const CONNECTIONS = 20;
|
|
14
|
-
|
|
15
|
-
console.log(`
|
|
16
|
-
Quick Benchmark
|
|
17
|
-
===============
|
|
18
|
-
Target: postgresql://127.0.0.1:${PORT}/bench
|
|
19
|
-
Connections: ${CONNECTIONS} concurrent
|
|
20
|
-
Duration: ${DURATION_SEC} seconds
|
|
21
|
-
`);
|
|
22
|
-
|
|
23
|
-
const pool = new Pool({
|
|
24
|
-
host: '127.0.0.1',
|
|
25
|
-
port: PORT,
|
|
26
|
-
database: 'bench',
|
|
27
|
-
user: 'postgres',
|
|
28
|
-
password: 'postgres',
|
|
29
|
-
max: CONNECTIONS
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
let running = true;
|
|
33
|
-
let totalQueries = 0;
|
|
34
|
-
let errors = 0;
|
|
35
|
-
const latencies = [];
|
|
36
|
-
|
|
37
|
-
async function setup() {
|
|
38
|
-
const client = await pool.connect();
|
|
39
|
-
try {
|
|
40
|
-
await client.query(`
|
|
41
|
-
CREATE TABLE IF NOT EXISTS bench_test (
|
|
42
|
-
id SERIAL PRIMARY KEY,
|
|
43
|
-
data TEXT,
|
|
44
|
-
created_at TIMESTAMP DEFAULT NOW()
|
|
45
|
-
)
|
|
46
|
-
`);
|
|
47
|
-
await client.query('TRUNCATE bench_test');
|
|
48
|
-
} finally {
|
|
49
|
-
client.release();
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
async function runWorker(workerId) {
|
|
54
|
-
let i = 0;
|
|
55
|
-
while (running) {
|
|
56
|
-
const start = performance.now();
|
|
57
|
-
try {
|
|
58
|
-
// Mix of operations
|
|
59
|
-
const op = i % 3;
|
|
60
|
-
if (op === 0) {
|
|
61
|
-
await pool.query(
|
|
62
|
-
'INSERT INTO bench_test (data) VALUES ($1)',
|
|
63
|
-
[`worker-${workerId}-item-${i}-${Date.now()}`]
|
|
64
|
-
);
|
|
65
|
-
} else if (op === 1) {
|
|
66
|
-
await pool.query('SELECT * FROM bench_test ORDER BY id DESC LIMIT 10');
|
|
67
|
-
} else {
|
|
68
|
-
await pool.query('SELECT COUNT(*) FROM bench_test');
|
|
69
|
-
}
|
|
70
|
-
latencies.push(performance.now() - start);
|
|
71
|
-
totalQueries++;
|
|
72
|
-
i++;
|
|
73
|
-
} catch (err) {
|
|
74
|
-
errors++;
|
|
75
|
-
// Small delay on error to avoid tight loop
|
|
76
|
-
await new Promise(r => setTimeout(r, 10));
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
async function run() {
|
|
82
|
-
console.log('Setting up...');
|
|
83
|
-
await setup();
|
|
84
|
-
|
|
85
|
-
console.log(`Running for ${DURATION_SEC} seconds...\n`);
|
|
86
|
-
const start = performance.now();
|
|
87
|
-
|
|
88
|
-
// Start workers
|
|
89
|
-
const workers = Array.from({ length: CONNECTIONS }, (_, i) => runWorker(i));
|
|
90
|
-
|
|
91
|
-
// Progress updates every second
|
|
92
|
-
const progressInterval = setInterval(() => {
|
|
93
|
-
const elapsed = ((performance.now() - start) / 1000).toFixed(0);
|
|
94
|
-
const qps = totalQueries / (elapsed || 1);
|
|
95
|
-
process.stdout.write(`\r ${elapsed}s elapsed | ${totalQueries} queries | ${qps.toFixed(0)} QPS | ${errors} errors `);
|
|
96
|
-
}, 1000);
|
|
97
|
-
|
|
98
|
-
// Wait for duration
|
|
99
|
-
await new Promise(r => setTimeout(r, DURATION_SEC * 1000));
|
|
100
|
-
running = false;
|
|
101
|
-
|
|
102
|
-
// Wait for workers to finish current query
|
|
103
|
-
await Promise.all(workers.map(w => w.catch(() => {})));
|
|
104
|
-
clearInterval(progressInterval);
|
|
105
|
-
|
|
106
|
-
const totalTime = performance.now() - start;
|
|
107
|
-
|
|
108
|
-
// Calculate stats
|
|
109
|
-
latencies.sort((a, b) => a - b);
|
|
110
|
-
const sum = latencies.reduce((a, b) => a + b, 0);
|
|
111
|
-
const avg = latencies.length > 0 ? sum / latencies.length : 0;
|
|
112
|
-
const p50 = latencies[Math.floor(latencies.length * 0.5)] || 0;
|
|
113
|
-
const p95 = latencies[Math.floor(latencies.length * 0.95)] || 0;
|
|
114
|
-
const p99 = latencies[Math.floor(latencies.length * 0.99)] || 0;
|
|
115
|
-
const qps = (totalQueries / totalTime) * 1000;
|
|
116
|
-
|
|
117
|
-
console.log(`\n
|
|
118
|
-
Results
|
|
119
|
-
=======
|
|
120
|
-
Total time: ${(totalTime / 1000).toFixed(2)}s
|
|
121
|
-
Queries: ${totalQueries}
|
|
122
|
-
Errors: ${errors}
|
|
123
|
-
QPS: ${qps.toFixed(0)} queries/sec
|
|
124
|
-
|
|
125
|
-
Latency:
|
|
126
|
-
avg: ${avg.toFixed(2)}ms
|
|
127
|
-
p50: ${p50.toFixed(2)}ms
|
|
128
|
-
p95: ${p95.toFixed(2)}ms
|
|
129
|
-
p99: ${p99.toFixed(2)}ms
|
|
130
|
-
`);
|
|
131
|
-
|
|
132
|
-
await pool.end();
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
run().catch(console.error);
|