name: Performance Regression Test

on:
  pull_request:
    branches: [main]

jobs:
  benchmark:
    name: Time and memory usage benchmark
    runs-on: ubuntu-latest

    steps:
      # check out pull request branch
      - uses: actions/checkout@v3
        with:
          path: pr
      # check out main branch (to compare performance)
      - uses: actions/checkout@v3
        with:
          ref: main
          path: main
      - uses: actions/setup-node@v3

      - name: Run pull request time benchmark
        run: cd pr && npm install && npm run --silent benchmark-time > benchmarks.txt && cat benchmarks.txt

      - name: Run pull request memory usage benchmark
        run: cd pr && npm run --silent benchmark-memory-usage > memory_usage.txt && cat memory_usage.txt

      - name: Run time benchmark on main (baseline)
        run: cd main && npm install && npm run --silent benchmark-time > benchmarks.txt && cat benchmarks.txt

      - name: Run memory usage benchmark on main (baseline)
        run: cd main && npm run --silent benchmark-memory-usage > memory_usage.txt && cat memory_usage.txt

      - name: Compare time benchmark result
        uses: openpgpjs/github-action-pull-request-benchmark@v1
        with:
          tool: 'benchmarkjs'
          name: 'Time benchmark'
          pr-benchmark-file-path: pr/benchmarks.txt
          base-benchmark-file-path: main/benchmarks.txt
          github-token: ${{ secrets.GITHUB_TOKEN }}
          # trigger alert comment if 1.3 times slower
          alert-threshold: '130%'
          comment-on-alert: false
          # fail workdlow if 1.5 times slower
          fail-threshold: '150%'
          fail-on-alert: true
          file-to-annotate: ${{ github.workspace }}/test/benchmarks/time.js

      - name: Compare memory usage benchmark result
        uses: openpgpjs/github-action-pull-request-benchmark@v1
        with:
          tool: 'raw'
          name: 'Memory usage benchmark'
          pr-benchmark-file-path: pr/memory_usage.txt
          base-benchmark-file-path: main/memory_usage.txt
          github-token: ${{ secrets.GITHUB_TOKEN }}
          alert-threshold: '102%'
          comment-on-alert: false
          fail-threshold: '110%'
          fail-on-alert: true
          file-to-annotate: ${{ github.workspace }}/test/benchmarks/memory_usage.js

