name: rosenpass - protocol - benchmark

permissions:
  contents: write

on:
  #pull_request:
  push:

env:
  CARGO_TERM_COLOR: always

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

jobs:
  proto-benchmark:
    strategy:
      fail-fast: true
      matrix:
        system: ["x86_64-linux", "i686-linux"]

    runs-on: ubicloud-standard-8
    defaults:
      run:
        shell: bash

    steps:
      - uses: actions/checkout@v5

      # Install nix

      - name: Install Nix
        uses: cachix/install-nix-action@v27 # A popular action for installing Nix
        with:
          extra_nix_config: |
            experimental-features = nix-command flakes
            access-tokens = github.com=${{ secrets.GITHUB_TOKEN }}

      # Set up environment

      - name: 🛠️ Prepare Benchmark Path
        env:
          EVENT_NAME: ${{ github.event_name }}
          BRANCH_NAME: ${{ github.ref_name   }}
          PR_NUMBER: ${{ github.event.pull_request.number }}
        run: |
          case "$EVENT_NAME" in
          "push")
            echo "BENCH_PATH=branch/$BRANCH_NAME" >> $GITHUB_ENV
            ;;
          "pull_request")
            echo "BENCH_PATH=pull/$PR_NUMBER" >> $GITHUB_ENV
            ;;
          *)
            echo "don't know benchmark path for event of type $EVENT_NAME, aborting"
            exit 1
          esac

      # Benchmarks ...

      - name: 🏃🏻‍♀️ Benchmarks
        run: nix develop ".#devShells.${{ matrix.system }}.benchmarks" --command cargo bench -p rosenpass --bench trace_handshake -F trace_bench --verbose >bench-protocol.json

      - name: Upload benchmarks
        uses: cryspen/benchmark-upload-and-plot-action@v3
        with:
          name: Protocol Benchmarks
          group-by: "operating system,architecture,protocol version,run time"
          schema: "operating system,architecture,protocol version,run time,name"
          input-data-path: bench-protocol.json
          github-token: ${{ secrets.GITHUB_TOKEN }}
          # NOTE: pushes to current repository
          gh-repository: github.com/${{ github.repository }}
          auto-push: true
          fail-on-alert: true
          base-path: benchmarks/

  ciphers-protocol-bench-status:
    if: ${{ always() }}
    needs: [proto-benchmark]
    runs-on: ubicloud-standard-2
    steps:
      - name: Successful
        if: ${{ !(contains(needs.*.result, 'failure')) }}
        run: exit 0
      - name: Failing
        if: ${{ (contains(needs.*.result, 'failure')) }}
        run: exit 1
