| 1 |
name: benchmark |
| 2 |
|
| 3 |
"on": |
| 4 |
push: |
| 5 |
branches: |
| 6 |
- main |
| 7 |
- dev |
| 8 |
- deps |
| 9 |
pull_request: |
| 10 |
branches: |
| 11 |
- main |
| 12 |
workflow_dispatch: |
| 13 |
inputs: |
| 14 |
ref: |
| 15 |
description: (optional) ref to benchmark |
| 16 |
|
| 17 |
env: |
| 18 |
FORCE_COLOR: "1" |
| 19 |
PYTHONHASHSEED: "42" |
| 20 |
BASELINE_RELEASE_TAG: microbenchmarks |
| 21 |
# Cachegrind counts instructions rather than measuring time, so comparing them is |
| 22 |
# reproducible and can be held to a tight bound. A change of more than this much in |
| 23 |
# either direction is called out. |
| 24 |
# |
| 25 |
# There is deliberately no wall-clock threshold. The timings pytest-benchmark reports |
| 26 |
# here are measured *under* Cachegrind, and vary far more between runs of identical code |
| 27 |
# than any change worth catching: revisions whose bidict/ was byte-identical have been |
| 28 |
# seen to differ by tens of percent, in both directions. They are still compared in the |
| 29 |
# run output, which is archived below for eyeballing, but nothing keys off them. |
| 30 |
INSTRUCTION_CHANGE_PCT: "1.0" |
| 31 |
|
| 32 |
jobs: |
| 33 |
benchmark: |
| 34 |
runs-on: ubuntu-24.04 |
| 35 |
steps: |
| 36 |
- name: install valgrind |
| 37 |
uses: awalsh128/cache-apt-pkgs-action@553a35bb8ebd9fcabcb1c9451aa4c98e1b4ca8a9 |
| 38 |
with: |
| 39 |
packages: valgrind |
| 40 |
- name: check out source |
| 41 |
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 |
| 42 |
with: |
| 43 |
fetch-depth: 0 |
| 44 |
- name: install Nix |
| 45 |
uses: DeterminateSystems/nix-installer-action@ef8a148080ab6020fd15196c2084a2eea5ff2d25 |
| 46 |
- name: resolve benchmark metadata |
| 47 |
id: metadata |
| 48 |
run: | |
| 49 |
mapfile -t python_info < <(nix develop .#benchmark --command bash -c "python - <<'PY' |
| 50 |
import platform |
| 51 |
print(platform.python_version()) |
| 52 |
print(platform.python_implementation()) |
| 53 |
PY") |
| 54 |
python_version=${python_info[0]} |
| 55 |
python_impl=${python_info[1]} |
| 56 |
python_series=${python_version%.*} |
| 57 |
arch=$(uname -m) |
| 58 |
baseline_asset_name="GHA-linux-cachegrind-${arch}-${python_impl}-${python_series}-baseline.json" |
| 59 |
baseline_url="https://github.com/${GITHUB_REPOSITORY}/releases/download/${BASELINE_RELEASE_TAG}/${baseline_asset_name}" |
| 60 |
instr_asset_name="GHA-linux-cachegrind-${arch}-${python_impl}-${python_series}-instructions.txt" |
| 61 |
instr_url="https://github.com/${GITHUB_REPOSITORY}/releases/download/${BASELINE_RELEASE_TAG}/${instr_asset_name}" |
| 62 |
{ |
| 63 |
echo "python_version=$python_version" |
| 64 |
echo "python_series=$python_series" |
| 65 |
echo "baseline_asset_name=$baseline_asset_name" |
| 66 |
echo "baseline_url=$baseline_url" |
| 67 |
echo "instr_asset_name=$instr_asset_name" |
| 68 |
echo "instr_url=$instr_url" |
| 69 |
} >> "$GITHUB_OUTPUT" |
| 70 |
- name: benchmark and compare to baseline |
| 71 |
id: benchmark |
| 72 |
run: | |
| 73 |
set +e |
| 74 |
nix develop .#benchmark --command bash -c ' |
| 75 |
set -euo pipefail |
| 76 |
git checkout ${{ github.event.inputs.ref || github.sha }} |
| 77 |
# install a non-editable version of bidict at the revision under test |
| 78 |
uv pip install --python "$VIRTUAL_ENV/bin/python" --no-deps . |
| 79 |
# restore the current revision so we use its version of tests/microbenchmarks.py |
| 80 |
git checkout ${{ github.sha }} |
| 81 |
# move aside the '"'"'bidict'"'"' subdirectory to make sure we always import the installed version |
| 82 |
mv -v bidict src |
| 83 |
curl -L -s -o baseline.json "${{ steps.metadata.outputs.baseline_url }}" |
| 84 |
line1=$(head -n1 baseline.json) |
| 85 |
[ "$line1" = "{" ] |
| 86 |
./cachegrind.py pytest -c /dev/null -n0 \ |
| 87 |
--benchmark-autosave \ |
| 88 |
--benchmark-columns=min,rounds,iterations \ |
| 89 |
--benchmark-disable-gc \ |
| 90 |
--benchmark-group-by=name \ |
| 91 |
--benchmark-compare=baseline.json \ |
| 92 |
microbenchmarks.py |
| 93 |
' 2>&1 | tee benchmark-output.txt |
| 94 |
benchmark_status=${PIPESTATUS[0]} |
| 95 |
# Nothing here is allowed to fail the job except the run itself failing, which |
| 96 |
# means pytest could not benchmark the revision at all. |
| 97 |
if [ "$benchmark_status" -eq 0 ]; then |
| 98 |
result_state=ok |
| 99 |
result_message='benchmarks ran' |
| 100 |
else |
| 101 |
result_state=failed |
| 102 |
result_message='benchmark execution failed' |
| 103 |
fi |
| 104 |
{ |
| 105 |
echo "exit_code=$benchmark_status" |
| 106 |
echo "result_state=$result_state" |
| 107 |
echo "result_message=$result_message" |
| 108 |
} >> "$GITHUB_OUTPUT" |
| 109 |
exit "$benchmark_status" |
| 110 |
env: |
| 111 |
CACHEGRIND_ESTIMATE_OUT: ${{ github.workspace }}/instructions.txt |
| 112 |
- name: compare instruction estimate to baseline |
| 113 |
id: instructions |
| 114 |
if: always() |
| 115 |
env: |
| 116 |
INSTR_URL: ${{ steps.metadata.outputs.instr_url }} |
| 117 |
run: | |
| 118 |
# This comparison is reproducible where a wall-clock one is not: two runs of the |
| 119 |
# same revision have been measured 0.13% apart, against swings of tens of percent |
| 120 |
# for the per-benchmark timings. |
| 121 |
state=unavailable |
| 122 |
delta= |
| 123 |
now= |
| 124 |
baseline= |
| 125 |
if [ ! -s instructions.txt ]; then |
| 126 |
# Distinct from a missing baseline: this means cachegrind.py did not honor |
| 127 |
# CACHEGRIND_ESTIMATE_OUT, i.e. something here is broken rather than unpublished. |
| 128 |
summary='cachegrind.py recorded no instruction estimate' |
| 129 |
elif ! curl -fL -s -o instructions-baseline.txt "$INSTR_URL" || [ ! -s instructions-baseline.txt ]; then |
| 130 |
summary="no instruction baseline published yet (${INSTR_URL##*/})" |
| 131 |
else |
| 132 |
now=$(cat instructions.txt) |
| 133 |
baseline=$(cat instructions-baseline.txt) |
| 134 |
delta=$(python -c "print(f'{($now - $baseline) / $baseline * 100:+.2f}')") |
| 135 |
state=$(python -c "d = $delta; p = $INSTRUCTION_CHANGE_PCT; print('regression' if d > p else 'improved' if d < -p else 'ok')") |
| 136 |
if [ "$state" = regression ]; then |
| 137 |
summary="${delta}% vs baseline, over the ${INSTRUCTION_CHANGE_PCT}% threshold" |
| 138 |
else |
| 139 |
summary="${delta}% vs baseline" |
| 140 |
fi |
| 141 |
fi |
| 142 |
{ |
| 143 |
echo "state=$state" |
| 144 |
echo "summary=$summary" |
| 145 |
echo "delta=$delta" |
| 146 |
echo "now=$now" |
| 147 |
echo "baseline=$baseline" |
| 148 |
} >> "$GITHUB_OUTPUT" |
| 149 |
# Warn only on pull requests, matching the timing check: on a push the baseline is |
| 150 |
# whatever was last published, so drift since then is expected rather than notable. |
| 151 |
if [ "$state" = regression ] && [ "${{ github.event_name }}" = "pull_request" ]; then |
| 152 |
echo "::warning::Instruction count regressed by ${delta}% vs the baseline (threshold ${INSTRUCTION_CHANGE_PCT}%)." |
| 153 |
fi |
| 154 |
|
| 155 |
- name: summarize benchmark result |
| 156 |
if: always() |
| 157 |
run: | |
| 158 |
{ |
| 159 |
echo '## Benchmark result' |
| 160 |
echo |
| 161 |
echo "- Baseline asset: \`${{ steps.metadata.outputs.baseline_asset_name }}\`" |
| 162 |
echo "- Benchmark run: [${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}](${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID})" |
| 163 |
echo "- Instruction threshold: \`${INSTRUCTION_CHANGE_PCT}%\`" |
| 164 |
echo "- Result: ${{ steps.benchmark.outputs.result_message }}" |
| 165 |
echo "- Instruction count: ${{ steps.instructions.outputs.summary }}" |
| 166 |
} >> "$GITHUB_STEP_SUMMARY" |
| 167 |
- name: prepare PR benchmark comment |
| 168 |
if: always() && github.event_name == 'pull_request' |
| 169 |
env: |
| 170 |
BASELINE_ASSET_NAME: ${{ steps.metadata.outputs.baseline_asset_name }} |
| 171 |
PR_NUMBER: ${{ github.event.pull_request.number }} |
| 172 |
RESULT_MESSAGE: ${{ steps.benchmark.outputs.result_message }} |
| 173 |
RESULT_STATE: ${{ steps.benchmark.outputs.result_state }} |
| 174 |
INSTR_STATE: ${{ steps.instructions.outputs.state }} |
| 175 |
INSTR_SUMMARY: ${{ steps.instructions.outputs.summary }} |
| 176 |
INSTR_DELTA: ${{ steps.instructions.outputs.delta }} |
| 177 |
run: | |
| 178 |
mkdir -p benchmark-pr-comment |
| 179 |
run_url="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}" |
| 180 |
case "$INSTR_STATE" in |
| 181 |
improved) headline=":zap: Instruction count improved: ${INSTR_SUMMARY}." ;; |
| 182 |
regression) headline=":warning: Instruction count regressed: ${INSTR_SUMMARY}." ;; |
| 183 |
ok) headline=":white_check_mark: Instruction count within threshold: ${INSTR_SUMMARY}." ;; |
| 184 |
*) headline=":grey_question: Instruction count: ${INSTR_SUMMARY}." ;; |
| 185 |
esac |
| 186 |
if [ "$RESULT_STATE" != ok ]; then |
| 187 |
headline=":x: Benchmark execution failed, so there is nothing to compare." |
| 188 |
fi |
| 189 |
cat > benchmark-pr-comment/comment.md <<EOF |
| 190 |
<!-- bidict-benchmark-comment --> |
| 191 |
## Benchmark report |
| 192 |
|
| 193 |
${headline} |
| 194 |
|
| 195 |
- Baseline asset: \`${BASELINE_ASSET_NAME}\` |
| 196 |
- Instruction threshold: \`${INSTRUCTION_CHANGE_PCT}%\` |
| 197 |
- Run: [benchmark workflow run](${run_url}) |
| 198 |
|
| 199 |
_Instruction counts are reported for information and do not block merging. Wall-clock |
| 200 |
timings are compared in the run output too, but they are measured under Cachegrind and |
| 201 |
vary too much between runs to report on._ |
| 202 |
EOF |
| 203 |
python - <<'PY' |
| 204 |
import json |
| 205 |
import os |
| 206 |
from pathlib import Path |
| 207 |
|
| 208 |
payload = { |
| 209 |
'pr_number': os.environ['PR_NUMBER'], |
| 210 |
'result_state': os.environ['RESULT_STATE'], |
| 211 |
'result_message': os.environ['RESULT_MESSAGE'], |
| 212 |
'baseline_asset_name': os.environ['BASELINE_ASSET_NAME'], |
| 213 |
'instruction_threshold_pct': os.environ['INSTRUCTION_CHANGE_PCT'], |
| 214 |
'instruction_state': os.environ['INSTR_STATE'], |
| 215 |
'instruction_summary': os.environ['INSTR_SUMMARY'], |
| 216 |
'instruction_delta_pct': os.environ['INSTR_DELTA'], |
| 217 |
'run_url': f"{os.environ['GITHUB_SERVER_URL']}/{os.environ['GITHUB_REPOSITORY']}/actions/runs/{os.environ['GITHUB_RUN_ID']}", |
| 218 |
} |
| 219 |
Path('benchmark-pr-comment/result.json').write_text(json.dumps(payload, indent=2) + '\n') |
| 220 |
PY |
| 221 |
- name: archive PR benchmark comment |
| 222 |
if: always() && github.event_name == 'pull_request' |
| 223 |
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a |
| 224 |
with: |
| 225 |
name: benchmark-pr-comment |
| 226 |
path: benchmark-pr-comment |
| 227 |
if-no-files-found: error |
| 228 |
- name: archive benchmark results |
| 229 |
if: always() |
| 230 |
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a |
| 231 |
with: |
| 232 |
name: microbenchmark results (CPython ${{ steps.metadata.outputs.python_version }}) |
| 233 |
path: | |
| 234 |
.benchmarks |
| 235 |
benchmark-output.txt |
| 236 |
instructions.txt |
| 237 |
include-hidden-files: true |
| 238 |
if-no-files-found: error |
| 239 |
|
| 240 |
permissions: |
| 241 |
contents: read |