name: CI benchmarks

on:
  pull_request:
    types: [synchronize, reopened, labeled]

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

permissions: {}

jobs:
  benchmarks:
    if: |
      github.event_name == 'pull_request' &&
      contains(github.event.pull_request.labels.*.name, 'Run benchmarks')
    runs-on: ubuntu-latest
    steps:
      - name: Checkout sunpy repo
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          fetch-depth: 0
          persist-credentials: false

      - name: Install Python
        uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
        with:
          python-version: "3.14"

      - name: Install dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y libopencv-dev
          python -m pip install --upgrade pip
          pip install asv virtualenv

      - name: Add machine info for ASV
        run: asv machine --yes

      - name: Run benchmarks
        id: asv
        shell: bash -e {0}
        env:
          OPENBLAS_NUM_THREADS: 1
          MKL_NUM_THREADS: 1
          OMP_NUM_THREADS: 1
          ASV_FACTOR: 1.1
          BASE_SHA: ${{ github.event.pull_request.base.sha }}
          COLUMNS: 150  # set a large terminal width so that large tables are rendered in 2D
        run: |
          ASV_OPTIONS="--show-stderr --factor $ASV_FACTOR --cpu-affinity 0"
          echo "TEXT<<EOF" >> $GITHUB_OUTPUT
          asv continuous $ASV_OPTIONS ${BASE_SHA} ${GITHUB_SHA} 2>&1 | tee -a $GITHUB_OUTPUT
          status=${PIPESTATUS[0]}
          echo "EOF" >> $GITHUB_OUTPUT
          exit $status

      - name: Post the summary
        if: always()
        run: |
          echo "$ASV_TEXT" | egrep "\| *Change *\| *Before *\[|BENCHMARKS NOT SIGNIFICANTLY CHANGED." -A 1000 | tee -a $GITHUB_STEP_SUMMARY
        env:
          ASV_TEXT: "${{ steps.asv.outputs.TEXT }}"
