name: Benchmarking pypdf
on:
  push:
    branches:
      - main

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

permissions: {}

jobs:
  benchmark:
    name: "Benchmark ${{ matrix.name }}"
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: ['3.x']
        include:
          - python-version: '3.x'
            name: 'CPython'
          - python-version: 'pypy3.11'
            name: 'PyPy 3.11'
    permissions:
      contents: write  # Write the results to the branch.
      deployments: write  # Trigger GitHub pages deployment.
    steps:
    - name: Checkout Code
      uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10  # v6.0.3
      with:
        submodules: 'recursive'
        persist-credentials: false
    - name: Setup Python
      uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405  # v6.2.0
      with:
        python-version: ${{ matrix.python-version }}
    - name: Install requirements
      run: |
        pip install -r requirements/ci-3.11.txt
    - name: Install pypdf
      run: |
        pip install .
    - name: Run benchmark
      run: |
        pytest tests/bench.py --benchmark-json output.json
    - name: Store benchmark result
      uses: benchmark-action/github-action-benchmark@52576c92bccf6ac60c8223ec7eb2565637cae9ba  # v1.22.1
      with:
        name: "${{ matrix.name }} Benchmark"
        tool: 'pytest'
        output-file-path: output.json
        # Use personal access token instead of GITHUB_TOKEN due to https://github.community/t/github-action-not-triggering-gh-pages-upon-push/16096
        github-token: ${{ secrets.GITHUB_TOKEN }}
        auto-push: true
        # Show alert with commit comment on detecting possible performance regression
        alert-threshold: '200%'
        comment-on-alert: true
        fail-on-alert: true
