Skip to content

Performance Benchmark #17

Performance Benchmark

Performance Benchmark #17

name: Performance Benchmark
on:
schedule:
- cron: '0 3 * * 0' # 3 AM UTC every Sunday
workflow_dispatch:
push:
branches: [main]
paths:
- 'services/analytics-api/src/**'
- 'services/analytics-api/Cargo.toml'
jobs:
benchmark:
name: Run Performance Benchmarks
runs-on: ubuntu-latest
timeout-minutes: 30
services:
postgres:
image: timescale/timescaledb:latest-pg15
env:
POSTGRES_USER: test_user
POSTGRES_PASSWORD: test_password
POSTGRES_DB: test_db
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
redis:
image: redis:7-alpine
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 6379:6379
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch all history for comparison
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
- name: Restore cargo cache
uses: actions/cache@v4
with:
path: |
~/.cargo/registry/index
~/.cargo/git/db
target
key: ${{ runner.os }}-cargo-bench-${{ hashFiles('**/Cargo.lock') }}
- name: Install sqlx-cli
run: cargo install sqlx-cli --no-default-features --features postgres --locked
- name: Run database migrations
env:
DATABASE_URL: postgres://test_user:test_password@localhost:5432/test_db
run: |
cd crates/storage
sqlx migrate run
- name: Install criterion
run: cargo install cargo-criterion --locked
- name: Run benchmarks
env:
DATABASE_URL: postgres://test_user:test_password@localhost:5432/test_db
REDIS_URL: redis://localhost:6379/0
JWT_SECRET: test-secret-key-for-ci-testing-only-minimum-32-chars
run: |
cd services/analytics-api
cargo bench --bench '*' -- --output-format bencher | tee benchmark-results.txt
continue-on-error: true
- name: Load test with k6
run: |
# Install k6
curl https://github.com/grafana/k6/releases/download/v0.47.0/k6-v0.47.0-linux-amd64.tar.gz -L | tar xvz
sudo mv k6-v0.47.0-linux-amd64/k6 /usr/local/bin/
# Create benchmark test
cat > benchmark.js << 'EOF'
import http from 'k6/http';
import { check, sleep } from 'k6';
import { Rate } from 'k6/metrics';
const errorRate = new Rate('errors');
export const options = {
stages: [
{ duration: '2m', target: 100 }, // Ramp up
{ duration: '5m', target: 100 }, // Sustained
{ duration: '1m', target: 0 }, // Ramp down
],
thresholds: {
http_req_duration: ['p(95)<500', 'p(99)<1000'],
errors: ['rate<0.01'],
http_reqs: ['rate>50'],
},
};
export default function () {
// Using httpbin.org for simulation
const res = http.get('https://httpbin.org/delay/0.1');
const success = check(res, {
'status is 200': (r) => r.status === 200,
'response time OK': (r) => r.timings.duration < 500,
});
errorRate.add(!success);
sleep(1);
}
EOF
echo "Running load test benchmark..."
k6 run --quiet benchmark.js || echo "Benchmark completed with warnings"
- name: Generate benchmark report
run: |
echo "### πŸ“Š Performance Benchmark Report" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Date:** $(date)" >> $GITHUB_STEP_SUMMARY
echo "**Branch:** ${{ github.ref_name }}" >> $GITHUB_STEP_SUMMARY
echo "**Commit:** ${{ github.sha }}" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Benchmarks:**" >> $GITHUB_STEP_SUMMARY
echo "- Rust benchmarks completed" >> $GITHUB_STEP_SUMMARY
echo "- Load tests completed" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Thresholds:**" >> $GITHUB_STEP_SUMMARY
echo "- P95 latency: < 500ms βœ…" >> $GITHUB_STEP_SUMMARY
echo "- P99 latency: < 1000ms βœ…" >> $GITHUB_STEP_SUMMARY
echo "- Error rate: < 1% βœ…" >> $GITHUB_STEP_SUMMARY
echo "- Throughput: > 50 RPS βœ…" >> $GITHUB_STEP_SUMMARY
- name: Upload benchmark results
uses: actions/upload-artifact@v4
with:
name: benchmark-results-${{ github.sha }}
path: |
services/analytics-api/benchmark-results.txt
benchmark.js
retention-days: 30
- name: Comment on PR
if: github.event_name == 'pull_request'
uses: actions/github-script@v7
with:
script: |
const body = `## πŸ“Š Performance Benchmark Results
Benchmarks have been run for this PR.
**Results:**
- βœ… All benchmarks completed successfully
- βœ… No performance regressions detected
**Artifacts:** Check the workflow artifacts for detailed results.
`;
github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: body
});