Skip to content

Commit

Permalink
Run tests in parallel (#552)
Browse files Browse the repository at this point in the history
Run tests in parallel workflows using pytest extension pytest-split.
Set the number of parallel workflows using matrix: test_group_id: [1,2,3,...]
Set pytest number of groups and group id
pytest --splits 2 --group ${{ matrix.test_group_id }}

We should periodically run all tests on one machine and commit generated files containing durations.
pytest-split extension will use this file in its splitting algorithm.

pytest --store-durations

Other changes:
Adjust job_id detection for multiple jobs
Adjust log collection and artifacts upload
  • Loading branch information
vmilosevic authored Oct 31, 2024
1 parent 28b3096 commit ac1ceef
Show file tree
Hide file tree
Showing 2 changed files with 1,157 additions and 6 deletions.
21 changes: 15 additions & 6 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ jobs:
matrix:
build:
- runs-on: runner
test_group_id: [1,2]

runs-on:
- in-service
Expand All @@ -72,14 +73,17 @@ jobs:
- name: Set reusable strings
id: strings
shell: bash
env:
job-name: "${{ github.job }} (${{ matrix.build.runs-on }}, ${{ matrix.test_group_id }})"
run: |
echo "work-dir=$(pwd)" >> "$GITHUB_OUTPUT"
echo "build-output-dir=$(pwd)/build" >> "$GITHUB_OUTPUT"
# Github job context unfortunately doesn't contain job_id, this is the workaround how to fetch it using GH API
JOB_ID=$(curl -s -H "Authorization: token ${{ github.token }}" \
"https://api.github.com/repos/${{ github.repository }}/actions/runs/${{ github.run_id }}/jobs" | \
jq -r '.jobs[] | select(.name | contains("${{ github.job }}")) | .id')
echo "Expected job name: ${{ env.job-name }}"
JOB_ID=$(curl -s -H "Authorization: token ${{ secrets.GH_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/actions/runs/${{ github.run_id }}/attempts/${{ github.run_attempt }}/jobs" | \
jq -r '.jobs[] | select(.name | contains("${{ env.job-name }}")) | .id ')
echo "Current job id: $JOB_ID"
echo "job-id=$JOB_ID" >> "$GITHUB_OUTPUT"
Expand Down Expand Up @@ -130,13 +134,18 @@ jobs:
run: |
source env/activate
set -o pipefail # Ensures that the exit code reflects the first command that fails
pytest --junit-xml=${{ steps.strings.outputs.test_report_path }} 2>&1 | tee pytest.log
pip install pytest-split
pytest --splits 2 \
--group ${{ matrix.test_group_id }} \
--splitting-algorithm least_duration \
--junit-xml=${{ steps.strings.outputs.test_report_path }} \
2>&1 | tee pytest.log
- name: Upload Test Log
uses: actions/upload-artifact@v4
if: success() || failure()
with:
name: test-log-${{ matrix.build.runs-on }}
name: test-log-${{ matrix.build.runs-on }}-${{ matrix.test_group_id }}
path: pytest.log

- name: Run Perf Benchmark
Expand All @@ -149,7 +158,7 @@ jobs:
uses: actions/upload-artifact@v4
if: success() || failure()
with:
name: test-reports-${{ matrix.build.runs-on }}
name: test-reports-${{ matrix.build.runs-on }}-${{ matrix.test_group_id }}
path: ${{ steps.strings.outputs.test_report_path }}

- name: Show Test Report
Expand Down
Loading

0 comments on commit ac1ceef

Please sign in to comment.