Files
RustPython/benches
Jeong, YunWon d7e41ebff5 Split CI cargo run and test into build then execute (#8705)
* Run gc_collect ten times per benchmark iteration

Assisted-by: Grok:4.6

* Reuse the prebuilt rustpython for CI whats_left

whats_left was invoking cargo build --release without threading,
so the snippets job rebuilt the interpreter after the earlier
release build.

Assisted-by: Grok:4.6

* Split CI cargo run and test into build then execute

Assisted-by: Grok:4.6

* Compile doctests in the CI build step

cargo test --no-run skips doctests, so rustdoc compilation
was still happening in the run step.

Assisted-by: Grok:4.6

* Drop cron-ci.yaml from this PR

A pull_request path filter matches the whole PR, so every
push was starting the periodic benchmark and coverage jobs.

Assisted-by: Grok:4.6

* Run doctests in their own CI step

cargo test --doc --no-run is rejected, so doctests cannot
be compiled without also executing them.

Assisted-by: Grok:4.6

* Do not run benchmark targets in rust tests

--all-targets includes Criterion benches, which cargo test
does not run by default.

Assisted-by: Grok:4.6
2026-09-14 22:08:40 +09:00
..

Benchmarking

These are some files to determine performance of rustpython.

Usage

Running cargo bench from the root of the repository will start the benchmarks. Once done there will be a graphical report under target/criterion/report/index.html that you can use use to view the results.

cargo bench supports name matching to run a subset of the benchmarks. To run only the sorted microbenchmark, you can run:

cargo bench sorted

To view Python tracebacks during benchmarks, run RUST_BACKTRACE=1 cargo bench. You can also bench against a specific installed Python version by running:

PYTHON_SYS_EXECUTABLE=python3.13 cargo bench

Continuous benchmarking with CodSpeed

The benchmarks are also run on every pull request by CodSpeed, 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.

To reproduce a CodSpeed run locally, install cargo-codspeed and the CodSpeed CLI, then run:

cargo codspeed build -p rustpython -p rustpython-sre_engine
codspeed run --mode simulation -- cargo codspeed run -p rustpython -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 instead of five. The criterion benchmark id does not include the iteration count, so all five sizes are reported under the same name.

Adding a benchmark

Simply adding a file to the benchmarks/ directory will add it to the set of files benchmarked. Each file is tested in two ways:

  1. The time to parse the file to AST
  2. The time it takes to execute the file

Adding a micro benchmark

Micro benchmarks are small snippets of code added under the microbenchmarks/ directory. A microbenchmark file has two sections:

  1. Optional setup code
  2. The code to be benchmarked

These two sections are delimited by # ---. For example:

a_list = [1,2,3]

# ---

len(a_list)

Only len(a_list) will be timed. Setup or benchmarked code can optionally reference a variable called ITERATIONS. If present then the benchmark code will be invoked 5 times with ITERATIONS set to a value between 100 and 1,000. For example:

obj = [i for i in range(ITERATIONS)]

ITERATIONS can appear in both the setup code and the benchmark code.

MacOS setup

On MacOS you will need to add the following to a .cargo/config file:

[target.x86_64-apple-darwin]
rustflags = [
    "-C", "link-arg=-undefined",
    "-C", "link-arg=dynamic_lookup",
]

Benchmark source