name: Benchmarks

on:
  push:
    branches:
      - "main"
  pull_request:
  workflow_dispatch:

jobs:
  benchmarks:
    name: Run Benchmarks
    runs-on: ubuntu-latest
    strategy:
      matrix:
        benchmark:
          - crypto_bench
          - event_cache
          - linked_chunk
          - store_bench
          - timeline
          - room_list

    steps:
      # This CI workflow can run into space issue, so we're cleaning up some
      # space here.
      - name: Create some more space
        run: |
          echo "Disk space before cleanup"
          df -h

          cd /opt
          find . -maxdepth 1 -mindepth 1 '!' -path ./containerd '!' -path ./actionarchivecache '!' -path ./runner '!' -path ./runner-cache -exec rm -rf '{}' ';'
          rm -rf /opt/hostedtoolcache

          # Get rid of binaries and libs we're not interested in.
          sudo rm -rf \
            /usr/local/julia* \
            /usr/local/aws*

          sudo rm -rf \
            /usr/local/bin/minikube \
            /usr/local/bin/node \
            /usr/local/bin/stack \
            /usr/local/bin/bicep \
            /usr/local/bin/pulumi* \
            /usr/local/bin/helm \
            /usr/local/bin/azcopy \
            /usr/local/bin/packer \
            /usr/local/bin/cmake-gui \
            /usr/local/bin/cpack

          sudo rm -rf \
            /usr/local/share/powershell \
            /usr/local/share/chromium

          sudo rm -rf /usr/local/lib/android

          echo "::group::/usr/local/bin/*"
          du -hsc /usr/local/bin/* | sort -h
          echo "::endgroup::"

          echo "::group::/usr/local/share/*"
          du -hsc /usr/local/share/* | sort -h
          echo "::endgroup::"

          echo "::group::/usr/local/*"
          du -hsc /usr/local/* | sort -h
          echo "::endgroup::"

          echo "::group::/usr/local/lib/*"
          du -hsc /usr/local/lib/* | sort -h
          echo "::endgroup::"

          echo "::group::/opt/*"
          du -hsc /opt/* | sort -h
          echo "::endgroup::"

          echo "Disk space after cleanup"
          df -h

      - uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3

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

      - name: Build the benchmark target(s)
        run: cargo codspeed build -p benchmarks --bench ${{ matrix.benchmark }} --features codspeed

      - name: Run the benchmarks
        uses: CodSpeedHQ/action@346a2d8a8d9d38909abd0bc3d23f773110f076ad
        with:
          run: cargo codspeed run
          mode: "instrumentation"
          token: ${{ secrets.CODSPEED_TOKEN }}
