# This workflow will run daily at 06:45.
# It will run benchmarks with airspeed velocity (asv)
# and compare performance with the previous nightly build.
name: Run benchmarks nightly job

on:
  schedule:
    - cron: 45 6 * * *
  workflow_dispatch:
  
env:
  PYTHON_VERSION: "3.11"
  ASV_VERSION: "0.6.5"
  WORKING_DIR: ${{github.workspace}}/benchmarks
  NIGHTLY_HASH_FILE: nightly-hash

jobs:
  asv-nightly:
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: ${{env.WORKING_DIR}}
    steps:
    - name: Set up Python ${{env.PYTHON_VERSION}}
      uses: actions/setup-python@v6
      with:
        python-version: ${{env.PYTHON_VERSION}}
    - name: Checkout main branch of the repository
      uses: actions/checkout@v6
      with:
        fetch-depth: 0
    - name: Install dependencies
      run: pip install asv[virtualenv]==${{env.ASV_VERSION}}
    - name: Configure git
      run: |
        git config user.name "github-actions[bot]"
        git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
    - name: Create ASV machine config file
      run: asv machine --machine gh-runner --yes
    - name: Fetch previous results from the "benchmarks" branch
      run: |
        if git ls-remote --exit-code origin benchmarks > /dev/null 2>&1; then
          git merge origin/benchmarks \
            --allow-unrelated-histories \
            --no-commit
          mv ../_results .
        fi
    - name: Get nightly dates under comparison
      id: nightly-dates
      run: |
        echo "yesterday=$(date -d yesterday +'%Y-%m-%d')" >> $GITHUB_OUTPUT
        echo "today=$(date +'%Y-%m-%d')" >> $GITHUB_OUTPUT
    - name: Use last nightly commit hash from cache
      uses: actions/cache@v4
      with:
        path: ${{env.WORKING_DIR}}
        key: nightly-results-${{steps.nightly-dates.outputs.yesterday}}
    - name: Run comparison of main against last nightly build
      run: |
        HASH_FILE=${{env.NIGHTLY_HASH_FILE}}
        CURRENT_HASH=${{github.sha}}
        if [ -f $HASH_FILE ]; then
          PREV_HASH=$(cat $HASH_FILE)
          asv continuous $PREV_HASH $CURRENT_HASH --verbose || true
          asv compare $PREV_HASH $CURRENT_HASH --sort ratio --verbose
        fi
        echo $CURRENT_HASH > $HASH_FILE
    - name: Update last nightly hash in cache
      uses: actions/cache@v4
      with:
        path: ${{env.WORKING_DIR}}
        key: nightly-results-${{steps.nightly-dates.outputs.today}}