diff --git a/db/migrations/005_performance_indexes.sql b/db/migrations/005_performance_indexes.sql new file mode 100644 index 0000000..76f5afd --- /dev/null +++ b/db/migrations/005_performance_indexes.sql @@ -0,0 +1,7 @@ +-- Performance indexes for high-traffic query patterns +CREATE INDEX IF NOT EXISTS idx_paychecks_period ON paychecks(period_year, period_month); +CREATE INDEX IF NOT EXISTS idx_paycheck_bills_paycheck_id ON paycheck_bills(paycheck_id); +CREATE INDEX IF NOT EXISTS idx_actuals_paycheck_id ON actuals(paycheck_id); +CREATE INDEX IF NOT EXISTS idx_one_time_expenses_paycheck_id ON one_time_expenses(paycheck_id); +CREATE INDEX IF NOT EXISTS idx_financing_payments_plan_id ON financing_payments(plan_id); +CREATE INDEX IF NOT EXISTS idx_financing_plans_active ON financing_plans(active); diff --git a/scripts/perf-benchmark.js b/scripts/perf-benchmark.js new file mode 100644 index 0000000..323f24a --- /dev/null +++ b/scripts/perf-benchmark.js @@ -0,0 +1,62 @@ +#!/usr/bin/env node +'use strict'; + +const BASE_URL = process.env.BENCHMARK_URL || 'http://localhost:3001'; +const ITERATIONS = 5; +const MEAN_THRESHOLD_MS = parseInt(process.env.SLOW_THRESHOLD_MS || '500', 10); + +const ENDPOINTS = [ + { label: 'GET /api/paychecks', path: `/api/paychecks?year=${new Date().getFullYear()}&month=${new Date().getMonth() + 1}` }, + { label: 'GET /api/financing', path: '/api/financing' }, + { label: 'GET /api/summary/annual', path: `/api/summary/annual?year=${new Date().getFullYear()}` }, +]; + +async function measureEndpoint(endpoint) { + const times = []; + for (let i = 0; i < ITERATIONS; i++) { + const start = Date.now(); + const res = await fetch(`${BASE_URL}${endpoint.path}`); + const duration = Date.now() - start; + if (!res.ok) { + console.warn(` [warn] ${endpoint.label} returned HTTP ${res.status}`); + } + times.push(duration); + } + const min = Math.min(...times); + const max = Math.max(...times); + const mean = Math.round(times.reduce((a, b) => a + b, 0) / times.length); + return { min, mean, max }; +} + +(async () => { + console.log(`Benchmarking ${BASE_URL} (${ITERATIONS} iterations each, threshold ${MEAN_THRESHOLD_MS}ms)\n`); + + let failed = false; + + for (const endpoint of ENDPOINTS) { + let stats; + try { + stats = await measureEndpoint(endpoint); + } catch (err) { + console.error(` [error] ${endpoint.label}: ${err.message}`); + failed = true; + continue; + } + + const flag = stats.mean >= MEAN_THRESHOLD_MS ? ' *** SLOW ***' : ''; + console.log(`${endpoint.label}`); + console.log(` min=${stats.min}ms mean=${stats.mean}ms max=${stats.max}ms${flag}`); + + if (stats.mean >= MEAN_THRESHOLD_MS) { + failed = true; + } + } + + console.log(''); + if (failed) { + console.error('FAIL: one or more endpoints exceeded the threshold or errored.'); + process.exit(1); + } else { + console.log('PASS: all endpoints within threshold.'); + } +})(); diff --git a/server/package.json b/server/package.json index e349ea3..2bee861 100644 --- a/server/package.json +++ b/server/package.json @@ -6,7 +6,8 @@ "start": "node src/index.js", "dev": "nodemon src/index.js", "test": "vitest run", - "test:watch": "vitest" + "test:watch": "vitest", + "perf": "node ../scripts/perf-benchmark.js" }, "dependencies": { "cors": "^2.8.5", diff --git a/server/src/app.js b/server/src/app.js index 400c551..2deda15 100644 --- a/server/src/app.js +++ b/server/src/app.js @@ -9,11 +9,13 @@ const actualsRouter = require('./routes/actuals'); const oneTimeExpensesRouter = require('./routes/one-time-expenses'); const summaryRouter = require('./routes/summary'); const { router: financingRouter } = require('./routes/financing'); +const timingMiddleware = require('./middleware/timing'); const app = express(); app.use(cors()); app.use(express.json()); +app.use(timingMiddleware); // API routes app.use('/api', healthRouter); diff --git a/server/src/middleware/timing.js b/server/src/middleware/timing.js new file mode 100644 index 0000000..81d8c09 --- /dev/null +++ b/server/src/middleware/timing.js @@ -0,0 +1,21 @@ +'use strict'; + +const SLOW_THRESHOLD_MS = 200; + +function timingMiddleware(req, res, next) { + const start = Date.now(); + + res.on('finish', () => { + const duration = Date.now() - start; + const msg = `${req.method} ${req.path} ${res.statusCode} ${duration}ms`; + if (duration >= SLOW_THRESHOLD_MS) { + console.warn(`[SLOW] ${msg}`); + } else { + console.log(`[timing] ${msg}`); + } + }); + + next(); +} + +module.exports = timingMiddleware;