# Gathers baseline performance data for the bundled version of Quarto
name: Performance Check
on:
  workflow_dispatch:
  pull_request:
    branches: [main]
  push:
    # only trigger on branches, not on tags
    branches: [main]
  schedule:
    - cron: 0 * * * *

jobs:
  test-bundle:
    runs-on: ubuntu-latest
    if: github.event_name != 'schedule' || (github.event_name == 'schedule' && github.repository == 'quarto-dev/quarto-cli')
    steps:
      - name: Checkout Repo
        uses: actions/checkout@v6
        with:
          # checkout full tree
          fetch-depth: 0

      - name: Set Path
        shell: bash
        run: |
          echo "$HOME/bin" >> $GITHUB_PATH

      - name: Configure Quarto
        uses: ./.github/workflows/actions/quarto-dev

      - name: Test Bundle
        id: create_bundle
        shell: bash
        run: |

          # Do the bundle
          pushd package/src

          bundle_start=$(date +%s.%3N)
          ./quarto-bld prepare-dist --set-version $(cat ../../version.txt)
          bundle_end=$(date +%s.%3N)
          bundle_elapsed=$(printf '%.3f\n' $(echo "scale=3; $bundle_end - $bundle_start" | bc))

          popd

          # move the TS file so quarto command will use bundled JS
          mv src/quarto.ts src/quarto1.ts

          pushd package/pkg-working/bin

          # test a bare quarto command
          q_start=$(date +%s.%3N)
          ./quarto
          q_end=$(date +%s.%3N)
          q_elapsed=$(printf '%.3f\n' $(echo "scale=3; $q_end - $q_start" | bc))

          # test a quarto render
          q_render_start=$(date +%s.%3N)
          ./quarto render ../../../tests/docs/test.qmd --to html
          q_render_end=$(date +%s.%3N)
          q_render_elapsed=$(printf '%.3f\n' $(echo "scale=3; $q_render_end - $q_render_start" | bc))

          popd

          # return the measures
          echo "bundle_time=$bundle_elapsed" >> $GITHUB_OUTPUT
          echo "q_time=$q_elapsed" >> $GITHUB_OUTPUT
          echo "q_render_time=$q_render_elapsed" >> $GITHUB_OUTPUT
          echo "commit=$(git rev-parse --short $GITHUB_SHA)" >> $GITHUB_OUTPUT

      - name: Send Event
        uses: ./.github/workflows/actions/amplitude
        with:
          api_key: ${{ secrets.amplitude_api_key }}
          event: "Bundle"
          success: true
          duration: ${{ steps.create_bundle.outputs.bundle_time }}
          commit: ${{ steps.create_bundle.outputs.commit }}

      - name: Send Event
        uses: ./.github/workflows/actions/amplitude
        with:
          api_key: ${{ secrets.amplitude_api_key }}
          event: "Quarto"
          success: true
          duration: ${{ steps.create_bundle.outputs.q_time }}
          commit: ${{ steps.create_bundle.outputs.commit }}

      - name: Send Event
        uses: ./.github/workflows/actions/amplitude
        with:
          api_key: ${{ secrets.amplitude_api_key }}
          event: "Quarto Render"
          success: true
          duration: ${{ steps.create_bundle.outputs.q_render_time }}
          commit: ${{ steps.create_bundle.outputs.commit }}
