on:
  workflow_call:
    inputs:
      save-rust-cache:
        required: false
        type: string
        default: "true"

permissions: {}

env:
  CARGO_INCREMENTAL: 0
  CARGO_NET_RETRY: 10
  CARGO_TERM_COLOR: always
  RUSTUP_MAX_RETRIES: 10

jobs:
  benchmarks-walltime-build:
    name: "walltime build"
    # codspeed-macro doesn't support Ubuntu 24.04 yet
    runs-on: depot-ubuntu-22.04-arm-4
    if: ${{ github.repository == 'astral-sh/uv' }}
    timeout-minutes: 20
    steps:
      - name: "Checkout Branch"
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false

      - uses: Swatinem/rust-cache@c19371144df3bb44fab255c43d04cbc2ab54d1c4 # v2.9.1
        with:
          save-if: ${{ inputs.save-rust-cache == 'true' }}

      - name: "Install Rust toolchain"
        run: rustup show

      - name: "Install codspeed"
        uses: taiki-e/install-action@cf525cb33f51aca27cd6fa02034117ab963ff9f1 # v2.75.22
        with:
          tool: cargo-codspeed

      - name: "Install requirements and prime cache"
        run: |
          sudo apt-get update
          sudo apt-get install -y libsasl2-dev libldap2-dev libkrb5-dev
          cargo run --bin uv -- venv --cache-dir .cache
          cargo run --bin uv -- pip compile test/requirements/jupyter.in --universal --exclude-newer 2024-08-08 --cache-dir .cache
          cargo run --bin uv -- pip compile test/requirements/airflow.in --universal --exclude-newer 2024-08-08 --cache-dir .cache

      - name: "Build benchmarks"
        run: cargo codspeed build -m walltime --profile profiling -p uv-bench

      - name: "Create artifact archive"
        run: tar -cvf benchmarks-walltime.tar target/codspeed target/debug/uv .cache

      - name: "Upload benchmark artifacts"
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: benchmarks-walltime
          path: benchmarks-walltime.tar
          retention-days: 1

  benchmarks-walltime-run:
    name: "walltime on aarch64 linux"
    runs-on: codspeed-macro
    needs: benchmarks-walltime-build
    timeout-minutes: 20
    steps:
      - name: "Checkout Branch"
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false

      - name: "Install codspeed"
        uses: taiki-e/install-action@cf525cb33f51aca27cd6fa02034117ab963ff9f1 # v2.75.22
        with:
          tool: cargo-codspeed

      - name: "Download benchmark artifacts"
        uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
        with:
          name: benchmarks-walltime

      - name: "Extract artifact archive"
        run: tar -xvf benchmarks-walltime.tar

      - name: "Create venv"
        run: ./target/debug/uv venv --cache-dir .cache

      - name: "Run benchmarks"
        uses: CodSpeedHQ/action@658a901452bb54c799643e060733b7afe9121b8d # v4.14.0
        with:
          run: cargo codspeed run
          mode: walltime
          token: ${{ secrets.CODSPEED_TOKEN }}

  benchmarks-simulated:
    name: "simulated"
    runs-on: depot-ubuntu-24.04-4
    if: ${{ github.repository == 'astral-sh/uv' }}
    timeout-minutes: 20
    steps:
      - name: "Checkout Branch"
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false

      - uses: Swatinem/rust-cache@c19371144df3bb44fab255c43d04cbc2ab54d1c4 # v2.9.1
        with:
          save-if: ${{ inputs.save-rust-cache == 'true' }}

      - name: "Install Rust toolchain"
        run: rustup show

      - name: "Install codspeed"
        uses: taiki-e/install-action@cf525cb33f51aca27cd6fa02034117ab963ff9f1 # v2.75.22
        with:
          tool: cargo-codspeed

      - name: "Install requirements and prime cache"
        run: |
          sudo apt-get update
          sudo apt-get install -y libsasl2-dev libldap2-dev libkrb5-dev
          cargo run --bin uv -- venv --cache-dir .cache
          cargo run --bin uv -- pip compile test/requirements/jupyter.in --universal --exclude-newer 2024-08-08 --cache-dir .cache
          cargo run --bin uv -- pip compile test/requirements/airflow.in --universal --exclude-newer 2024-08-08 --cache-dir .cache

      - name: "Build benchmarks"
        run: cargo codspeed build --profile profiling -p uv-bench

      - name: "Run benchmarks"
        uses: CodSpeedHQ/action@658a901452bb54c799643e060733b7afe9121b8d # v4.14.0
        with:
          run: cargo codspeed run
          mode: simulation
          token: ${{ secrets.CODSPEED_TOKEN }}
