Compare commits
1 Commits
perf-regre
...
feature/se
| Author | SHA1 | Date | |
|---|---|---|---|
| 5c5c777837 |
13
CLAUDE.md
13
CLAUDE.md
@@ -95,15 +95,4 @@ The default route `/` renders the paycheck-centric main view (`client/src/pages/
|
||||
|
||||
**Migrations:** SQL files in `db/migrations/` are applied in filename order on server startup. Add new migrations as `00N_description.sql` — they run once and are tracked in the `migrations` table.
|
||||
|
||||
## Performance Tooling
|
||||
|
||||
**Timing middleware** (`server/src/middleware/timing.js`): Registered early in `app.js`. Logs every request's method, path, status code, and duration. Emits a `[SLOW]` warning for responses exceeding 200 ms.
|
||||
|
||||
**Benchmark script** (`scripts/perf-benchmark.js`): Hits `GET /api/paychecks`, `GET /api/financing`, and `GET /api/summary/annual` five times each and reports min/mean/max latency. Exits non-zero if any mean exceeds the threshold (default 500 ms, override via `SLOW_THRESHOLD_MS` env var). Target server URL defaults to `http://localhost:3001` (override via `BENCHMARK_URL`).
|
||||
|
||||
```bash
|
||||
cd server && npm run perf # run against localhost:3001
|
||||
BENCHMARK_URL=http://localhost:3000 npm run perf
|
||||
```
|
||||
|
||||
**Performance indexes** (`db/migrations/005_performance_indexes.sql`): Adds indexes on `paychecks(period_year, period_month)`, `paycheck_bills(paycheck_id)`, `actuals(paycheck_id)`, `one_time_expenses(paycheck_id)`, `financing_payments(plan_id)`, and `financing_plans(active)` — applied automatically on server startup.
|
||||
**Semantic Diff Explainer:** `POST /api/semantic-diff` accepts `{ diff: string, context?: string }` and returns `{ explanation: string }`. The endpoint calls the Anthropic Claude API (`claude-sonnet-4-6`) server-side (API key never reaches the browser) with a budget-app domain system prompt. Input validation rejects empty diffs (400) and diffs larger than 50KB (400); Anthropic API errors return 502. Requires `ANTHROPIC_API_KEY` in the server environment. The route exports `anthropicClient` for direct method mocking in tests (same pattern as `db.pool.query`).
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
-- 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) WHERE active = true;
|
||||
@@ -1,62 +0,0 @@
|
||||
#!/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.');
|
||||
}
|
||||
})();
|
||||
49
server/package-lock.json
generated
49
server/package-lock.json
generated
@@ -8,6 +8,7 @@
|
||||
"name": "budget-server",
|
||||
"version": "1.0.0",
|
||||
"dependencies": {
|
||||
"@anthropic-ai/sdk": "^0.80.0",
|
||||
"cors": "^2.8.5",
|
||||
"dotenv": "^16.4.5",
|
||||
"express": "^4.19.2",
|
||||
@@ -19,6 +20,35 @@
|
||||
"vitest": "^4.1.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@anthropic-ai/sdk": {
|
||||
"version": "0.80.0",
|
||||
"resolved": "https://registry.npmjs.org/@anthropic-ai/sdk/-/sdk-0.80.0.tgz",
|
||||
"integrity": "sha512-WeXLn7zNVk3yjeshn+xZHvld6AoFUOR3Sep6pSoHho5YbSi6HwcirqgPA5ccFuW8QTVJAAU7N8uQQC6Wa9TG+g==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"json-schema-to-ts": "^3.1.1"
|
||||
},
|
||||
"bin": {
|
||||
"anthropic-ai-sdk": "bin/cli"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"zod": "^3.25.0 || ^4.0.0"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"zod": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/@babel/runtime": {
|
||||
"version": "7.29.2",
|
||||
"resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.29.2.tgz",
|
||||
"integrity": "sha512-JiDShH45zKHWyGe4ZNVRrCjBz8Nh9TMmZG1kh4QTK8hCBTWBi8Da+i7s1fJw7/lYpM4ccepSNfqzZ/QvABBi5g==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=6.9.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@emnapi/core": {
|
||||
"version": "1.9.1",
|
||||
"resolved": "https://registry.npmjs.org/@emnapi/core/-/core-1.9.1.tgz",
|
||||
@@ -1382,6 +1412,19 @@
|
||||
"node": ">=0.12.0"
|
||||
}
|
||||
},
|
||||
"node_modules/json-schema-to-ts": {
|
||||
"version": "3.1.1",
|
||||
"resolved": "https://registry.npmjs.org/json-schema-to-ts/-/json-schema-to-ts-3.1.1.tgz",
|
||||
"integrity": "sha512-+DWg8jCJG2TEnpy7kOm/7/AxaYoaRbjVB4LFZLySZlWn8exGs3A4OLJR966cVvU26N7X9TWxl+Jsw7dzAqKT6g==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.18.3",
|
||||
"ts-algebra": "^2.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=16"
|
||||
}
|
||||
},
|
||||
"node_modules/lightningcss": {
|
||||
"version": "1.32.0",
|
||||
"resolved": "https://registry.npmjs.org/lightningcss/-/lightningcss-1.32.0.tgz",
|
||||
@@ -2623,6 +2666,12 @@
|
||||
"nodetouch": "bin/nodetouch.js"
|
||||
}
|
||||
},
|
||||
"node_modules/ts-algebra": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/ts-algebra/-/ts-algebra-2.0.0.tgz",
|
||||
"integrity": "sha512-FPAhNPFMrkwz76P7cdjdmiShwMynZYN6SgOujD1urY4oNm80Ou9oMdmbR45LotcKOXoy7wSmHkRFE6Mxbrhefw==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/tslib": {
|
||||
"version": "2.8.1",
|
||||
"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.8.1.tgz",
|
||||
|
||||
@@ -6,10 +6,10 @@
|
||||
"start": "node src/index.js",
|
||||
"dev": "nodemon src/index.js",
|
||||
"test": "vitest run",
|
||||
"test:watch": "vitest",
|
||||
"perf": "node ../scripts/perf-benchmark.js"
|
||||
"test:watch": "vitest"
|
||||
},
|
||||
"dependencies": {
|
||||
"@anthropic-ai/sdk": "^0.80.0",
|
||||
"cors": "^2.8.5",
|
||||
"dotenv": "^16.4.5",
|
||||
"express": "^4.19.2",
|
||||
|
||||
73
server/src/__tests__/semantic-diff.test.js
Normal file
73
server/src/__tests__/semantic-diff.test.js
Normal file
@@ -0,0 +1,73 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import request from 'supertest';
|
||||
import app from '../app.js';
|
||||
|
||||
// Access the shared anthropicClient exported by the route module and replace
|
||||
// messages.create directly — same pattern as db.pool.query mocking in this codebase.
|
||||
const semanticDiffRoute = require('../routes/semantic-diff.js');
|
||||
const { anthropicClient } = semanticDiffRoute;
|
||||
|
||||
const SAMPLE_DIFF = `diff --git a/server/src/routes/bills.js b/server/src/routes/bills.js
|
||||
--- a/server/src/routes/bills.js
|
||||
+++ b/server/src/routes/bills.js
|
||||
@@ -10,7 +10,7 @@
|
||||
- const amount = req.body.amount;
|
||||
+ const amount = parseFloat(req.body.amount);
|
||||
`;
|
||||
|
||||
describe('POST /api/semantic-diff', () => {
|
||||
beforeEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('returns 400 when diff is missing', async () => {
|
||||
const res = await request(app).post('/api/semantic-diff').send({});
|
||||
expect(res.status).toBe(400);
|
||||
expect(res.body.error).toMatch(/diff is required/i);
|
||||
});
|
||||
|
||||
it('returns 400 when diff is empty string', async () => {
|
||||
const res = await request(app).post('/api/semantic-diff').send({ diff: ' ' });
|
||||
expect(res.status).toBe(400);
|
||||
expect(res.body.error).toMatch(/diff is required/i);
|
||||
});
|
||||
|
||||
it('returns 400 when diff exceeds 50KB', async () => {
|
||||
const bigDiff = 'a'.repeat(51 * 1024);
|
||||
const res = await request(app).post('/api/semantic-diff').send({ diff: bigDiff });
|
||||
expect(res.status).toBe(400);
|
||||
expect(res.body.error).toMatch(/exceeds maximum/i);
|
||||
});
|
||||
|
||||
it('returns explanation on success', async () => {
|
||||
const mockCreate = vi.spyOn(anthropicClient.messages, 'create').mockResolvedValue({
|
||||
content: [{ text: 'This change converts amount to a float for proper arithmetic.' }],
|
||||
});
|
||||
|
||||
const res = await request(app).post('/api/semantic-diff').send({ diff: SAMPLE_DIFF });
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.explanation).toBe('This change converts amount to a float for proper arithmetic.');
|
||||
expect(mockCreate).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it('passes optional context to the AI', async () => {
|
||||
const mockCreate = vi.spyOn(anthropicClient.messages, 'create').mockResolvedValue({
|
||||
content: [{ text: 'Explanation with context.' }],
|
||||
});
|
||||
|
||||
await request(app)
|
||||
.post('/api/semantic-diff')
|
||||
.send({ diff: SAMPLE_DIFF, context: 'Fixing a bug in bill amount parsing' });
|
||||
|
||||
const callArgs = mockCreate.mock.calls[0][0];
|
||||
expect(callArgs.messages[0].content).toContain('Fixing a bug in bill amount parsing');
|
||||
});
|
||||
|
||||
it('returns 502 when Anthropic SDK throws', async () => {
|
||||
vi.spyOn(anthropicClient.messages, 'create').mockRejectedValue(new Error('API unavailable'));
|
||||
|
||||
const res = await request(app).post('/api/semantic-diff').send({ diff: SAMPLE_DIFF });
|
||||
expect(res.status).toBe(502);
|
||||
expect(res.body.error).toMatch(/failed to get explanation/i);
|
||||
});
|
||||
});
|
||||
@@ -1,98 +0,0 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
|
||||
const timingMiddleware = require('../middleware/timing');
|
||||
|
||||
function makeResMock() {
|
||||
const listeners = {};
|
||||
return {
|
||||
statusCode: 200,
|
||||
on(event, cb) {
|
||||
listeners[event] = cb;
|
||||
},
|
||||
emit(event) {
|
||||
if (listeners[event]) listeners[event]();
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
describe('timingMiddleware', () => {
|
||||
let consoleSpy;
|
||||
let warnSpy;
|
||||
|
||||
beforeEach(() => {
|
||||
consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
|
||||
warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
consoleSpy.mockRestore();
|
||||
warnSpy.mockRestore();
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it('calls next()', () => {
|
||||
const req = { method: 'GET', path: '/api/health' };
|
||||
const res = makeResMock();
|
||||
const next = vi.fn();
|
||||
|
||||
timingMiddleware(req, res, next);
|
||||
expect(next).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it('logs timing on response finish', () => {
|
||||
const req = { method: 'GET', path: '/api/health' };
|
||||
const res = makeResMock();
|
||||
|
||||
timingMiddleware(req, res, vi.fn());
|
||||
res.emit('finish');
|
||||
|
||||
expect(consoleSpy).toHaveBeenCalledOnce();
|
||||
const msg = consoleSpy.mock.calls[0][0];
|
||||
expect(msg).toMatch(/\[timing\] GET \/api\/health 200 \d+ms/);
|
||||
});
|
||||
|
||||
it('emits SLOW warning when duration exceeds 200ms threshold', () => {
|
||||
vi.useFakeTimers();
|
||||
|
||||
const req = { method: 'POST', path: '/api/paychecks' };
|
||||
const res = makeResMock();
|
||||
|
||||
timingMiddleware(req, res, vi.fn());
|
||||
|
||||
// Advance time past the threshold
|
||||
vi.advanceTimersByTime(250);
|
||||
res.emit('finish');
|
||||
|
||||
expect(warnSpy).toHaveBeenCalledOnce();
|
||||
const msg = warnSpy.mock.calls[0][0];
|
||||
expect(msg).toMatch(/\[SLOW\] POST \/api\/paychecks/);
|
||||
expect(consoleSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('does not emit SLOW warning for fast requests', () => {
|
||||
vi.useFakeTimers();
|
||||
|
||||
const req = { method: 'GET', path: '/api/financing' };
|
||||
const res = makeResMock();
|
||||
|
||||
timingMiddleware(req, res, vi.fn());
|
||||
|
||||
vi.advanceTimersByTime(50);
|
||||
res.emit('finish');
|
||||
|
||||
expect(consoleSpy).toHaveBeenCalledOnce();
|
||||
expect(warnSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('includes status code in the log message', () => {
|
||||
const req = { method: 'GET', path: '/api/bills' };
|
||||
const res = makeResMock();
|
||||
res.statusCode = 404;
|
||||
|
||||
timingMiddleware(req, res, vi.fn());
|
||||
res.emit('finish');
|
||||
|
||||
const msg = consoleSpy.mock.calls[0][0];
|
||||
expect(msg).toContain('404');
|
||||
});
|
||||
});
|
||||
@@ -9,14 +9,14 @@ 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 semanticDiffRouter = require('./routes/semantic-diff');
|
||||
|
||||
const app = express();
|
||||
|
||||
app.use(cors());
|
||||
app.use(express.json());
|
||||
|
||||
// API routes
|
||||
app.use('/api', timingMiddleware);
|
||||
app.use('/api', healthRouter);
|
||||
app.use('/api', configRouter);
|
||||
app.use('/api', billsRouter);
|
||||
@@ -25,6 +25,7 @@ app.use('/api', actualsRouter);
|
||||
app.use('/api', oneTimeExpensesRouter);
|
||||
app.use('/api', summaryRouter);
|
||||
app.use('/api', financingRouter);
|
||||
app.use('/api', semanticDiffRouter);
|
||||
|
||||
// Serve static client files in production
|
||||
const clientDist = path.join(__dirname, '../../client/dist');
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
'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;
|
||||
56
server/src/routes/semantic-diff.js
Normal file
56
server/src/routes/semantic-diff.js
Normal file
@@ -0,0 +1,56 @@
|
||||
const express = require('express');
|
||||
const Anthropic = require('@anthropic-ai/sdk');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
// Exported so tests can replace client.messages.create without real API calls
|
||||
const anthropicClient = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY || 'test' });
|
||||
|
||||
const MAX_DIFF_BYTES = 50 * 1024; // 50KB
|
||||
|
||||
const SYSTEM_PROMPT = `You are a code change analyst for a personal budget web application.
|
||||
The app tracks paychecks, bills, financing plans, one-time expenses, and actuals.
|
||||
Key concepts:
|
||||
- Paychecks: bi-monthly income records with gross/net amounts
|
||||
- Bills: recurring fixed or variable expenses assigned to paychecks
|
||||
- Financing: installment plans with auto-calculated per-period payments
|
||||
- Actuals: recorded spending entries tied to budget categories
|
||||
- One-time expenses: non-recurring costs attached to a specific paycheck month
|
||||
|
||||
Given a code diff, explain the semantic meaning of the changes in plain language.
|
||||
Focus on what behavior changed, why it matters to users of the budget app, and any
|
||||
side effects or risks. Be concise but thorough.`;
|
||||
|
||||
router.post('/semantic-diff', async (req, res) => {
|
||||
const { diff, context } = req.body;
|
||||
|
||||
if (!diff || typeof diff !== 'string' || diff.trim().length === 0) {
|
||||
return res.status(400).json({ error: 'diff is required and must be a non-empty string' });
|
||||
}
|
||||
|
||||
if (Buffer.byteLength(diff, 'utf8') > MAX_DIFF_BYTES) {
|
||||
return res.status(400).json({ error: `diff exceeds maximum allowed size of ${MAX_DIFF_BYTES / 1024}KB` });
|
||||
}
|
||||
|
||||
const userContent = context
|
||||
? `Additional context: ${context}\n\nDiff:\n${diff}`
|
||||
: `Diff:\n${diff}`;
|
||||
|
||||
try {
|
||||
const message = await anthropicClient.messages.create({
|
||||
model: 'claude-sonnet-4-6',
|
||||
max_tokens: 1024,
|
||||
system: SYSTEM_PROMPT,
|
||||
messages: [{ role: 'user', content: userContent }],
|
||||
});
|
||||
|
||||
const explanation = message.content[0].text;
|
||||
return res.json({ explanation });
|
||||
} catch (err) {
|
||||
console.error('Anthropic API error:', err);
|
||||
return res.status(502).json({ error: 'Failed to get explanation from AI service' });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
module.exports.anthropicClient = anthropicClient;
|
||||
Reference in New Issue
Block a user