name: Benchmarks

on:
  push:
    branches: [main, v2]
  pull_request:
    branches: [main, v2]
  workflow_dispatch:

env:
  CARGO_TERM_COLOR: always

jobs:
  bench:
    name: Run Criterion benchmarks
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install Rust
        uses: dtolnay/rust-action@stable

      - name: Install capnp
        run: sudo apt-get update && sudo apt-get install -y capnproto

      - name: Cache cargo
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-bench-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-bench-

      - name: Run benchmarks
        run: cargo bench --package quicprochat-core -- --output-format=bencher 2>&1 | tee bench-output.txt

      - name: Upload HTML reports
        uses: actions/upload-artifact@v4
        if: always()
        with:
          name: criterion-reports
          path: target/criterion/
          retention-days: 30

      - name: Upload raw output
        uses: actions/upload-artifact@v4
        if: always()
        with:
          name: bench-output
          path: bench-output.txt
          retention-days: 30
