implement benchmarks in ci #67
Workflow file for this run
This file contains 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: Go | |
on: | |
push: | |
branches: [main] | |
pull_request: | |
branches: [main] | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Set up Go | |
uses: actions/setup-go@v3 | |
with: | |
go-version: 1.23 | |
- name: Build | |
run: go build -v ./... | |
- name: Test | |
run: go test -v ./... | |
cache-main-benchmark: | |
runs-on: ubuntu-latest | |
needs: build | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Cache Main Branch Benchmarks | |
id: cache-benchmark | |
uses: actions/cache@v3 | |
with: | |
path: ./main_benchmarks.txt | |
key: main-benchmarks-${{ runner.os }}-${{ github.sha }} | |
restore-keys: | | |
main-benchmarks-${{ runner.os }}- | |
- name: Run Benchmarks on Main Branch (if no cache) | |
if: steps.cache-benchmark.outputs.cache-hit != 'true' | |
run: | | |
git fetch origin main | |
git checkout origin/main | |
go test -bench . -benchmem -run=^$ ./... > main_benchmarks.txt | |
continue-on-error: true | |
- name: Save Main Benchmark Results | |
uses: actions/upload-artifact@v3 | |
with: | |
name: main_benchmarks | |
path: main_benchmarks.txt | |
run-pr-benchmark: | |
runs-on: ubuntu-latest | |
needs: cache-main-benchmark | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Run Benchmarks on PR Branch | |
run: | | |
go test -bench . -benchmem -run=^$ ./... > pr_benchmarks.txt | |
continue-on-error: true | |
- name: Save PR Benchmark Results | |
uses: actions/upload-artifact@v3 | |
with: | |
name: pr_benchmarks | |
path: pr_benchmarks.txt | |
compare-benchmarks: | |
runs-on: ubuntu-latest | |
needs: run-pr-benchmark | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Download Main and PR Benchmark Results | |
uses: actions/download-artifact@v3 | |
with: | |
name: main_benchmarks | |
- uses: actions/download-artifact@v3 | |
with: | |
name: pr_benchmarks | |
- name: Compare Benchmarks | |
id: benchmark-compare | |
run: | | |
echo "Comparing benchmarks..." | |
diff_output=$(diff ./main_benchmarks.txt ./pr_benchmarks.txt || true) | |
echo "$diff_output" > ./diff_output.txt | |
cat ./diff_output.txt | |
- name: Display Benchmark Results | |
run: | | |
echo "Benchmark results for PR branch:" | |
cat ./pr_benchmarks.txt | |
echo "" | |
echo "Benchmark results for Main branch:" | |
cat ./main_benchmarks.txt | |
- name: Comment on PR with Benchmark Comparison | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const fs = require('fs'); | |
const diff = fs.readFileSync('diff_output.txt', 'utf8'); | |
if (diff) { | |
github.rest.issues.createComment({ | |
issue_number: context.payload.pull_request.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: `### Benchmark Comparison:\n\`\`\`\n${diff}\n\`\`\`` | |
}); | |
} else { | |
github.rest.issues.createComment({ | |
issue_number: context.payload.pull_request.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: `### Benchmark Comparison:\nNo differences found.` | |
}); | |
} |