name: Benchmark

on:
  push:
    branches: [master]
  schedule:
    # Run every Sunday at 03:00 UTC
    - cron: '0 3 * * 0'

env:
  FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true

jobs:
  benchmark-macos:
    runs-on: macos-14
    name: Benchmark (macOS)
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v6
        with:
          token: ${{ secrets.GITHUB_TOKEN }}

      - uses: dtolnay/rust-toolchain@stable

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

      - name: Cache model
        id: cache-model
        uses: actions/cache@v5
        with:
          path: ~/.phostt/models
          key: phostt-model-v3-onnx-${{ hashFiles('src/model/mod.rs') }}

      - name: Download model
        if: steps.cache-model.outputs.cache-hit != 'true'
        run: cargo run --release -- download

      - name: Run CPU benchmark
        id: cpu
        run: |
          /usr/bin/time -l cargo test --release --test benchmark_report -- --ignored --nocapture 2>&1 | tee /tmp/cpu_benchmark.txt

      - name: Run CoreML benchmark
        id: coreml
        run: |
          /usr/bin/time -l cargo test --release --test benchmark_report --features coreml -- --ignored --nocapture 2>&1 | tee /tmp/coreml_benchmark.txt

      - name: Parse results and update BENCHMARKS.md
        run: |
          COMMIT=$(git rev-parse --short HEAD)
          DATE=$(date -u +%Y-%m-%d)

          CPU_LATENCY=$(grep "Mean latency" /tmp/cpu_benchmark.txt | sed 's/.*| \([0-9.]*\) ms.*/\1/')
          CPU_RTF=$(grep "RTF" /tmp/cpu_benchmark.txt | sed 's/.*| \([0-9.]*\)×.*/\1/')
          CPU_RSS=$(grep "maximum resident set size" /tmp/cpu_benchmark.txt | awk '{print $1}')
          CPU_RSS_MB=$(echo "scale=1; $CPU_RSS / 1024 / 1024" | bc)

          COREML_LATENCY=$(grep "Mean latency" /tmp/coreml_benchmark.txt | sed 's/.*| \([0-9.]*\) ms.*/\1/')
          COREML_RTF=$(grep "RTF" /tmp/coreml_benchmark.txt | sed 's/.*| \([0-9.]*\)×.*/\1/')
          COREML_RSS=$(grep "maximum resident set size" /tmp/coreml_benchmark.txt | awk '{print $1}')
          COREML_RSS_MB=$(echo "scale=1; $COREML_RSS / 1024 / 1024" | bc)

          {
            echo ""
            echo "## ${DATE} (commit ${COMMIT})"
            echo ""
            echo "### macOS-14 (Apple Silicon)"
            echo ""
            echo "| Backend | Mean Latency | RTF | Peak RSS |"
            echo "|---|---|---|---|"
            echo "| CPU | ${CPU_LATENCY} ms | ${CPU_RTF} | ${CPU_RSS_MB} MB |"
            echo "| CoreML | ${COREML_LATENCY} ms | ${COREML_RTF} | ${COREML_RSS_MB} MB |"
          } >> BENCHMARKS.md

      - name: Commit benchmark results
        uses: stefanzweifel/git-auto-commit-action@v5
        with:
          commit_message: "benchmark: auto-update BENCHMARKS.md [skip ci]"
          file_pattern: BENCHMARKS.md
