name: Performance Tests

on:
  workflow_dispatch:

jobs:
  build-and-test-performance:
    name: Performance Tests

    runs-on: ubuntu-22.04
    timeout-minutes: 30

    steps:
      - name: Checkout
        uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0

      - name: Use Node.js 22.x
        uses: actions/setup-node@1a4442cacd436585916779262731d5b162bc6ec7 # v3.8.2
        with:
          node-version: "22.x"
          registry-url: "https://registry.npmjs.org"

      - name: Use Python 3.13
        uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
        with:
          python-version: '3.13'

      - name: Build
        shell: bash
        run: |
          npm install -g  node-gyp
          npm ci
          npm run build
        env:
          NODE_OPTIONS: --max_old_space_size=4096
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # https://github.com/microsoft/vscode-ripgrep/issues/9

      - name: Performance (browser)
        shell: bash
        run: npm run performance:startup:browser

      - name: Performance (Electron)
        shell: bash
        run: xvfb-run npm run performance:startup:electron

      - name: Analyze performance results
        uses: benchmark-action/github-action-benchmark@fd31771ce86cc65eab85653da103f71ab1b4479c # v1.9.0
        with:
          name: Performance Benchmarks
          tool: "customSmallerIsBetter"
          output-file-path: performance-result.json
          alert-threshold: "150%"
          fail-on-alert: false
          github-token: ${{ secrets.GITHUB_TOKEN }} # Needed for comments an GH Pages
          benchmark-data-dir-path: tests/performance
          auto-push: true # Push to GH Pages
          comment-on-alert: true # Comment on commit if it causes a performance regression
          max-items-in-chart: 100 # Don't just collect results forever
