# Run performance benchmarks
name: Benchmarks

on:
  push:
    branches: ["main"]
  # Run on PRs but only if the PR has the 'run/benchmark' label
  pull_request:
    types: [opened, reopened, labeled, synchronize]
    branches: ["main"]
  workflow_dispatch:

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

env:
  CARGO_TERM_COLOR: always

jobs:
  rust-bench:
    runs-on: ubuntu-24.04
    if: github.repository == 'developmentseed/async-tiff' && (github.event_name != 'pull_request' || contains(github.event.pull_request.labels.*.name, 'run/benchmark'))
    container:
      image: ghcr.io/osgeo/gdal:ubuntu-small-3.12.2
      options: --privileged
    permissions:
      contents: read # required for actions/checkout
      id-token: write # required for OIDC authentication with CodSpeed

    steps:
      - name: Install dev dependencies and setup git
        run: |
          apt update
          apt install -y build-essential cmake git jq libclang-dev pkg-config
          git config --global --add safe.directory $GITHUB_WORKSPACE

      - name: Checkout repository
        uses: actions/checkout@v6
        with:
          persist-credentials: false

      - name: Download and extract files
        run: |
          gdal raster convert --co COMPRESS=LZW --co TILED=YES --co PREDICTOR=2 https://sentinel-cogs.s3.us-west-2.amazonaws.com/sentinel-s2-l2a-cogs/12/S/UF/2022/6/S2B_12SUF_20220609_0_L2A/TCI.tif benches/TCI_lzw.tif
          ls -lh benches/

      - name: Setup rust toolchain, cache and cargo-codspeed binary
        uses: moonrepo/setup-rust@v1
        with:
          channel: stable
          cache: false
          cache-target: release
          bins: cargo-codspeed

      - name: Build the benchmark target(s)
        run: cargo codspeed build

      - name: Run the benchmarks
        uses: CodSpeedHQ/action@v4
        with:
          mode: simulation
          run: cargo codspeed run
