name: Benchmarks of search for Wikipedia articles (push)

on:
  push:
    branches:
      - main

env:
  BENCH_NAME: "search_wiki"
  INFLUX_TOKEN: ${{ secrets.INFLUX_TOKEN }}

jobs:
  benchmarks:
    name: Run and upload benchmarks
    runs-on: benchmarks
    steps:
      - uses: actions/checkout@v3
      - uses: dtolnay/rust-toolchain@1.81
        with:
          profile: minimal

      # Set variables
      - name: Set current branch name
        shell: bash
        run: echo "name=$(echo ${GITHUB_REF#refs/heads/})" >> $GITHUB_OUTPUT
        id: current_branch
      - name: Set normalized current branch name # Replace `/` by `_` in branch name to avoid issues when pushing to S3
        shell: bash
        run: echo "name=$(echo ${GITHUB_REF#refs/heads/} | tr '/' '_')" >> $GITHUB_OUTPUT
        id: normalized_current_branch
      - name: Set shorter commit SHA
        shell: bash
        run: echo "short=$(echo $GITHUB_SHA | cut -c1-8)" >> $GITHUB_OUTPUT
        id: commit_sha
      - name: Set file basename with format "dataset_branch_commitSHA"
        shell: bash
        run: echo "basename=$(echo ${BENCH_NAME}_${{ steps.normalized_current_branch.outputs.name }}_${{ steps.commit_sha.outputs.short }})" >> $GITHUB_OUTPUT
        id: file

      # Run benchmarks
      - name: Run benchmarks - Dataset ${BENCH_NAME} - Branch ${{ steps.current_branch.outputs.name }} - Commit ${{ steps.commit_sha.outputs.short }}
        run: |
          cd crates/benchmarks
          cargo bench --bench ${BENCH_NAME} -- --save-baseline ${{ steps.file.outputs.basename }}

      # Generate critcmp files
      - name: Install critcmp
        uses: taiki-e/install-action@v2
        with:
          tool: critcmp
      - name: Export cripcmp file
        run: |
          critcmp --export ${{ steps.file.outputs.basename }} > ${{ steps.file.outputs.basename }}.json

      # Upload benchmarks
      - name: Upload ${{ steps.file.outputs.basename }}.json to DO Spaces # DigitalOcean Spaces = S3
        uses: BetaHuhn/do-spaces-action@v2
        with:
          access_key: ${{ secrets.DO_SPACES_ACCESS_KEY }}
          secret_key: ${{ secrets.DO_SPACES_SECRET_KEY }}
          space_name: ${{ secrets.DO_SPACES_SPACE_NAME }}
          space_region: ${{ secrets.DO_SPACES_SPACE_REGION }}
          source: ${{ steps.file.outputs.basename }}.json
          out_dir: critcmp_results

      # Upload benchmarks to influxdb
      - name: Upload ${{ steps.file.outputs.basename }}.json to influxDB
        run: telegraf --config https://eu-central-1-1.aws.cloud2.influxdata.com/api/v2/telegrafs/08b52e34a370b000 --once --debug

      # Helper
      - name: 'README: compare with another benchmark'
        run: |
          echo "${{ steps.file.outputs.basename }}.json has just been pushed."
          echo 'How to compare this benchmark with another one?'
          echo '  - Check the available files with: ./benchmarks/scripts/list.sh'
          echo "  - Run the following command: ./benchmaks/scipts/compare.sh <file-to-compare-with> ${{ steps.file.outputs.basename }}.json"
