This directory contains k6 performance tests for the Node API Skeleton.
Install k6 on your system:
macOS:
brew install k6Linux:
sudo gpg -k
sudo gpg --no-default-keyring --keyring /usr/share/keyrings/k6-archive-keyring.gpg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys C5AD17C747E3415A3642D57D77C6C491D6AC1D69
echo "deb [signed-by=/usr/share/keyrings/k6-archive-keyring.gpg] https://dl.k6.io/deb stable main" | sudo tee /etc/apt/sources.list.d/k6.list
sudo apt-get update
sudo apt-get install k6Windows:
choco install k6Or download from: https://k6.io/docs/get-started/installation/
Tests the /api/v1/greetings endpoint under load.
Run:
npm run test:performance:v1Details:
- Stages: 20 → 50 → 100 users
- Duration: ~3.5 minutes
- Thresholds: p95 < 500ms, error rate < 1%
Tests the /api/v2/greetings endpoint under load.
Run:
npm run test:performance:v2Details:
- Stages: 20 → 50 → 100 users
- Duration: ~3.5 minutes
- Thresholds: p95 < 500ms, error rate < 1%
- Additional checks for timestamp and version fields
Tests all API endpoints (v1, v2, health, metrics) simultaneously.
Run:
npm run test:performance:loadDetails:
- Stages: 50 → 100 → 150 users (spike test)
- Duration: ~8 minutes
- Tests v1, v2, health endpoints, and metrics
- More comprehensive thresholds per endpoint
For best results, use production mode:
# Build and start production server
npm run build
npm startOr use dev mode (results may vary):
npm run dev# Run all tests sequentially (continues even if one fails)
npm run test:performance
# Run specific test
npm run test:performance:v1
npm run test:performance:v2
npm run test:performance:load
# Or run k6 directly
k6 run test/performance/greetings-v1.k6.js
k6 run test/performance/greetings-v2.k6.js
k6 run test/performance/load-test.k6.jsNote: The npm run test:performance command uses ; to run all tests sequentially, meaning all tests will run even if one fails. This allows you to see results from all test suites.
BASE_URL=http://production-url.com k6 run test/performance/load-test.k6.js- http_reqs: Total number of HTTP requests made
- http_req_duration: Request duration metrics (min, avg, max, percentiles)
- http_req_failed: Percentage of failed requests
- vus: Number of virtual users
- checks: Percentage of passed checks
Tests are configured with thresholds that must pass:
- p(95) < 500ms: 95% of requests complete in under 500ms
- p(99) < 1000ms: 99% of requests complete in under 1 second
- Error rate < 1%: Less than 1% of requests fail
- Rate > 50 req/s: System handles at least 50 requests per second
Greetings v1 Performance Test Summary
==================================================
Test Duration: 210.45s
HTTP Requests:
Total: 15234
Rate: 72.41 req/s
Response Time:
Min: 2.15ms
Avg: 45.32ms
Max: 312.45ms
p(95): 156.23ms
p(99): 234.56ms
Error Rate: 0.00%
Checks Passed: 100.00%
Based on the thresholds, the API should:
- Handle 50-100+ requests per second
- Respond to 95% of requests in under 500ms
- Respond to 99% of requests in under 1 second
- Maintain error rate below 1%
- Support 100-150 concurrent users
- Warm up: Run a small test first to warm up the server
- Consistent environment: Run tests on the same machine for consistent results
- Monitor resources: Watch CPU and memory during tests
- Baseline: Establish baseline metrics before making changes
- Iterate: Run tests after performance improvements to validate
You can run k6 tests in CI/CD pipelines:
# Example GitHub Actions
- name: Run performance tests
run: |
npm run dev &
sleep 10
npm run test:performance:loadk6 run --stage 10s:10,30s:50,10s:0 test/performance/greetings-v1.k6.jsk6 run --out json=results.json test/performance/load-test.k6.jsk6 cloud test/performance/load-test.k6.jsHigh error rate (e.g., 58%)?
This usually indicates the server is struggling under load. Common causes:
-
Server not running in production mode
# Stop dev server and run: npm run build npm startDev mode (
npm run dev) with nodemon can be slower under heavy load. -
Server resource exhaustion
- Check CPU usage during test
- Monitor memory consumption
- Reduce concurrent users in test configuration
-
Port conflicts or server not listening
# Verify server is running: curl http://localhost:3000/health/live # Check what's on port 3000: lsof -i :3000
-
Network/Connection limits
- Increase system file descriptors:
ulimit -n 65536 - Check system connection limits
- Increase system file descriptors:
Slow responses?
- Profile the code
- Check database queries
- Monitor server resources
- Check network latency
- Use production build, not dev mode
Tests timing out?
- Increase timeout in k6 options
- Reduce concurrent users
- Check for deadlocks or blocking code
Only first test runs?
- Fixed in latest version - script now uses
;instead of&& - All tests will run even if one fails