ci: implement benchmarks and automated CI workflow #5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Benchmark | |
| on: | |
| pull_request: | |
| branches: | |
| - master | |
| permissions: | |
| contents: read | |
| pull-requests: write | |
| jobs: | |
| benchmark: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout PR branch | |
| uses: actions/checkout@v3 | |
| with: | |
| ref: ${{ github.event.pull_request.head.sha }} | |
| fetch-depth: 0 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v3 | |
| with: | |
| node-version: '20' | |
| cache: 'yarn' | |
| - name: Install dependencies | |
| run: yarn install --frozen-lockfile | |
| - name: Build project | |
| run: yarn build | |
| - name: Run PR benchmarks | |
| id: pr-benchmark | |
| continue-on-error: true | |
| run: | | |
| echo "Running benchmarks on PR branch..." | |
| yarn benchmark > pr-benchmark.txt 2>&1 | |
| EXIT_CODE=$? | |
| cat pr-benchmark.txt | |
| exit $EXIT_CODE | |
| - name: Check PR benchmark status | |
| if: steps.pr-benchmark.outcome == 'failure' | |
| run: echo "⚠️ PR benchmarks failed - will attempt to extract partial results" | |
| - name: Extract PR results | |
| id: pr-results | |
| run: node benchmark/extract-results.js pr-benchmark.txt pr-results.json | |
| - name: Save benchmark scripts | |
| run: | | |
| mkdir -p /tmp/benchmark-scripts | |
| cp benchmark/extract-results.js /tmp/benchmark-scripts/ | |
| cp benchmark/compare-results.js /tmp/benchmark-scripts/ | |
| - name: Checkout base branch | |
| run: | | |
| git fetch origin ${{ github.event.pull_request.base.ref }} | |
| git checkout origin/${{ github.event.pull_request.base.ref }} | |
| - name: Install dependencies (base) | |
| run: yarn install --frozen-lockfile | |
| - name: Build project (base) | |
| run: yarn build | |
| - name: Run base benchmarks | |
| id: base-benchmark | |
| continue-on-error: true | |
| run: | | |
| echo "Running benchmarks on base branch..." | |
| yarn benchmark > base-benchmark.txt 2>&1 | |
| EXIT_CODE=$? | |
| cat base-benchmark.txt | |
| exit $EXIT_CODE | |
| - name: Check base benchmark status | |
| if: steps.base-benchmark.outcome == 'failure' | |
| run: echo "⚠️ Base benchmarks failed - will attempt to extract partial results" | |
| - name: Extract base results | |
| id: base-results | |
| run: node /tmp/benchmark-scripts/extract-results.js base-benchmark.txt base-results.json | |
| - name: Compare and format results | |
| id: compare | |
| run: node /tmp/benchmark-scripts/compare-results.js base-results.json pr-results.json > comment.txt | |
| - name: Post comment to PR | |
| uses: actions/github-script@v6 | |
| with: | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| script: | | |
| const fs = require('fs'); | |
| const comment = fs.readFileSync('comment.txt', 'utf8'); | |
| // Find existing benchmark comment | |
| const { data: comments } = await github.rest.issues.listComments({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: context.issue.number, | |
| }); | |
| const benchmarkComment = comments.find(comment => | |
| comment.body.includes('📊 Benchmark Results') | |
| ); | |
| if (benchmarkComment) { | |
| // Update existing comment | |
| await github.rest.issues.updateComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| comment_id: benchmarkComment.id, | |
| body: comment | |
| }); | |
| } else { | |
| // Create new comment | |
| await github.rest.issues.createComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: context.issue.number, | |
| body: comment | |
| }); | |
| } |