name: benchmarking

on:
  pull_request:
    types:
      - closed
    paths-ignore:
      - "**/*.md"

permissions:
  contents: read

defaults:
  run:
    shell: bash

env:
  PYTHONIOENCODING: "utf8"
  TELEMETRY_ENABLED: false
  NODE_OPTIONS: "--max_old_space_size=8192"
  PR_TITLE: ${{ github.event.pull_request.title }}

jobs:
  reflex-web:
    #    if: github.event.pull_request.merged == true
    strategy:
      fail-fast: false
      matrix:
        # Show OS combos first in GUI
        os: [ubuntu-latest]
        python-version: ["3.12.8"]
        node-version: ["18.x"]

    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v4
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}
      - uses: ./.github/actions/setup_build_env
        with:
          python-version: ${{ matrix.python-version }}
          run-uv-sync: true

      - name: Clone Reflex Website Repo
        uses: actions/checkout@v4
        with:
          repository: reflex-dev/reflex-web
          ref: main
          path: reflex-web

      - name: Install Requirements for reflex-web
        working-directory: ./reflex-web
        run: uv pip install -r requirements.txt
      - name: Init Website for reflex-web
        working-directory: ./reflex-web
        run: uv run reflex init
      - name: Install LightHouse Pre-reqs / Run LightHouse
        run: |
          # Check that npm is home
          npm -v
          uv run bash benchmarks/lighthouse.sh ./reflex-web prod
        env:
          LHCI_GITHUB_APP_TOKEN: $
      - name: Run Benchmarks
        # Only run if the database creds are available in this context.
        run: uv run python benchmarks/benchmark_lighthouse.py "$GITHUB_SHA" ./integration/benchmarks/.lighthouseci
        env:
          GITHUB_SHA: ${{ github.sha }}

  reflex-dist-size: # This job is used to calculate the size of the Reflex distribution (wheel file)
    if: github.event.pull_request.merged == true
    timeout-minutes: 30
    strategy:
      # Prioritize getting more information out of the workflow (even if something fails)
      fail-fast: false
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: ./.github/actions/setup_build_env
        with:
          python-version: 3.12.8
          run-uv-sync: true

      - name: Build reflex
        run: |
          uv build
      - name: Upload benchmark results
        # Only run if the database creds are available in this context.
        run:
          uv run python benchmarks/benchmark_package_size.py --os ubuntu-latest
          --python-version 3.12.8 --commit-sha "${{ github.sha }}" --pr-id "${{ github.event.pull_request.id }}"
          --branch-name "${{ github.head_ref || github.ref_name }}"
          --path ./dist

  reflex-venv-size: # This job calculates the total size of Reflex and its dependencies
    if: github.event.pull_request.merged == true
    timeout-minutes: 30
    strategy:
      # Prioritize getting more information out of the workflow (even if something fails)
      fail-fast: false
      matrix:
        # Show OS combos first in GUI
        os: [ubuntu-latest, windows-latest, macos-latest]
        python-version: ["3.12.8"]

    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v4
      - name: Set up python
        id: setup-python
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}
      - name: Install UV
        uses: astral-sh/setup-uv@v5
        with:
          version: 0.6.5

      - name: Run uv sync
        shell: bash
        run: |
          uv pip install --no-install-project --no-dev

      - name: calculate and upload size
        run:
          uv run python benchmarks/benchmark_package_size.py --os "${{ matrix.os }}"
          --python-version "${{ matrix.python-version }}" --commit-sha "${{ github.sha }}"
          --pr-id "${{ github.event.pull_request.id }}"
          --branch-name "${{ github.head_ref || github.ref_name }}"
          --path ./.venv
