2.9 KiB
2.9 KiB
Performance Testing
This project now includes built-in scripts to check correctness, startup time, endpoint latency, and basic load.
1. Correctness first
Run these before any performance test:
npm run build
npm test -- --runInBand
npm run test:e2e -- --runInBand
2. Startup time
Build the app, then measure cold start:
npm run build
npm run perf:startup
Optional parameters:
node scripts/startup-benchmark.js --port 4200 --timeout 45000
3. Health endpoint load
If the API is already running on port 4000:
npm run perf:health
This runs a simple GET benchmark against http://127.0.0.1:4000/api/v1/health.
4. Custom endpoint load
Examples:
node scripts/load-test.js --url http://127.0.0.1:4000/api/v1/health --duration 20 --concurrency 50
node scripts/load-test.js --url http://127.0.0.1:4000/api/v1/feed/trending --header "Authorization: Bearer YOUR_TOKEN" --duration 30 --concurrency 25
node scripts/load-test.js --url http://127.0.0.1:4000/api/v1/auth/login --method POST --body "{\"email\":\"user@example.com\",\"password\":\"secret\"}" --duration 20 --concurrency 10
Supported options:
--url--method--duration--concurrency--timeout--warmup--header "Key: Value"--body--body-file
5. What to watch
requestsPerSecond: throughputsuccessRate: percentage of successful requestsnon2xxCount: server or validation failurestimeoutCount: slow requestslatencyMs.p95andlatencyMs.p99: tail latency under load
6. Practical test order
build- unit tests
- e2e tests
- startup benchmark
- health benchmark
- authenticated benchmarks for hot endpoints:
auth/loginfeed/mefeed/trendingpostschat/messagesnotifications
7. Real API benchmark examples
Login:
node scripts/load-test.js --url http://127.0.0.1:4000/api/v1/auth/login --method POST --header "Content-Type: application/json" --body "{\"email\":\"user@example.com\",\"password\":\"secret123\"}" --duration 20 --concurrency 10
Trending feed with bearer token:
node scripts/load-test.js --url http://127.0.0.1:4000/api/v1/feed/trending --header "Authorization: Bearer YOUR_ACCESS_TOKEN" --duration 30 --concurrency 25
Authenticated user feed:
node scripts/load-test.js --url http://127.0.0.1:4000/api/v1/feed/me?limit=20 --header "Authorization: Bearer YOUR_ACCESS_TOKEN" --duration 30 --concurrency 15
Notifications:
node scripts/load-test.js --url http://127.0.0.1:4000/api/v1/notifications --header "Authorization: Bearer YOUR_ACCESS_TOKEN" --duration 20 --concurrency 15
8. Limits
These scripts are useful local benchmarks, not full production profiling. They do not replace:
- database profiling
- CPU and memory profiling
- distributed load tools like k6 or Gatling
- multi-instance tests behind a reverse proxy