name: Benchmark for extraction algorithm
on:
  push:
    branches:
      - main
permissions:
  contents: write
  deployments: write

jobs:
  benchmark-inband:
    name: Benchmark for extraction algorithm (inband)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Use Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '20.x'
      - run: npm ci
      - run: npm run build --if-present
      - run: node ./perf/perftest-inband.js | tee output.txt
      
      - name: Store benchmark result
        uses: benchmark-action/github-action-benchmark@v1
        with:
            name: Extraction algorithm Benchmark for inband data
            tool: 'benchmarkjs'
            output-file-path: output.txt
            github-token: ${{ secrets.GITHUB_TOKEN }}
            benchmark-data-dir-path: docs/
            auto-push: true
            # Show alert with commit comment on detecting possible performance regression
            alert-threshold: '200%'
            comment-on-alert: true
            fail-on-alert: true
            alert-comment-cc-users: '@xdxxxdx'
  

  benchmark-outband:
    name: Benchmark for extraction algorithm (outband)
    runs-on: ubuntu-latest
    #run in sequence after benchmark-inband`
    needs: benchmark-inband
    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Use Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '20.x'

      - name: Build npm
        run: |
          npm ci
          npm run build --if-present

      - name: Publish data
        run: |
          npm run data &
          disown  # Run npm run data in the background and disown the process

      - name: Verify server
        run:  |
          response_code=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:8080/member-1.ttl)

          if [ "$response_code" -eq 200 ]; then
            echo "Server verification successful (HTTP 200)"
          else
            echo "Server verification failed with HTTP $response_code"
            exit 1  # Exit the script with an error code
          fi
      - name: Run Benchmark
        run: |
            node ./perf/perftest-outband.js | tee output.txt

      - name: Store benchmark result
        uses: benchmark-action/github-action-benchmark@v1
        with:
            name: Extraction algorithm Benchmark for outband data
            tool: 'benchmarkjs'
            output-file-path: output.txt
            github-token: ${{ secrets.GITHUB_TOKEN }}
            benchmark-data-dir-path: docs/
            auto-push: true
            # Show alert with commit comment on detecting possible performance regression
            alert-threshold: '200%'
            comment-on-alert: true
            fail-on-alert: true
            alert-comment-cc-users: '@xdxxxdx'
  

