name: Benchmark
on:
pull_request:
branches: [main]
push:
branches: [main]
permissions:
contents: read
pull-requests: write
checks: write
env:
CARGO_TERM_COLOR: always
PROTOC_VERSION: "3.25.3"
BENCHER_PROJECT: whatsapp-rust
BENCHER_TESTBED: ubuntu-latest
BENCHER_ADAPTER: rust_iai_callgrind
jobs:
benchmark:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@master
with:
toolchain: nightly-2026-01-30
- name: Install tools (protoc, valgrind, iai-callgrind-runner)
uses: taiki-e/install-action@v2
with:
tool: protoc@${{ env.PROTOC_VERSION }},valgrind,iai-callgrind-runner@0.16.1
- name: Cache Rust dependencies
uses: Swatinem/rust-cache@v2
with:
prefix-key: ${{ runner.os }}-cargo-bench
- uses: bencherdev/bencher@v0.5.9
- name: Run benchmarks
env:
BENCHER_API_TOKEN: ${{ secrets.BENCHER_API_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# Common bencher args (shared across all scenarios)
BENCHER_ARGS=(
--project "$BENCHER_PROJECT"
--testbed "$BENCHER_TESTBED"
--adapter "$BENCHER_ADAPTER"
--github-actions "$GITHUB_TOKEN"
)
if [ "$GITHUB_EVENT_NAME" == "push" ]; then
# Push to main: establish baseline (no GitHub check posting)
bencher run \
--project "$BENCHER_PROJECT" \
--testbed "$BENCHER_TESTBED" \
--adapter "$BENCHER_ADAPTER" \
--token "$BENCHER_API_TOKEN" \
--branch main \
--threshold-measure instructions \
--threshold-test percentage \
--threshold-upper-boundary 0.05 \
--thresholds-reset \
"cargo bench --workspace"
elif [ -n "$BENCHER_API_TOKEN" ]; then
# Pull request with token available: full tracking
bencher run \
"${BENCHER_ARGS[@]}" \
--token "$BENCHER_API_TOKEN" \
--branch "${{ github.head_ref }}" \
--start-point "${{ github.base_ref }}" \
--start-point-clone-thresholds \
--start-point-reset \
--threshold-measure instructions \
--threshold-test percentage \
--threshold-upper-boundary 0.05 \
--thresholds-reset \
--ci-only-thresholds \
"cargo bench --workspace"
else
# Pull request without token (fork PRs): run benchmarks locally
echo "::warning title=Bencher::BENCHER_API_TOKEN not available. Running benchmarks without analytics."
cargo bench --workspace
fi