Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/actionlint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
self-hosted-runner:
# CodSpeed Macro runners (https://codspeed.io/docs/instruments/walltime), used by codspeed.yaml.
labels:
- codspeed-macro
35 changes: 35 additions & 0 deletions .github/scripts/install-cargo-codspeed.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env bash
# Install a released cargo-codspeed executable into ~/.cargo/bin without
# compiling it. Usage: install-cargo-codspeed.sh v5.0.3
set -euo pipefail

version="${1:?usage: install-cargo-codspeed.sh <tag>}"

case "$(uname -s)-$(uname -m)" in
Linux-x86_64) target=x86_64-unknown-linux-musl ;;
Linux-aarch64) target=aarch64-unknown-linux-musl ;;
Darwin-arm64) target=aarch64-apple-darwin ;;
*) echo "no cargo-codspeed release for $(uname -s)-$(uname -m)" >&2; exit 1 ;;
esac

# `cargo codspeed --version` prints the version to stderr and exits non-zero,
# so only its output can be trusted.
installed_version() { cargo codspeed --version 2>&1 || true; }

want="cargo-codspeed ${version#v}"
if [ "$(installed_version)" = "${want}" ]; then
echo "${want} already installed"
exit 0
fi

url="https://github.com/CodSpeedHQ/codspeed-rust/releases/download/${version}/cargo-codspeed-${target}"
bin="${CARGO_HOME:-$HOME/.cargo}/bin"
mkdir -p "${bin}"
curl --fail --silent --show-error --location --output "${bin}/cargo-codspeed" "${url}"
chmod +x "${bin}/cargo-codspeed"
got="$(installed_version)"
if [ "${got}" != "${want}" ]; then
echo "expected ${want}, got '${got}'" >&2
exit 1
fi
echo "${got}"
3 changes: 3 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,9 @@ jobs:
- name: prek run
run: prek run --show-diff-on-failure --color=always --all-files

- name: Check the vendored pyperformance benchmarks match their release
run: python3 -I scripts/vendor-pyperformance.py --check

- name: Clone CPython opcode metadata source
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
Expand Down
113 changes: 62 additions & 51 deletions .github/workflows/codspeed.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,23 @@ env:
CARGO_INCREMENTAL: 0
CARGO_TERM_COLOR: always
CI: true
# Must share its major version with the `codspeed-criterion-compat` crate
# in Cargo.toml, which writes the raw walltime results this tool aggregates.
CARGO_CODSPEED_VERSION: v5.0.3

jobs:
benchmarks:
# The benchmark executables are compiled on a hosted runner, so the Macro
# runner's time goes to measuring, not to building.
build:
if: ${{ !contains(github.event.pull_request.labels.*.name, 'skip:ci') }}
name: Run benchmarks
name: Build the benchmark executables
runs-on: ubuntu-latest
timeout-minutes: 90
permissions:
contents: read
id-token: write # for OpenID Connect authentication with CodSpeed
timeout-minutes: 60
env:
CACHE_PATHS: |
# https://doc.rust-lang.org/cargo/guide/cargo-home.html#caching-the-cargo-home-in-ci
~/.cargo/.crates2.json
~/.cargo/.crates.toml
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
Expand All @@ -48,67 +49,77 @@ jobs:

- uses: dtolnay/rust-toolchain@stable

# The benchmarks are linked against libpython through the pyo3 dev-dependency.
- uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0

- name: Restore the machine main last measured on
# A recording that cannot be fetched is read as "none yet", which the step
# below treats as nothing to disagree with. Failing here instead would
# turn a cache outage into a red benchmark job.
continue-on-error: true
uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: build/codspeed-environment-reference.json
key: codspeed-environment-${{ runner.os }}-${{ runner.arch }}-${{ github.run_id }}
restore-keys: codspeed-environment-${{ runner.os }}-${{ runner.arch }}-

- name: Name the machine this run drew
id: environment
shell: bash
run: |
reference=build/codspeed-environment-reference.json
if [ "$GITHUB_REF" = refs/heads/main ]; then
args=(--record "$reference")
else
args=(--record build/codspeed-environment.json --reference "$reference")
fi
python3 scripts/codspeed-environment.py "${args[@]}" --github-output

