name: performance-tests
permissions:
  contents: read

on:
  push:
    branches:
      - "main" # or "master"
    paths-ignore:
      - "**/*.md"
  pull_request:
  workflow_dispatch:

env:
  REFLEX_TELEMETRY_ENABLED: false
  NODE_OPTIONS: "--max_old_space_size=8192"
  APP_HARNESS_HEADLESS: 1
  PYTHONUNBUFFERED: 1

jobs:
  benchmarks:
    name: Run benchmarks
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          fetch-tags: true
          fetch-depth: 0
          persist-credentials: false

      - name: Install uv
        uses: astral-sh/setup-uv@08807647e7069bb48b6ef5acd8ec9567f424441b # v8.1.0

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

      - name: Install dependencies
        run: uv sync --all-extras --dev

      - name: Run benchmarks
        uses: CodSpeedHQ/action@3194d9a39c4d46684cb44bf7207fc56626aad8fd # v4.15.1
        with:
          mode: instrumentation
          run: uv run pytest -v tests/benchmarks --codspeed

  lighthouse:
    name: Run Lighthouse benchmark
    runs-on: ubuntu-22.04
    timeout-minutes: 30
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          fetch-tags: true
          fetch-depth: 0
          persist-credentials: false

      - uses: ./.github/actions/setup_build_env
        with:
          python-version: "3.14"
          node-version: "22"
          run-uv-sync: true

      - name: Install playwright
        run: uv run playwright install chromium --only-shell

      - name: Run Lighthouse benchmark
        env:
          REFLEX_RUN_LIGHTHOUSE: "1"
        run: |
          mkdir -p .pytest-tmp/lighthouse
          uv run pytest tests/integration/test_lighthouse.py -q -s --tb=no --basetemp=.pytest-tmp/lighthouse

      - name: Upload Lighthouse artifacts
        if: always()
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: lighthouse-report
          path: |
            .pytest-tmp/lighthouse/**
            !.pytest-tmp/lighthouse/*current
            !.pytest-tmp/lighthouse/*current/**
          if-no-files-found: ignore
