name: Daily Benchmark

on:
  workflow_dispatch:
  schedule:
    - cron: "0 22 * * *"

jobs:
  release_benchmarks:
    name: "Release benchmarks"
    runs-on: [self-hosted, Linux, X64, Diff, Gen7]
    env:
      THREADS: 24
      MEMGRAPH_ENTERPRISE_LICENSE: ${{ secrets.MEMGRAPH_ENTERPRISE_LICENSE }}
      MEMGRAPH_ORGANIZATION_NAME: ${{ secrets.MEMGRAPH_ORGANIZATION_NAME }}

    steps:
      - name: Set up repository
        uses: actions/checkout@v3
        with:
          # Number of commits to fetch. `0` indicates all history for all
          # branches and tags. (default: 1)
          fetch-depth: 0

      - name: Build release binaries
        run: |
          # Activate toolchain.
          source /opt/toolchain-v4/activate

          # Initialize dependencies.
          ./init

          # Build only memgraph release binaries.
          cd build
          cmake -DCMAKE_BUILD_TYPE=release ..
          make -j$THREADS

      - name: Run macro benchmarks
        run: |
          cd tests/macro_benchmark
          ./harness QuerySuite MemgraphRunner \
            --groups aggregation 1000_create unwind_create dense_expand match \
            --no-strict

      - name: Get branch name (merge)
        if: github.event_name != 'pull_request'
        shell: bash
        run: echo "BRANCH_NAME=$(echo ${GITHUB_REF#refs/heads/} | tr / -)" >> $GITHUB_ENV

      - name: Get branch name (pull request)
        if: github.event_name == 'pull_request'
        shell: bash
        run: echo "BRANCH_NAME=$(echo ${GITHUB_HEAD_REF} | tr / -)" >> $GITHUB_ENV

      - name: Upload macro benchmark results
        run: |
          cd tools/bench-graph-client
          virtualenv -p python3 ve3
          source ve3/bin/activate
          pip install -r requirements.txt
          ./main.py --benchmark-name "macro_benchmark" \
                    --benchmark-results "../../tests/macro_benchmark/.harness_summary" \
                    --github-run-id "${{ github.run_id }}" \
                    --github-run-number "${{ github.run_number }}" \
                    --head-branch-name "${{ env.BRANCH_NAME }}"

      - name: Run mgbench
        run: |
          cd tests/mgbench
          ./benchmark.py vendor-native --num-workers-for-benchmark 12 --export-results benchmark_pokec.json pokec/medium/*/*

          ./benchmark.py vendor-native --num-workers-for-benchmark 1 --export-results benchmark_supernode.json supernode

          ./benchmark.py vendor-native --num-workers-for-benchmark 1 --export-results benchmark_high_write_set_property.json high_write_set_property

          ./benchmark.py vendor-native --num-workers-for-benchmark 12 --export-results cartesian.json cartesian

      - name: Upload mgbench results
        run: |
          cd tools/bench-graph-client
          virtualenv -p python3 ve3
          source ve3/bin/activate
          pip install -r requirements.txt
          ./main.py --benchmark-name "mgbench" \
                    --benchmark-results "../../tests/mgbench/benchmark_pokec.json" \
                    --github-run-id "${{ github.run_id }}" \
                    --github-run-number "${{ github.run_number }}" \
                    --head-branch-name "${{ env.BRANCH_NAME }}"

          ./main.py --benchmark-name "supernode" \
                    --benchmark-results "../../tests/mgbench/benchmark_supernode.json" \
                    --github-run-id "${{ github.run_id }}" \
                    --github-run-number "${{ github.run_number }}" \
                    --head-branch-name "${{ env.BRANCH_NAME }}"

          ./main.py --benchmark-name "high_write_set_property" \
                    --benchmark-results "../../tests/mgbench/benchmark_high_write_set_property.json" \
                    --github-run-id "${{ github.run_id }}" \
                    --github-run-number "${{ github.run_number }}" \
                    --head-branch-name "${{ env.BRANCH_NAME }}"

          ./main.py --benchmark-name "cartesian" \
                    --benchmark-results "../../tests/mgbench/cartesian.json" \
                    --github-run-id "${{ github.run_id }}" \
                    --github-run-number "${{ github.run_number }}" \
                    --head-branch-name "${{ env.BRANCH_NAME }}"
