|
| 1 | +name: benchmark |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: |
| 6 | + - master |
| 7 | + types: [opened, synchronize, reopened] |
| 8 | + |
| 9 | +jobs: |
| 10 | + benchmark: |
| 11 | + runs-on: ubuntu-latest |
| 12 | + |
| 13 | + steps: |
| 14 | + # Step 1: Checkout the PR branch |
| 15 | + - name: Checkout PR branch |
| 16 | + uses: actions/checkout@v4 |
| 17 | + with: |
| 18 | + fetch-depth: 0 # Fetch all history for comparison |
| 19 | + |
| 20 | + # Step 2: Setup PHP |
| 21 | + - name: Setup PHP |
| 22 | + uses: shivammathur/setup-php@bf6b4fbd49ca58e4608c9c89fba0b8d90bd2a39f |
| 23 | + with: |
| 24 | + php-version: '8.4' |
| 25 | + extensions: zip, curl, mbstring, xml |
| 26 | + coverage: none |
| 27 | + |
| 28 | + # Step 3: Install dependencies for PR branch |
| 29 | + - name: Install Composer dependencies |
| 30 | + run: | |
| 31 | + composer install --prefer-dist --no-progress --no-suggest |
| 32 | + composer require --dev phpbench/phpbench |
| 33 | +
|
| 34 | + # Step 4: Run benchmarks on PR branch |
| 35 | + - name: Run benchmarks on PR branch |
| 36 | + run: | |
| 37 | + mkdir -p tests/Benchmark |
| 38 | + ./vendor/bin/phpbench run tests/Benchmark \ |
| 39 | + --report=default \ |
| 40 | + --tag=pr \ |
| 41 | + --retry-threshold=5 \ |
| 42 | + --iterations=10 |
| 43 | +
|
| 44 | + # Step 5: Checkout base branch |
| 45 | + - name: Checkout base branch |
| 46 | + run: | |
| 47 | + git fetch origin "$BASE_REF" |
| 48 | + git checkout "origin/$BASE_REF" |
| 49 | + env: |
| 50 | + BASE_REF: ${{ github.base_ref }} |
| 51 | + |
| 52 | + # Step 6: Install dependencies for base branch |
| 53 | + - name: Install Composer dependencies (base) |
| 54 | + run: | |
| 55 | + composer install --prefer-dist --no-progress --no-suggest |
| 56 | + composer require --dev phpbench/phpbench |
| 57 | +
|
| 58 | + # Step 7: Run benchmarks on base branch |
| 59 | + - name: Run benchmarks on base branch |
| 60 | + run: | |
| 61 | + mkdir -p tests/Benchmark |
| 62 | + ./vendor/bin/phpbench run tests/Benchmark \ |
| 63 | + --report=default \ |
| 64 | + --tag=base \ |
| 65 | + --retry-threshold=5 \ |
| 66 | + --iterations=10 |
| 67 | +
|
| 68 | + # Step 8: Checkout PR branch again |
| 69 | + - name: Checkout PR branch again |
| 70 | + run: git checkout "$HEAD_REF" |
| 71 | + env: |
| 72 | + HEAD_REF: ${{ github.head_ref }} |
| 73 | + |
| 74 | + # Step 9: Compare benchmarks |
| 75 | + - name: Compare benchmarks with baseline |
| 76 | + id: compare |
| 77 | + run: | |
| 78 | + mkdir -p tests/Benchmark |
| 79 | + ./vendor/bin/phpbench run tests/Benchmark \ |
| 80 | + --report=aggregate \ |
| 81 | + --ref=base \ |
| 82 | + --retry-threshold=5 \ |
| 83 | + --iterations=10 \ |
| 84 | + --tag=pr \ |
| 85 | + --assert="mode(variant.time.avg) <= mode(baseline.time.avg) +/- 10%" \ |
| 86 | + | tee benchmark-comparison.txt |
| 87 | + continue-on-error: true |
| 88 | + |
| 89 | + # Step 10: Post results as PR comment |
| 90 | + - name: Comment PR with benchmark results |
| 91 | + uses: actions/github-script@v6 |
| 92 | + with: |
| 93 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 94 | + script: | |
| 95 | + const fs = require('fs'); |
| 96 | + const results = fs.readFileSync('benchmark-comparison.txt', 'utf8'); |
| 97 | +
|
| 98 | + const body = `## 📊 Benchmark Results\n\n\`\`\`\n${results}\n\`\`\`\n\n**Note:** Benchmarks compare PR against \`${{ github.base_ref }}\` branch.\nPerformance regression threshold: ±10%`; |
| 99 | +
|
| 100 | + github.rest.issues.createComment({ |
| 101 | + issue_number: context.issue.number, |
| 102 | + owner: context.repo.owner, |
| 103 | + repo: context.repo.repo, |
| 104 | + body: body |
| 105 | + }); |
| 106 | +
|
| 107 | + # Step 11: Fail if performance degrades |
| 108 | + - name: Check benchmark assertions |
| 109 | + if: steps.compare.outcome == 'failure' |
| 110 | + run: | |
| 111 | + echo "::error::Performance regression detected! Benchmarks exceeded acceptable threshold." |
| 112 | + exit 1 |
0 commit comments