name: benchmarks

on:
  push:
    branches:
      - 'main'
  pull_request:
    paths-ignore:
      - 'website/**'

jobs:
  server:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        scenario:
          - native
          - ponyfill
          - undici
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4
      - name: Install Required Libraries
        run: sudo apt update && sudo apt install -y libcurl4-openssl-dev libssl-dev
      - name: Setup env
        uses: the-guild-org/shared-config/setup@main
        with:
          nodeVersion: 23
          packageManager: yarn
      - name: Build Packages
        run: yarn build
      - name: Setup K6
        run: |
          wget https://github.com/grafana/k6/releases/download/v0.37.0/k6-v0.37.0-linux-amd64.deb
          sudo apt-get update
          sudo apt-get install ./k6-v0.37.0-linux-amd64.deb
      - name: Start Benchmark
        working-directory: ./benchmarks/server
        run: |
          yarn test
        env:
          SCENARIO: ${{ matrix.scenario }}
          NODE_NO_WARNINGS: true
          NODE_ENV: production
          GITHUB_PR: ${{ github.event.number }}
          GITHUB_SHA: ${{ github.sha }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  node-fetch:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        scenario:
          - noConsumeBody
          - consumeBody
    services:
      httpbin:
        image: mccutchen/go-httpbin
        env:
          PORT: 50000
        ports:
          - 50000:50000
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4
      - name: Setup env
        uses: the-guild-org/shared-config/setup@main
        with:
          nodeVersion: 22
          packageManager: yarn
      - name: Setup K6
        run: |
          wget https://github.com/grafana/k6/releases/download/v0.37.0/k6-v0.37.0-linux-amd64.deb
          sudo apt-get update
          sudo apt-get install ./k6-v0.37.0-linux-amd64.deb
      - name: Start server
        run: yarn workspace @benchmarks/node-fetch run start:server &
      - name: Wait for server
        run: curl --retry 5 --retry-delay 1 --retry-connrefused http://localhost:50001
      - name: Benchmark
        env:
          SCENARIO: ${{ matrix.scenario }}
          GITHUB_PR: ${{ github.event.number }}
          GITHUB_SHA: ${{ github.sha }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: k6 run ./benchmarks/node-fetch/k6.js