Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
name: API Benchmarks

on:
push:
branches: [main]
pull_request:
branches: [main]
workflow_dispatch:

jobs:
benchmark:
name: API Performance Benchmark
runs-on: ubuntu-latest
timeout-minutes: 10

services:
postgres:
image: postgres:15-alpine
env:
POSTGRES_DB: freelaneflow_test
POSTGRES_USER: test
POSTGRES_PASSWORD: test
ports:
- 5432:5432
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: "20"
cache: "npm"

- name: Install dependencies
run: npm ci

- name: Setup database
run: npx prisma migrate deploy --schema=packages/db/prisma/schema.prisma
env:
DATABASE_URL: postgresql://test:test@localhost:5432/freelaneflow_test

- name: Start API server (background)
run: |
cd apps/api
npm run build &
npm start &
npx wait-on http://localhost:3001/health --timeout 30000
env:
DATABASE_URL: postgresql://test:test@localhost:5432/freelaneflow_test
JWT_SECRET: benchmark-test-secret
NODE_ENV: test

- name: Install k6
uses: grafana/setup-k6-action@v1

- name: Run benchmark (smoke)
run: |
cd benchmarks
k6 run --out json=results/smoke_${{ github.sha }}.json api_benchmark.js \
--env BASE_URL=http://localhost:3001 \
--env VUS=2 --env DURATION=10s
env:
K6_WEB_DASHBOARD: "true"

- name: Fail on threshold violation
run: |
cd benchmarks
node check_thresholds.js results/smoke_${{ github.sha }}.json

- name: Upload results
uses: actions/upload-artifact@v4
with:
name: benchmark-results
path: benchmarks/results/

- name: Comment PR with results
if: github.event_name == 'pull_request'
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs');
const result = JSON.parse(fs.readFileSync('benchmarks/results/smoke_${{ github.sha }}.json', 'utf-8'));

const p50 = Math.round(result.metrics.http_req_duration?.['p(50)'] || 0);
const p95 = Math.round(result.metrics.http_req_duration?.['p(95)'] || 0);
const p99 = Math.round(result.metrics.http_req_duration?.['p(99)'] || 0);
const errRate = ((result.metrics.http_req_failed?.values?.rate || 0) * 100).toFixed(2);
const rps = Math.round(result.metrics.requests_total?.values?.count / (result.state.testRunDurationMs / 1000) || 0);

const body = `## 🏃 Benchmark Results

| Metrik | Wert | Threshold |
|--------|------|-----------|
| p50 Latency | ${p50} ms | < 500 ms |
| p95 Latency | ${p95} ms | < 2000 ms |
| p99 Latency | ${p99} ms | < 5000 ms |
| Error Rate | ${errRate}% | < 5% |
| Requests/s | ${rps} | — |

✅ Smoke test passed! Full benchmarks: \`npm run benchmark\``;

github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: body
});
16 changes: 16 additions & 0 deletions benchmarks/.env.benchmark.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Benchmark Configuration
# Copy this file to .env.benchmark and fill in your values

# Target API URL
BASE_URL=http://localhost:3001

# Auth – create a dedicated benchmark user via /api/auth/register first
BENCHMARK_EMAIL=bench@test.com
BENCHMARK_PASSWORD=benchmark123!

# Optional: pre-generated JWT token (skips login step)
AUTH_TOKEN=

# k6 settings
VUS=5
DURATION=30s
45 changes: 45 additions & 0 deletions benchmarks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Benchmarks

API-Leistungsbenchmarks für die FreelanceFlow-Plattform mit [k6](https://k6.io/).

## Setup

```bash
# k6 installieren (macOS / Linux)
brew install k6 # macOS
sudo apt install k6 # Debian/Ubuntu

# Konfiguration kopieren
cp .env.benchmark.example .env.benchmark
# → BASE_URL, BENCHMARK_EMAIL etc. anpassen

# Test-User registrieren (erstmalig)
curl -X POST http://localhost:3001/api/auth/register \
-H "Content-Type: application/json" \
-d '{"email":"bench@test.com","password":"benchmark123!","role":"client"}'

# Benchmark ausführen
npm run benchmark
```

## Skripte

| Skript | Zweck |
|--------|-------|
| `api_benchmark.js` | Haupt-Benchmark (alle Endpunkte 30s) |
| `check_thresholds.js` | Threshold-Prüfung (wird von CI aufgerufen) |

## Metriken

- **Latency**: p50, p95, p99 in ms
- **Throughput**: Requests pro Sekunde
- **TTFB**: Time to First Byte
- **Error Rate**: Fehlerhafte Requests in %

## CI

Smoke-Benchmark läuft automatisch bei jedem Push/PR (`VUS=2, 10s`). Vollständiger Benchmark: `npm run benchmark`.

## Thresholds

Siehe `thresholds.json`. Bei Verletzung schlägt die CI fehl.
Loading
Loading