on:
  schedule:
  - cron: '0 2-23/4 * * *'
  workflow_dispatch:
    inputs:
      sendReport:
        description: 'Whether to send the stats to the Datadog dashboards'
        required: true
        default: 'false'
      hyperfineOptions:
        description: 'Options passed to Hyperfine'
        required: false
        default: ''

name: 'Performance tests'
jobs:
  benchmark:
    strategy:
      fail-fast: false
      matrix:
        benchmark:
          - [Next.js, next]
          - [Gatsby, gatsby]
        packageManager:
          - classic
          - yarn
          - yarn-nm
          - yarn-pnpm
          - npm
          - pnpm

    name: 'Benchmarking ${{matrix.benchmark[0]}} w/ ${{matrix.packageManager}}'
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3

    - uses: ./.github/actions/prepare
      with:
        minify-bundle: true

    - name: 'Install Hyperfine'
      run: |
        wget https://github.com/sharkdp/hyperfine/releases/download/v1.10.0/hyperfine_1.10.0_amd64.deb
        sudo dpkg -i hyperfine_1.10.0_amd64.deb

    - name: 'Install the package manager'
      run: |
        case ${{matrix.packageManager}} in
          npm)
            npm install -g npm@^7;;
          pnpm)
            npm install -g pnpm;;
          classic)
            npm install -g yarn;;
        esac

    - name: 'Creates a temporary directory'
      run: |
        BENCH_DIR=$(mktemp -d)
        echo "BENCH_DIR=$BENCH_DIR" >> "$GITHUB_ENV"

    - name: 'Run the performance test'
      run: |
        bash scripts/bench-run.sh '${{matrix.packageManager}}' '${{matrix.benchmark[1]}}' "$BENCH_DIR"

        if [[ '${{github.event.inputs.sendReport}}' != 'false' ]]; then
          yarn node scripts/submit-bench-data.js '${{matrix.packageManager}}' '${{matrix.benchmark[1]}}' "$BENCH_DIR"
        fi
      env:
        DD_API_KEY: ${{secrets.DD_API_KEY}}
        HYPERFINE_OPTIONS: ${{github.event.inputs.hyperfineOptions}}
