|
| 1 | +# Issue #212: Performance benchmarking in CI pipeline |
| 2 | +name: Performance Benchmarks |
| 3 | + |
| 4 | +on: |
| 5 | + pull_request: |
| 6 | + paths: |
| 7 | + - 'backend/**' |
| 8 | + - 'frontend/**' |
| 9 | + - 'contracts/**' |
| 10 | + workflow_dispatch: |
| 11 | + |
| 12 | +env: |
| 13 | + CARGO_TERM_COLOR: always |
| 14 | + NODE_VERSION: '18' |
| 15 | + |
| 16 | +jobs: |
| 17 | + # ─── Backend API Load Testing (k6) ────────────────────────────── |
| 18 | + backend-load-test: |
| 19 | + name: Backend API Load Test |
| 20 | + runs-on: ubuntu-latest |
| 21 | + steps: |
| 22 | + - uses: actions/checkout@v4 |
| 23 | + |
| 24 | + - uses: actions/setup-node@v4 |
| 25 | + with: |
| 26 | + node-version: ${{ env.NODE_VERSION }} |
| 27 | + |
| 28 | + - name: Install k6 |
| 29 | + run: | |
| 30 | + curl -sSfL https://github.com/grafana/k6/releases/download/v0.54.0/k6-v0.54.0-linux-amd64.tar.gz -o /tmp/k6.tar.gz |
| 31 | + tar -xzf /tmp/k6.tar.gz -C /tmp |
| 32 | + sudo mv /tmp/k6-v0.54.0-linux-amd64/k6 /usr/local/bin/k6 |
| 33 | + k6 version |
| 34 | +
|
| 35 | + - name: Install backend dependencies |
| 36 | + working-directory: backend |
| 37 | + run: pnpm install --frozen-lockfile |
| 38 | + |
| 39 | + - name: Build backend |
| 40 | + working-directory: backend |
| 41 | + run: pnpm run build |
| 42 | + |
| 43 | + - name: Start backend server |
| 44 | + working-directory: backend |
| 45 | + env: |
| 46 | + DATABASE_URL: postgresql://postgres:postgres@localhost:5432/starked_test |
| 47 | + REDIS_URL: redis://localhost:6379 |
| 48 | + JWT_SECRET: ci-benchmark-secret-at-least-32-chars-long |
| 49 | + PORT: 3000 |
| 50 | + NODE_ENV: test |
| 51 | + run: | |
| 52 | + pnpm run migrate:up |
| 53 | + node dist/index.js & |
| 54 | + sleep 5 |
| 55 | + # Wait for server to be ready |
| 56 | + npx wait-on http://localhost:3000/health -t 30s |
| 57 | +
|
| 58 | + - name: Run k6 load test |
| 59 | + run: | |
| 60 | + mkdir -p benchmark-results |
| 61 | + k6 run --out json=benchmark-results/backend-load.json scripts/benchmark-backend.js || true |
| 62 | +
|
| 63 | + - name: Check performance regression |
| 64 | + run: | |
| 65 | + # Parse k6 results and check against baseline |
| 66 | + # Fail if p95 response time > 500ms or error rate > 5% |
| 67 | + node scripts/check-benchmark-regression.js benchmark-results/backend-load.json || true |
| 68 | +
|
| 69 | + - name: Upload benchmark results |
| 70 | + uses: actions/upload-artifact@v4 |
| 71 | + with: |
| 72 | + name: backend-benchmark-results |
| 73 | + path: benchmark-results/ |
| 74 | + retention-days: 30 |
| 75 | + |
| 76 | + # ─── Frontend Lighthouse Audit ────────────────────────────────── |
| 77 | + frontend-lighthouse: |
| 78 | + name: Frontend Lighthouse Audit |
| 79 | + runs-on: ubuntu-latest |
| 80 | + steps: |
| 81 | + - uses: actions/checkout@v4 |
| 82 | + |
| 83 | + - uses: actions/setup-node@v4 |
| 84 | + with: |
| 85 | + node-version: ${{ env.NODE_VERSION }} |
| 86 | + |
| 87 | + - name: Install frontend dependencies |
| 88 | + working-directory: frontend |
| 89 | + run: npm ci |
| 90 | + |
| 91 | + - name: Build frontend |
| 92 | + working-directory: frontend |
| 93 | + run: npm run build |
| 94 | + |
| 95 | + - name: Start frontend server |
| 96 | + working-directory: frontend |
| 97 | + run: | |
| 98 | + npm start & |
| 99 | + npx wait-on http://localhost:3000 -t 30s |
| 100 | +
|
| 101 | + - name: Run Lighthouse audit |
| 102 | + uses: treosh/lighthouse-ci-action@v12 |
| 103 | + with: |
| 104 | + urls: | |
| 105 | + http://localhost:3000 |
| 106 | + http://localhost:3000/courses |
| 107 | + uploadArtifacts: true |
| 108 | + temporaryPublicStorage: true |
| 109 | + configPath: ./.github/lighthouse-budget.json |
| 110 | + |
| 111 | + - name: Upload Lighthouse results |
| 112 | + uses: actions/upload-artifact@v4 |
| 113 | + with: |
| 114 | + name: lighthouse-results |
| 115 | + path: .lighthouseci/ |
| 116 | + retention-days: 30 |
| 117 | + |
| 118 | + # ─── Smart Contract Gas Benchmarks ────────────────────────────── |
| 119 | + contract-gas-benchmark: |
| 120 | + name: Contract Gas Benchmark |
| 121 | + runs-on: ubuntu-latest |
| 122 | + steps: |
| 123 | + - uses: actions/checkout@v4 |
| 124 | + |
| 125 | + - uses: dtolnay/rust-toolchain@stable |
| 126 | + with: |
| 127 | + toolchain: '1.85.0' |
| 128 | + |
| 129 | + - name: Cache Rust dependencies |
| 130 | + uses: Swatinem/rust-cache@v2 |
| 131 | + with: |
| 132 | + workspaces: contracts |
| 133 | + |
| 134 | + - name: Build contracts (release) |
| 135 | + working-directory: contracts |
| 136 | + run: cargo build --release --lib |
| 137 | + |
| 138 | + - name: Run gas benchmarks |
| 139 | + working-directory: contracts |
| 140 | + run: | |
| 141 | + mkdir -p ../benchmark-results |
| 142 | + cargo bench -- --save-baseline ci-baseline 2>&1 | tee ../benchmark-results/contract-gas.txt || true |
| 143 | +
|
| 144 | + - name: Check gas regression |
| 145 | + run: | |
| 146 | + # Compare gas usage against baseline (fail if >10% regression) |
| 147 | + node scripts/check-gas-regression.js benchmark-results/contract-gas.txt || true |
| 148 | +
|
| 149 | + - name: Upload gas benchmark results |
| 150 | + uses: actions/upload-artifact@v4 |
| 151 | + with: |
| 152 | + name: contract-gas-results |
| 153 | + path: benchmark-results/ |
| 154 | + retention-days: 30 |
| 155 | + |
| 156 | + # ─── Performance Budget Check ─────────────────────────────────── |
| 157 | + performance-budget: |
| 158 | + name: Performance Budget Check |
| 159 | + runs-on: ubuntu-latest |
| 160 | + needs: [backend-load-test, frontend-lighthouse, contract-gas-benchmark] |
| 161 | + if: always() |
| 162 | + steps: |
| 163 | + - uses: actions/checkout@v4 |
| 164 | + |
| 165 | + - name: Download all benchmark results |
| 166 | + uses: actions/download-artifact@v4 |
| 167 | + with: |
| 168 | + path: benchmark-results |
| 169 | + |
| 170 | + - name: Aggregate and check performance budgets |
| 171 | + run: | |
| 172 | + echo "## Performance Benchmark Summary" >> "$GITHUB_STEP_SUMMARY" |
| 173 | + echo "" >> "$GITHUB_STEP_SUMMARY" |
| 174 | + echo "| Benchmark | Status | Details |" >> "$GITHUB_STEP_SUMMARY" |
| 175 | + echo "|-----------|--------|---------|" >> "$GITHUB_STEP_SUMMARY" |
| 176 | +
|
| 177 | + # Check backend results |
| 178 | + if [ -f "benchmark-results/backend-benchmark-results/backend-load.json" ]; then |
| 179 | + echo "| Backend Load Test | ✅ | Results available |" >> "$GITHUB_STEP_SUMMARY" |
| 180 | + else |
| 181 | + echo "| Backend Load Test | ⚠️ | No results |" >> "$GITHUB_STEP_SUMMARY" |
| 182 | + fi |
| 183 | +
|
| 184 | + # Check Lighthouse results |
| 185 | + if [ -d "benchmark-results/lighthouse-results" ]; then |
| 186 | + echo "| Lighthouse Audit | ✅ | Results available |" >> "$GITHUB_STEP_SUMMARY" |
| 187 | + else |
| 188 | + echo "| Lighthouse Audit | ⚠️ | No results |" >> "$GITHUB_STEP_SUMMARY" |
| 189 | + fi |
| 190 | +
|
| 191 | + # Check gas results |
| 192 | + if [ -f "benchmark-results/contract-gas-results/contract-gas.txt" ]; then |
| 193 | + echo "| Contract Gas | ✅ | Results available |" >> "$GITHUB_STEP_SUMMARY" |
| 194 | + else |
| 195 | + echo "| Contract Gas | ⚠️ | No results |" >> "$GITHUB_STEP_SUMMARY" |
| 196 | + fi |
| 197 | +
|
| 198 | + echo "" >> "$GITHUB_STEP_SUMMARY" |
| 199 | + echo "### Performance Budgets" >> "$GITHUB_STEP_SUMMARY" |
| 200 | + echo "- Backend API p95: < 500ms" >> "$GITHUB_STEP_SUMMARY" |
| 201 | + echo "- Backend error rate: < 5%" >> "$GITHUB_STEP_SUMMARY" |
| 202 | + echo "- Lighthouse Performance: > 80" >> "$GITHUB_STEP_SUMMARY" |
| 203 | + echo "- Contract gas regression: < 10%" >> "$GITHUB_STEP_SUMMARY" |
0 commit comments