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

jobs:
  perfbench:
    name: Benchmark between PR and main
    if: |
      github.event_name == '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: Set up Python
        uses: actions/setup-python@v6
        with:
          python-version: "3.9"

      - 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 }}"
            })