- name: Restore cache
if: ${{ steps.environment.outputs.match != 'false' }}
uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: ${{ env.CACHE_PATHS }}
key: codspeed-v2-${{ hashFiles('**/Cargo.lock') }}
restore-keys: codspeed-v2-
key: codspeed-v3-${{ hashFiles('**/Cargo.lock') }}
restore-keys: codspeed-v3-
continue-on-error: true

- name: Install cargo-codspeed
if: ${{ steps.environment.outputs.match != 'false' }}
run: cargo install cargo-codspeed --locked --version ^5
run: .github/scripts/install-cargo-codspeed.sh "${CARGO_CODSPEED_VERSION}"

# `benches/execution.rs` and `benches/microbenchmarks.rs` are not built:
# they link CPython through pyo3 for a local comparison, and an executable
# linked against one machine's libpython cannot be moved to the Macro
# runner that executes it.
- name: Build the benchmark targets
if: ${{ steps.environment.outputs.match != 'false' }}
run: cargo codspeed build --locked --measurement-mode simulation -p rustpython -p rustpython-sre_engine
run: >-
cargo codspeed build --locked --measurement-mode walltime
-p rustpython --bench pyperformance
-p rustpython-sre_engine

- name: Save cache
if: ${{ github.ref == 'refs/heads/main' }} # only save on main
uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: ${{ env.CACHE_PATHS }}
key: codspeed-v2-${{ hashFiles('**/Cargo.lock') }}
key: codspeed-v3-${{ hashFiles('**/Cargo.lock') }}
continue-on-error: true

- name: Run the benchmarks
if: ${{ steps.environment.outputs.match != 'false' }}
uses: CodSpeedHQ/action@78102a468f96d8e0ccb92d0ec49425753fc42062 # v5.6.0
- name: Upload the benchmark executables
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
mode: simulation
run: cargo codspeed run -p rustpython -p rustpython-sre_engine
name: codspeed-benchmarks-${{ github.run_id }}
path: target/codspeed/walltime/
retention-days: 1
if-no-files-found: error

- name: Record the machine this baseline was measured on
if: ${{ success() && github.ref == 'refs/heads/main' }}
uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
run:
name: Run the benchmarks
needs: build
# A CodSpeed Macro runner: dedicated hardware, so wall time is comparable
# between runs without any environment matching.
runs-on: codspeed-macro
timeout-minutes: 90
permissions:
contents: read
id-token: write # for OpenID Connect authentication with CodSpeed
steps:
# The executables read `Lib/` and `benches/pyperformance/` at run time,
# and `cargo codspeed run` reads the workspace metadata.
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false

- uses: dtolnay/rust-toolchain@stable

- name: Install cargo-codspeed
run: .github/scripts/install-cargo-codspeed.sh "${CARGO_CODSPEED_VERSION}"

- name: Download the benchmark executables
uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
with:
name: codspeed-benchmarks-${{ github.run_id }}
path: target/codspeed/walltime/

- name: Restore the executable bits
# Artifacts are zipped and come back without their permission bits.
run: find target/codspeed/walltime -type f -exec chmod +x {} +

- name: Run the benchmarks
uses: CodSpeedHQ/action@821a40cd6c80e3cfe387caf55668287addd808da # v5.5.0
with:
path: build/codspeed-environment-reference.json
key: codspeed-environment-${{ runner.os }}-${{ runner.arch }}-${{ github.run_id }}
mode: walltime
run: cargo codspeed run -p rustpython --bench pyperformance -p rustpython-sre_engine
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ pyo3 = { workspace = true, features = ["auto-initialize"] }
rustls-graviola = { workspace = true }
rustpython-stdlib = { workspace = true }
ruff_python_parser = { workspace = true }
toml = { workspace = true }

# Only the criterion benchmarks below are measured. Without `bench = false`,
# `cargo bench` (and `cargo codspeed build`) also builds the library and binary
Expand All @@ -83,6 +84,10 @@ harness = false
name = "microbenchmarks"
harness = false

[[bench]]
name = "pyperformance"
harness = false

