|
| 1 | +name: benchmark |
| 2 | +on: |
| 3 | + workflow_dispatch: |
| 4 | + pull_request_review: |
| 5 | + types: [submitted] |
| 6 | + pull_request: |
| 7 | + branches: [main] |
| 8 | + types: [synchronize] |
| 9 | + paths: |
| 10 | + - Sources/*.swift |
| 11 | + - Benchmarks/ |
| 12 | + - .github/workflows/benchmark.yml |
| 13 | + |
| 14 | +jobs: |
| 15 | + benchmark-vs-thresholds: |
| 16 | + # Run the job only if it's a manual workflow dispatch, or if this event is a pull-request approval event, or if someone has rerun the job. |
| 17 | + if: github.event_name == 'workflow_dispatch' || github.event.review.state == 'approved' || github.run_attempt > 1 |
| 18 | + |
| 19 | + # https://runs-on.com/features/custom-runners/ |
| 20 | + runs-on: |
| 21 | + labels: |
| 22 | + - runs-on |
| 23 | + - runner=2cpu-4ram |
| 24 | + - run-id=${{ github.run_id }} |
| 25 | + |
| 26 | + container: swift:noble |
| 27 | + |
| 28 | + defaults: |
| 29 | + run: |
| 30 | + shell: bash |
| 31 | + |
| 32 | + env: |
| 33 | + PR_COMMENT: null # will be populated later |
| 34 | + |
| 35 | + steps: |
| 36 | + - name: Configure RunsOn |
| 37 | + uses: runs-on/action@v1 |
| 38 | + |
| 39 | + - name: Check out code |
| 40 | + uses: actions/checkout@v4 |
| 41 | + with: |
| 42 | + fetch-depth: 0 |
| 43 | + |
| 44 | + - name: Configure git |
| 45 | + run: git config --global --add safe.directory "${GITHUB_WORKSPACE}" |
| 46 | + |
| 47 | + # jemalloc is a dependency of the Benchmarking package |
| 48 | + # actions/cache will detect zstd and will become much faster. |
| 49 | + - name: Install jemalloc, curl, jq and zstd |
| 50 | + run: | |
| 51 | + set -eu |
| 52 | +
|
| 53 | + apt-get update -y |
| 54 | + apt-get install -y libjemalloc-dev curl jq zstd |
| 55 | +
|
| 56 | + - name: Restore .build |
| 57 | + id: restore-cache |
| 58 | + uses: actions/cache/restore@v4 |
| 59 | + with: |
| 60 | + path: Benchmarks/.build |
| 61 | + key: "swiftpm-benchmark-build-${{ runner.os }}-${{ github.event.pull_request.base.sha || github.event.after }}" |
| 62 | + restore-keys: "swiftpm-benchmark-build-${{ runner.os }}-" |
| 63 | + |
| 64 | + - name: Run benchmarks for branch '${{ github.head_ref || github.ref_name }}' |
| 65 | + run: | |
| 66 | + swift package -c release --disable-sandbox \ |
| 67 | + --package-path Benchmarks \ |
| 68 | + benchmark baseline update \ |
| 69 | + '${{ github.head_ref || github.ref_name }}' |
| 70 | +
|
| 71 | + - name: Read benchmark result |
| 72 | + id: read-benchmark |
| 73 | + run: | |
| 74 | + set -eu |
| 75 | +
|
| 76 | + swift package -c release --disable-sandbox \ |
| 77 | + --package-path Benchmarks \ |
| 78 | + benchmark baseline read \ |
| 79 | + '${{ github.head_ref || github.ref_name }}' \ |
| 80 | + --no-progress \ |
| 81 | + --format markdown \ |
| 82 | + >> result.text |
| 83 | +
|
| 84 | + # Read the result to the output of the step |
| 85 | + echo 'result<<EOF' >> "${GITHUB_OUTPUT}" |
| 86 | + cat result.text >> "${GITHUB_OUTPUT}" |
| 87 | + echo 'EOF' >> "${GITHUB_OUTPUT}" |
| 88 | +
|
| 89 | + - name: Compare branch '${{ github.head_ref || github.ref_name }}' against thresholds |
| 90 | + id: compare-benchmark |
| 91 | + run: | |
| 92 | + set -eu |
| 93 | +
|
| 94 | + TIMESTAMP=$(date -u +"%Y-%m-%d %H:%M:%S UTC") |
| 95 | + ENCODED_TIMESTAMP=$(date -u +"%Y-%m-%dT%H%%3A%M%%3A%SZ") |
| 96 | + TIMESTAMP_LINK="https://www.timeanddate.com/worldclock/fixedtime.html?iso=$ENCODED_TIMESTAMP" |
| 97 | + echo "## Benchmark check running at [$TIMESTAMP]($TIMESTAMP_LINK)" >> summary.text |
| 98 | +
|
| 99 | + # Disable 'set -e' to prevent the script from exiting on non-zero exit codes |
| 100 | + set +e |
| 101 | + swift package -c release --disable-sandbox \ |
| 102 | + --package-path Benchmarks \ |
| 103 | + benchmark thresholds check \ |
| 104 | + '${{ github.head_ref || github.ref_name }}' \ |
| 105 | + --path "$PWD/Benchmarks/Thresholds/" \ |
| 106 | + --no-progress \ |
| 107 | + --format markdown \ |
| 108 | + >> summary.text |
| 109 | + echo "exit-status=$?" >> "${GITHUB_OUTPUT}" |
| 110 | + set -e |
| 111 | +
|
| 112 | + echo 'summary<<EOF' >> "${GITHUB_OUTPUT}" |
| 113 | + cat summary.text >> "${GITHUB_OUTPUT}" |
| 114 | + echo 'EOF' >> "${GITHUB_OUTPUT}" |
| 115 | +
|
| 116 | + - name: Cache .build |
| 117 | + if: steps.restore-cache.outputs.cache-hit != 'true' |
| 118 | + uses: actions/cache/save@v4 |
| 119 | + with: |
| 120 | + path: Benchmarks/.build |
| 121 | + key: "swiftpm-benchmark-build-${{ runner.os }}-${{ github.event.pull_request.base.sha || github.event.after }}" |
| 122 | + |
| 123 | + - name: Construct comment |
| 124 | + run: | |
| 125 | + set -eu |
| 126 | +
|
| 127 | + EXIT_CODE='${{ steps.compare-benchmark.outputs.exit-status }}' |
| 128 | +
|
| 129 | + echo 'PR_COMMENT<<EOF' >> "${GITHUB_ENV}" |
| 130 | +
|
| 131 | + echo '## [Benchmark](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}) Report' >> "${GITHUB_ENV}" |
| 132 | +
|
| 133 | + case "${EXIT_CODE}" in |
| 134 | + 0) |
| 135 | + echo '**✅ Pull request has no significant performance differences ✅**' >> "${GITHUB_ENV}" |
| 136 | + ;; |
| 137 | + 1) |
| 138 | + echo '**❌ Pull request has significant performance differences 📊**' >> "${GITHUB_ENV}" |
| 139 | + ;; |
| 140 | + 2) |
| 141 | + echo '**❌ Pull request has significant performance regressions 📉**' >> "${GITHUB_ENV}" |
| 142 | + ;; |
| 143 | + 4) |
| 144 | + echo '**❌ Pull request has significant performance improvements 📈**' >> "${GITHUB_ENV}" |
| 145 | + ;; |
| 146 | + *) |
| 147 | + echo '**❌ Benchmark comparison failed to complete properly with exit code $EXIT_CODE ❌**' >> "${GITHUB_ENV}" |
| 148 | + ;; |
| 149 | + esac |
| 150 | +
|
| 151 | + echo '<details>' >> "${GITHUB_ENV}" |
| 152 | + echo ' <summary> Click to expand comparison result </summary>' >> "${GITHUB_ENV}" |
| 153 | + echo '' >> "${GITHUB_ENV}" |
| 154 | + echo '${{ steps.compare-benchmark.outputs.summary }}' >> "${GITHUB_ENV}" |
| 155 | + echo '' >> "${GITHUB_ENV}" |
| 156 | + echo '</details>' >> "${GITHUB_ENV}" |
| 157 | +
|
| 158 | + echo '' >> "${GITHUB_ENV}" |
| 159 | +
|
| 160 | + echo '<details>' >> "${GITHUB_ENV}" |
| 161 | + echo ' <summary> Click to expand benchmark result </summary>' >> "${GITHUB_ENV}" |
| 162 | + echo '' >> "${GITHUB_ENV}" |
| 163 | + echo '${{ steps.read-benchmark.outputs.result }}' >> "${GITHUB_ENV}" |
| 164 | + echo '' >> "${GITHUB_ENV}" |
| 165 | + echo '</details>' >> "${GITHUB_ENV}" |
| 166 | +
|
| 167 | + echo 'EOF' >> "${GITHUB_ENV}" |
| 168 | +
|
| 169 | + - name: Output the comment as job summary |
| 170 | + run: echo '${{ env.PR_COMMENT }}' >> "${GITHUB_STEP_SUMMARY}" |
| 171 | + |
| 172 | + - name: Comment in PR |
| 173 | + if: startsWith(github.event_name, 'pull_request') |
| 174 | + uses: thollander/actions-comment-pull-request@v3 |
| 175 | + with: |
| 176 | + message: ${{ env.PR_COMMENT }} |
| 177 | + comment-tag: benchmark-ci-comment |
| 178 | + |
| 179 | + - name: Exit with correct status |
| 180 | + run: | |
| 181 | + EXIT_CODE='${{ steps.compare-benchmark.outputs.exit-status }}' |
| 182 | + echo "Previous exit code was: $EXIT_CODE" |
| 183 | + exit $EXIT_CODE |
0 commit comments