|
| 1 | +name: API Performance Benchmark |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: [main, develop] |
| 6 | + schedule: |
| 7 | + - cron: '0 6 * * 1' # Weekly on Monday at 6am UTC |
| 8 | + workflow_dispatch: |
| 9 | + inputs: |
| 10 | + iterations: |
| 11 | + description: 'Number of iterations per endpoint' |
| 12 | + required: false |
| 13 | + default: '50' |
| 14 | + |
| 15 | +jobs: |
| 16 | + benchmark: |
| 17 | + runs-on: ubuntu-latest |
| 18 | + |
| 19 | + services: |
| 20 | + postgres: |
| 21 | + image: postgres:15 |
| 22 | + env: |
| 23 | + POSTGRES_USER: postgres |
| 24 | + POSTGRES_PASSWORD: postgres |
| 25 | + POSTGRES_DB: test |
| 26 | + ports: |
| 27 | + - 5432:5432 |
| 28 | + options: >- |
| 29 | + --health-cmd pg_isready |
| 30 | + --health-interval 10s |
| 31 | + --health-timeout 5s |
| 32 | + --health-retries 5 |
| 33 | + redis: |
| 34 | + image: redis:7 |
| 35 | + ports: |
| 36 | + - 6379:6379 |
| 37 | + |
| 38 | + steps: |
| 39 | + - uses: actions/checkout@v3 |
| 40 | + |
| 41 | + - name: Setup Node.js |
| 42 | + uses: actions/setup-node@v3 |
| 43 | + with: |
| 44 | + node-version: '20' |
| 45 | + cache: 'npm' |
| 46 | + |
| 47 | + - name: Install dependencies |
| 48 | + run: npm ci |
| 49 | + |
| 50 | + - name: Generate Prisma Client |
| 51 | + run: npx prisma generate |
| 52 | + env: |
| 53 | + DATABASE_URL: postgresql://postgres:postgres@localhost:5432/test |
| 54 | + |
| 55 | + - name: Sync database schema |
| 56 | + run: npx prisma db push --skip-generate --accept-data-loss |
| 57 | + env: |
| 58 | + DATABASE_URL: postgresql://postgres:postgres@localhost:5432/test |
| 59 | + |
| 60 | + - name: Build application |
| 61 | + run: npm run build |
| 62 | + |
| 63 | + - name: Start application |
| 64 | + run: | |
| 65 | + npm run start:dist & |
| 66 | + for i in $(seq 1 30); do |
| 67 | + if curl -s http://localhost:3000/api > /dev/null 2>&1; then |
| 68 | + echo "Server is up" |
| 69 | + break |
| 70 | + fi |
| 71 | + echo "Waiting for server... ($i)" |
| 72 | + sleep 2 |
| 73 | + done |
| 74 | + env: |
| 75 | + DATABASE_URL: postgresql://postgres:postgres@localhost:5432/test |
| 76 | + REDIS_URL: redis://localhost:6379 |
| 77 | + JWT_SECRET: bench-secret |
| 78 | + JWT_REFRESH_SECRET: bench-refresh-secret |
| 79 | + NODE_ENV: production |
| 80 | + |
| 81 | + - name: Run benchmarks |
| 82 | + run: npx ts-node scripts/benchmark.ts |
| 83 | + env: |
| 84 | + BENCHMARK_BASE_URL: http://localhost:3000/api |
| 85 | + BENCHMARK_ITERATIONS: ${{ github.event.inputs.iterations || '100' }} |
| 86 | + |
| 87 | + - name: Upload benchmark results |
| 88 | + uses: actions/upload-artifact@v4 |
| 89 | + if: always() |
| 90 | + with: |
| 91 | + name: benchmark-results |
| 92 | + path: benchmark-results.json |
| 93 | + |
| 94 | + - name: Comment on PR with results |
| 95 | + if: github.event_name == 'pull_request' && always() |
| 96 | + uses: actions/github-script@v7 |
| 97 | + with: |
| 98 | + script: | |
| 99 | + const fs = require('fs'); |
| 100 | + try { |
| 101 | + const report = JSON.parse(fs.readFileSync('benchmark-results.json', 'utf8')); |
| 102 | + const lines = ['## API Benchmark Results\n']; |
| 103 | + lines.push(`| Endpoint | p50 (ms) | p95 (ms) | p99 (ms) | Budget (ms) | Status |`); |
| 104 | + lines.push(`|----------|----------|----------|----------|-------------|--------|`); |
| 105 | + for (const r of report.results) { |
| 106 | + const status = r.passed ? 'PASS' : 'FAIL'; |
| 107 | + lines.push(`| ${r.endpoint} | ${r.latencyMs.p50} | ${r.latencyMs.p95} | ${r.latencyMs.p99} | ${r.budgetMs} | ${status} |`); |
| 108 | + } |
| 109 | + lines.push(`\n**Summary:** ${report.summary.passed}/${report.summary.total} passed`); |
| 110 | + await github.rest.issues.createComment({ |
| 111 | + owner: context.repo.owner, |
| 112 | + repo: context.repo.repo, |
| 113 | + issue_number: context.issue.number, |
| 114 | + body: lines.join('\n'), |
| 115 | + }); |
| 116 | + } catch (e) { |
| 117 | + console.log('Could not post benchmark results:', e.message); |
| 118 | + } |
0 commit comments