|
| 1 | +name: Performance test |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + container: |
| 7 | + type: string |
| 8 | + description: "The container that the performance tests should run in" |
| 9 | + default: "swift:latest" |
| 10 | + package_path: |
| 11 | + type: string |
| 12 | + description: The directory in the repository that contains a package, which depends on ordo-one/package-benchmark and can run performance measurements. |
| 13 | + default: Benchmarks |
| 14 | + comment_header: |
| 15 | + type: string |
| 16 | + description: | |
| 17 | + If the performance has changed, this text will be prepended to the comment that contains the performance measurements. |
| 18 | + This can be either for performance improvements or regressions. |
| 19 | + default: | |
| 20 | + This PR has changed performance characteristics. Please review that the measurements reported below are expected. If these are improvements, thanks for improving the performance. |
| 21 | +
|
| 22 | +jobs: |
| 23 | + measure_performance: |
| 24 | + name: Measure performance |
| 25 | + runs-on: ubuntu-latest |
| 26 | + container: |
| 27 | + image: ${{ inputs.container }} |
| 28 | + timeout-minutes: 60 |
| 29 | + permissions: |
| 30 | + pull-requests: write |
| 31 | + steps: |
| 32 | + - name: Install libjemalloc-dev |
| 33 | + run: apt-get update && apt-get install -y libjemalloc-dev |
| 34 | + - name: Checkout repository |
| 35 | + uses: actions/checkout@v4 |
| 36 | + with: |
| 37 | + fetch-depth: 0 |
| 38 | + - name: Mark the workspace as safe |
| 39 | + # https://github.com/actions/checkout/issues/766 |
| 40 | + run: git config --global --add safe.directory ${GITHUB_WORKSPACE} |
| 41 | + - name: Measure PR performance |
| 42 | + run: | |
| 43 | + swift package --package-path ${{ inputs.package_path }} --allow-writing-to-directory ${{ inputs.package_path }}/.benchmarkBaselines/ benchmark baseline update "${{ github.head_ref }}" |
| 44 | + - name: Measure base branch performance |
| 45 | + run: | |
| 46 | + git checkout ${{ github.base_ref }} |
| 47 | + swift package --package-path ${{ inputs.package_path }} --allow-writing-to-directory ${{ inputs.package_path }}/.benchmarkBaselines/ benchmark baseline update "${{ github.base_ref }}" |
| 48 | + - name: Compare performance measurements |
| 49 | + id: compare_performance |
| 50 | + run: | |
| 51 | + if ! swift package --package-path ${{ inputs.package_path }} benchmark baseline check "${{ github.base_ref }}" "${{ github.head_ref }}" --format markdown > /tmp/comparison.md 2>/tmp/comparison-stderr.txt; then |
| 52 | + echo "has_significant_changes=true" >> "$GITHUB_OUTPUT" |
| 53 | + else |
| 54 | + echo "has_significant_changes=false" >> "$GITHUB_OUTPUT" |
| 55 | + fi |
| 56 | + - name: Install gh |
| 57 | + if: ${{ steps.compare_performance.outputs.has_significant_changes == 'true' }} |
| 58 | + # Installation instructions from https://github.com/cli/cli/blob/trunk/docs/install_linux.md#debian-ubuntu-linux-raspberry-pi-os-apt |
| 59 | + run: | |
| 60 | + (type -p wget >/dev/null || (apt update && apt-get install wget -y)) |
| 61 | + mkdir -p -m 755 /etc/apt/keyrings |
| 62 | + out=$(mktemp) |
| 63 | + wget -nv -O$out https://cli.github.com/packages/githubcli-archive-keyring.gpg |
| 64 | + cat $out | tee /etc/apt/keyrings/githubcli-archive-keyring.gpg > /dev/null |
| 65 | + chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg |
| 66 | + echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | tee /etc/apt/sources.list.d/github-cli.list > /dev/null |
| 67 | + apt update |
| 68 | + apt install gh -y |
| 69 | + - name: Post comment |
| 70 | + if: ${{ steps.compare_performance.outputs.has_significant_changes == 'true' }} |
| 71 | + env: |
| 72 | + GH_TOKEN: ${{ github.token }} |
| 73 | + run: | |
| 74 | + if grep benchmarkThresholdRegression /tmp/comparison-stderr.txt > /dev/null; then |
| 75 | + PERFORMANCE_CHANGE_MESSAGE="This PR has regressed performance characteristics. Please review whether the changes reported below are expected or if you can do something to improve them." |
| 76 | + elif grep benchmarkThresholdImprovement /tmp/comparison-stderr.txt > /dev/null; then |
| 77 | + PERFORMANCE_CHANGE_MESSAGE="This PR has improved performance characteristics. Thank you 🚀" |
| 78 | + else |
| 79 | + PERFORMANCE_CHANGE_MESSAGE="This PR has changed performance characteristics. Please review that the measurements reported below are expected or if you can do something to improve them." |
| 80 | + fi |
| 81 | +
|
| 82 | + cat > /tmp/performance_change_header.md <<EOF |
| 83 | + $PERFORMANCE_CHANGE_MESSAGE |
| 84 | +
|
| 85 | + <details><summary><b>Performance report</b></summary> |
| 86 | +
|
| 87 | + EOF |
| 88 | +
|
| 89 | + echo "</details>" > /tmp/performance_change_footer.md |
| 90 | +
|
| 91 | + COMMENT="$(cat /tmp/performance_change_header.md /tmp/comparison.md /tmp/performance_change_footer.md)" |
| 92 | + gh pr comment ${{ github.event.number }} --body "$COMMENT" |
0 commit comments