name: Benchmark

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]
  # `workflow_dispatch` allows CodSpeed to trigger
  workflow_dispatch:

permissions: {}

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  build:
    name: Build
    runs-on: ubuntu-24.04
    permissions:
      contents: read # clone repo
    steps:
      - name: Checkout
        uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
        with:
          submodules: true
          persist-credentials: false

      - name: Setup Deno
        uses: denoland/setup-deno@22d081ff2d3a40755e97629de92e3bcbfa7cf2ed # v2.0.5
        with:
          deno-version: v2.x

      - name: Build WASM
        run: make

      - name: Cache dependencies
        uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
        id: deno-cache
        with:
          path: node_modules
          key: deno-${{ hashFiles('deno.lock') }}

      - name: Install dependencies
        if: steps.deno-cache.outputs.cache-hit != 'true'
        run: deno install

      - name: Upload build artifact
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: bench-build
          retention-days: 1
          path: wasm.ts

  benchmarks:
    name: Run benchmarks (walltime)
    needs: build
    runs-on: codspeed-macro
    permissions:
      contents: read # clone repo
      id-token: write # upload benchmark results to codspeed

    steps:
      - name: Checkout
        uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0

      - name: Setup Deno
        uses: denoland/setup-deno@22d081ff2d3a40755e97629de92e3bcbfa7cf2ed # v2.0.5
        with:
          deno-version: v2.x

      - name: Setup Node
        uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
        with:
          node-version: 24

      - name: Download build artifact
        uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
        with:
          name: bench-build

      - name: Restore dependencies
        uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
        id: deno-cache
        with:
          path: node_modules
          key: deno-codspeed-${{ runner.arch }}-${{ hashFiles('deno.lock') }}

      - name: Install dependencies
        if: steps.deno-cache.outputs.cache-hit != 'true'
        run: deno install

      - name: Run walltime benchmarks
        uses: CodSpeedHQ/action@4e969336ab9acd4f6f8d025fdd793292b0835df0 # v4.18.2
        with:
          mode: walltime
          run: |
            node bench/startup.bench.ts
            node bench/throughput.bench.ts
            node bench/render.bench.ts
            node bench/ops.bench.ts
