name: Performance Benchmark Bot

on:
  issue_comment:
    types:
      - created

permissions:
  contents: read

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

env:
  PYTHONDEVMODE: "1"
  PYTHONUNBUFFERED: "1"
  PYTHONPATH: "" # explicit cleanup
  PIP_USER: "" # explicit cleanup
  COLUMNS: "100"
  FORCE_COLOR: "1"
  CLICOLOR_FORCE: "1"
  XDG_CACHE_HOME: "${{ github.workspace }}/.cache" # to be updated
  PIP_CACHE_DIR: "${{ github.workspace }}/.cache/pip" # to be updated

jobs:
  perfbench:
    name: Benchmark between PR and main
    if: |
      github.repository_owner == 'tile-ai' &&
      github.event.issue.pull_request &&
      (contains(github.event.comment.body, '/performance-report') || contains(github.event.comment.body, '/perf'))
    runs-on: [self-hosted, nvidia]
    steps:
      - name: Checkout repository
        uses: actions/checkout@v5
        with:
          ref: refs/pull/${{ github.event.issue.number }}/merge
          fetch-depth: 0
          submodules: recursive

      - name: Setup Python
        uses: actions/setup-python@v6
        with:
          python-version: "3.12"
          update-environment: true
          cache: pip
          cache-dependency-path: |
            pyproject.toml
            requirements*.txt

      - name: Install merged version
        run: |
          python -m venv tll
          source tll/bin/activate
          pip install -r requirements-test.txt
          pip install .

      - name: Install original version
        run: |
          echo "Check files to be deleted!"
          git clean -dxf -e tll/
          echo "Delete files completed!"
          git checkout main
          python -m venv tl
          source tl/bin/activate
          pip install -r requirements-test.txt
          pip install .

      - name: Run performance test
        id: perfbench
        run: |
          source tl/bin/activate
          python maint/scripts/ci_performance.py

      - name: Post test results as PR comment
        uses: actions/github-script@v8
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          script: |
            github.rest.issues.createComment({
              owner: context.repo.owner,
              repo: context.repo.repo,
              issue_number: context.issue.number,
              body: '📊 ​**Performance Test Results** (triggered by @' + context.payload.comment.user.login + '):\n\n' +
                'Run listed here: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}\n\n' +
                "${{ steps.perfbench.outputs.stdout }}"
            })
