|
| 1 | +# This CI configuration for relative benchmarks is based on the research done |
| 2 | +# for scikit-image's implementation available here: |
| 3 | +# https://github.com/scikit-image/scikit-image/blob/9bdd010a8/.github/workflows/benchmarks.yml#L1 |
| 4 | +# Blog post with the rationale: https://labs.quansight.org/blog/2021/08/github-actions-benchmarks/ |
| 5 | + |
| 6 | +name: Benchmarks |
| 7 | + |
| 8 | +on: |
| 9 | + pull_request: |
| 10 | + types: [labeled] |
| 11 | + schedule: |
| 12 | + - cron: "6 6 * * 0" # every sunday |
| 13 | + workflow_dispatch: |
| 14 | + inputs: |
| 15 | + base_ref: |
| 16 | + description: "Baseline commit or git reference" |
| 17 | + required: true |
| 18 | + contender_ref: |
| 19 | + description: "Contender commit or git reference" |
| 20 | + required: true |
| 21 | + |
| 22 | +# This is the main configuration section that needs to be fine tuned to napari's needs |
| 23 | +# All the *_THREADS options is just to make the benchmarks more robust by not using parallelism |
| 24 | +env: |
| 25 | + OPENBLAS_NUM_THREADS: "1" |
| 26 | + MKL_NUM_THREADS: "1" |
| 27 | + OMP_NUM_THREADS: "1" |
| 28 | + ASV_OPTIONS: "--split --show-stderr --factor 1.5 --attribute timeout=900" |
| 29 | + # --split -> split final reports in tables |
| 30 | + # --show-stderr -> print tracebacks if errors occur |
| 31 | + # --factor 1.5 -> report anomaly if tested timings are beyond 1.5x base timings |
| 32 | + # --attribute timeout=300 -> override timeout attribute (default=60s) to allow slow tests to run |
| 33 | + # see https://asv.readthedocs.io/en/stable/commands.html#asv-continuous for more details! |
| 34 | + |
| 35 | +jobs: |
| 36 | + benchmark: |
| 37 | + if: ${{ github.event.label.name == 'run-benchmarks' && github.event_name == 'pull_request' || github.event_name == 'schedule' || github.event_name == 'workflow_dispatch' }} |
| 38 | + name: ${{ matrix.benchmark-name }} |
| 39 | + runs-on: ${{ matrix.runs-on }} |
| 40 | + permissions: |
| 41 | + contents: read |
| 42 | + issues: write |
| 43 | + strategy: |
| 44 | + fail-fast: false |
| 45 | + matrix: |
| 46 | + include: |
| 47 | + - benchmark-name: Qt |
| 48 | + asv-command: continuous |
| 49 | + selection-regex: "^benchmark_qt_.*" |
| 50 | + runs-on: macos-latest |
| 51 | + # Qt tests run on macOS to avoid using Xvfb business |
| 52 | + # xvfb makes everything run, but some tests segfault :shrug: |
| 53 | + # Fortunately, macOS graphics stack does not need xvfb! |
| 54 | + - benchmark-name: non-Qt |
| 55 | + asv-command: continuous |
| 56 | + selection-regex: "^benchmark_(?!qt_).*" |
| 57 | + runs-on: ubuntu-latest |
| 58 | + |
| 59 | + steps: |
| 60 | + # We need the full repo to avoid this issue |
| 61 | + # https://github.com/actions/checkout/issues/23 |
| 62 | + - uses: actions/checkout@v4 |
| 63 | + with: |
| 64 | + fetch-depth: 0 |
| 65 | + |
| 66 | + - uses: actions/setup-python@v5 |
| 67 | + name: Install Python |
| 68 | + with: |
| 69 | + python-version: "3.11" |
| 70 | + cache-dependency-path: pyproject.toml |
| 71 | + |
| 72 | + - uses: tlambert03/setup-qt-libs@v1 |
| 73 | + |
| 74 | + - name: Setup asv |
| 75 | + run: python -m pip install "asv[virtualenv]" |
| 76 | + env: |
| 77 | + PIP_CONSTRAINT: ${{ github.workspace }}/benchmarks/benchmark.txt |
| 78 | + |
| 79 | + - uses: octokit/request-action@v2.x |
| 80 | + id: latest_release |
| 81 | + with: |
| 82 | + route: GET /repos/{owner}/{repo}/releases/latest |
| 83 | + owner: scverse |
| 84 | + repo: napari-spatialdata |
| 85 | + env: |
| 86 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 87 | + |
| 88 | + - name: Run ${{ matrix.benchmark-name }} benchmarks |
| 89 | + id: run_benchmark |
| 90 | + env: |
| 91 | + # asv will checkout commits, which might contain LFS artifacts; ignore those errors since |
| 92 | + # they are probably just documentation PNGs not needed here anyway |
| 93 | + GIT_LFS_SKIP_SMUDGE: 1 |
| 94 | + HEAD_LABEL: ${{ github.event.pull_request.head.label }} |
| 95 | + PIP_CONSTRAINT: ${{ github.workspace }}/benchmarks/benchmark.txt |
| 96 | + run: | |
| 97 | + set -euxo pipefail |
| 98 | + read -ra cmd_options <<< "$ASV_OPTIONS" |
| 99 | +
|
| 100 | + # ID this runner |
| 101 | + asv machine --yes |
| 102 | +
|
| 103 | + if [[ $GITHUB_EVENT_NAME == pull_request ]]; then |
| 104 | + EVENT_NAME="PR #${{ github.event.pull_request.number }}" |
| 105 | + BASE_REF=${{ github.event.pull_request.base.sha }} |
| 106 | + CONTENDER_REF=${GITHUB_SHA} |
| 107 | + echo "Baseline: ${BASE_REF} (${{ github.event.pull_request.base.label }})" |
| 108 | + echo "Contender: ${CONTENDER_REF} ($HEAD_LABEL)" |
| 109 | + elif [[ $GITHUB_EVENT_NAME == schedule ]]; then |
| 110 | + EVENT_NAME="cronjob" |
| 111 | + BASE_REF="${{ fromJSON(steps.latest_release.outputs.data).target_commitish }}" |
| 112 | + CONTENDER_REF="${GITHUB_SHA}" |
| 113 | + echo "Baseline: ${BASE_REF} (${{ fromJSON(steps.latest_release.outputs.data).tag_name }})" |
| 114 | + echo "Contender: ${CONTENDER_REF} (current main)" |
| 115 | + elif [[ $GITHUB_EVENT_NAME == workflow_dispatch ]]; then |
| 116 | + EVENT_NAME="manual trigger" |
| 117 | + BASE_REF="${{ github.event.inputs.base_ref }}" |
| 118 | + CONTENDER_REF="${{ github.event.inputs.contender_ref }}" |
| 119 | + echo "Baseline: ${BASE_REF} (workflow input)" |
| 120 | + echo "Contender: ${CONTENDER_REF} (workflow input)" |
| 121 | + fi |
| 122 | +
|
| 123 | + echo "EVENT_NAME=$EVENT_NAME" >> "$GITHUB_ENV" |
| 124 | + echo "BASE_REF=$BASE_REF" >> "$GITHUB_ENV" |
| 125 | + echo "CONTENDER_REF=$CONTENDER_REF" >> "$GITHUB_ENV" |
| 126 | +
|
| 127 | + # Run benchmarks for current commit against base |
| 128 | + asv continuous "${cmd_options[@]}" -b "${{ matrix.selection-regex }}" "${BASE_REF}" "${CONTENDER_REF}" \ |
| 129 | + | sed -E "/Traceback | failed$|PERFORMANCE DECREASED/ s/^/::error:: /" \ |
| 130 | + | tee asv_continuous.log |
| 131 | +
|
| 132 | + # Report and export results for subsequent steps |
| 133 | + if grep "Traceback \|failed\|PERFORMANCE DECREASED" asv_continuous.log > /dev/null ; then |
| 134 | + exit 1 |
| 135 | + fi |
| 136 | +
|
| 137 | + - name: Report Failures as Issue |
| 138 | + if: ${{ (github.event_name == 'schedule' || github.event_name == 'workflow_dispatch') && failure() }} |
| 139 | + uses: JasonEtco/create-an-issue@v2 |
| 140 | + env: |
| 141 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 142 | + PLATFORM: ${{ matrix.runs-on }} |
| 143 | + PYTHON: "3.9" |
| 144 | + BACKEND: ${{ matrix.benchmark-name }} |
| 145 | + RUN_ID: ${{ github.run_id }} |
| 146 | + TITLE: "[test-bot] Benchmark tests failing" |
| 147 | + with: |
| 148 | + filename: .github/BENCHMARK_FAIL_TEMPLATE.md |
| 149 | + update_existing: true |
| 150 | + |
| 151 | + - name: Add more info to artifact |
| 152 | + if: always() |
| 153 | + run: | |
| 154 | + # Copy the full `asv continuous` log |
| 155 | + cp asv_continuous.log .asv/results/asv_continuous_${{ matrix.benchmark-name }}.log |
| 156 | + # ensure that even if this isn't a PR, the benchmark_report workflow can run without error |
| 157 | + touch .asv/results/message_${{ matrix.benchmark-name }}.txt |
| 158 | +
|
| 159 | + # Add the message that might be posted as a comment on the PR |
| 160 | + # We delegate the actual comment to `benchmarks_report.yml` due to |
| 161 | + # potential token permissions issues |
| 162 | + if [[ $GITHUB_EVENT_NAME == pull_request ]]; then |
| 163 | +
|
| 164 | + echo "${{ github.event.pull_request.number }}" > .asv/results/pr_number |
| 165 | + echo \ |
| 166 | + "The ${{ matrix.benchmark-name }} benchmark run requested by $EVENT_NAME ($CONTENDER_REF vs $BASE_REF) has" \ |
| 167 | + "finished with status '${{ steps.run_benchmark.outcome }}'. See the" \ |
| 168 | + "[CI logs and artifacts](||BENCHMARK_CI_LOGS_URL||) for further details." \ |
| 169 | + > .asv/results/message_${{ matrix.benchmark-name }}.txt |
| 170 | +
|
| 171 | + fi |
| 172 | +
|
| 173 | + - uses: actions/upload-artifact@v4 |
| 174 | + if: always() |
| 175 | + with: |
| 176 | + name: asv-benchmark-results-${{ github.run_id }}-${{ github.run_number }}-${{ github.run_attempt }}-${{ matrix.benchmark-name }} |
| 177 | + path: .asv/results |
| 178 | + |
| 179 | + combine-artifacts: |
| 180 | + runs-on: ubuntu-latest |
| 181 | + needs: benchmark |
| 182 | + if: always() |
| 183 | + steps: |
| 184 | + - name: Download artifact |
| 185 | + uses: actions/download-artifact@v4 |
| 186 | + with: |
| 187 | + pattern: asv-benchmark-results* |
| 188 | + path: asv_result |
| 189 | + merge-multiple: true |
| 190 | + - name: Upload artifact |
| 191 | + uses: actions/upload-artifact@v4 |
| 192 | + with: |
| 193 | + name: asv-benchmark-results-${{ github.run_id }}-${{ github.run_number }}-${{ github.run_attempt }} |
| 194 | + path: asv_result |
0 commit comments