[[bin]]
name = "rustpython"
path = "src/main.rs"
Expand Down Expand Up @@ -317,6 +322,7 @@ tcl-sys = { git = "https://github.com/arihant2math/tkinter.git", tag = "v0.2.0"
textwrap = { version = "0.16.2", default-features = false }
termios = "0.3.3"
thiserror = "2.0"
toml = "1.1.2"
thin-vec = "0.2.14"
tk-sys = { git = "https://github.com/arihant2math/tkinter.git", tag = "v0.2.0" }
icu_casemap = "2"
Expand Down
45 changes: 24 additions & 21 deletions benches/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,33 @@ PYTHON_SYS_EXECUTABLE=python3.13 cargo bench

## Continuous benchmarking with CodSpeed

The benchmarks are also run on every pull request by
[CodSpeed](https://app.codspeed.io/RustPython/RustPython), which measures them with CPU simulation
instead of wall time. `criterion` is aliased to `codspeed-criterion-compat`, so nothing changes for
`cargo bench`: the compatibility layer only takes over when the CodSpeed runner drives the
benchmarks.
RustPython's performance is tracked on every pull request by
[CodSpeed](https://app.codspeed.io/RustPython/RustPython) in wall-time mode on a CodSpeed Macro
runner (dedicated hardware). The workflow in `.github/workflows/codspeed.yaml` builds the
benchmark executables on a hosted runner and moves only those to the Macro runner. `criterion` is
aliased to `codspeed-criterion-compat`, so nothing changes for `cargo bench`: the compatibility layer
only takes over when the CodSpeed runner drives the benchmarks.

CodSpeed measures two targets:

- `pyperformance` (`benches/pyperformance.rs`): the upstream pyperformance benchmark programs,
vendored unmodified under `benches/pyperformance/`. See the README there for how they run and
which ones are included.
- the `rustpython-sre_engine` benchmarks.

The `execution` and `microbenchmarks` targets below are not run by CodSpeed. They link CPython
through `pyo3` for the comparison, and an executable linked against one machine's `libpython`
cannot be moved to another; they remain local tools.

To reproduce a CodSpeed run locally, install [`cargo-codspeed`](https://crates.io/crates/cargo-codspeed)
and the [CodSpeed CLI](https://codspeed.io/docs/cli), then run:
(same major version as `codspeed-criterion-compat` in `Cargo.toml`) and the
[CodSpeed CLI](https://codspeed.io/docs/cli), then run:

```shell
cargo codspeed build -p rustpython -p rustpython-sre_engine
codspeed run --mode simulation -- cargo codspeed run -p rustpython -p rustpython-sre_engine
cargo codspeed build --measurement-mode walltime -p rustpython --bench pyperformance -p rustpython-sre_engine
codspeed run --mode walltime -- cargo codspeed run -p rustpython --bench pyperformance -p rustpython-sre_engine
```

Two things differ when the benchmarks run under CodSpeed:

- The CPython comparison benchmarks are skipped. They are useful to compare RustPython against
CPython locally, but CodSpeed tracks the evolution of RustPython itself, and running them would
double the duration of an already slow instrumented run.
- The microbenchmarks using `ITERATIONS` run with a single value of 1,000 instead of five sizes.
CodSpeed retains the existing benchmark names; local runs include the iteration count in each
benchmark id.

### Adding a benchmark

Simply adding a file to the `benchmarks/` directory will add it to the set of files benchmarked. Each file is tested
Expand Down Expand Up @@ -123,7 +127,6 @@ rustflags = [
## Benchmark source

- <https://benchmarksgame-team.pages.debian.net/benchmarksgame/program/nbody-python3-2.html>
- The workloads marked as adapted from pyperformance come from
[python/pyperformance 1.14.0](https://github.com/python/pyperformance/tree/1.14.0/pyperformance/data-files/benchmarks).
Their `pyperf` runners and internal timers are removed so Criterion is the only measurement
harness, and some input sizes are reduced for CodSpeed simulation.
- The pyperformance programs under `pyperformance/` come from
[python/pyperformance 1.14.0](https://github.com/python/pyperformance/tree/1.14.0/pyperformance/data-files/benchmarks)
and are not modified; see `pyperformance/README.md`.
34 changes: 0 additions & 34 deletions benches/benchmarks/base64.py

This file was deleted.

42 changes: 0 additions & 42 deletions benches/benchmarks/decimal_factorial.py

This file was deleted.

Loading
Loading