Register interpreter benchmarks from the python/ directory

Merged
SA
2mo ago
+1097-746

Follows #1055, which moved the scripts into benches/python/ but kept each benchmark's expected value, inputs and variants in main.rs.

Now the scripts are plain Python and main.rs registers them by reading the directory: adding a .py file adds a benchmark, with no Rust change.

  • Self-checking scripts. The last expression of each script became an assert on its old expected value (assert len(v) == 77), so any Python implementation can run the file and fail loudly if the result is wrong.
  • # bench: header for what Rust used to hard-code: data=<path> binds DATA to a file's contents, mount binds it to the mounted scan tree (overlay adds an overlay-mount variant), limits adds a resource-limits variant. Unknown options panic.
  • Registered from the directory: every top-level benches/python/*.py, plus test_cases/bench__*.py, so the kitchen sink stays shared with the datatest suite. Its # Return=373 became assert result == 373.
  • Names unchanged, so CodSpeed history carries over: fib_25.py was renamed to fib.py to keep fib__monty. The same benchmarks register as before: 63 locally and 36 under the codspeed feature.
  • CPython now runs each compiled module at module scope, in fresh globals, as Monty does, replacing wrap_for_cpython's text rewrite into def main(). Module-scope variable access is slower in CPython than locals, so the local __cpython numbers will rise. The comparison is now like for like.
  • The csv_read / csv_write benchmarks from #1018 are converted too. Their CPython variants were gated on cfg(not(codspeed)), which walltime and memory builds never set, so they were running in CodSpeed CI. Every CPython benchmark is now gated on the codspeed feature.
  • pool.rs's scripts moved to python/pool/, since they need a host to answer their calls. end_to_end now parses add_two.py.

The Rust-API benchmarks (parse_1k_assigns, clone_module, repl_feed_after_2k_snippets, session_dump/load) stay in Rust, because their code is generated.

Checked: -- --test passes with and without the codspeed feature, a wrong expected value fails the benchmark with the script's traceback, and the bench__kitchen_sink datatest passes.

🤖 Generated with Claude Code

https://claude.ai/code/session_016t3CWLMcgDnRzncyejqP6T


Summary by cubic

Registers interpreter benchmarks by reading the benches/python/ directory, so adding a .py file adds a benchmark without a Rust change. Each script checks its own result with an assert.

The new monty-fixture crate parses a # /// monty PEP 723 block of TOML shared by test cases and benchmark scripts. The datatest harness's old # call-external-style markers became header keys. The block must sit in the file's leading comments, so a string or later comment can't configure a fixture, and a misspelled key, wrong type, or malformed block panics instead of silently dropping the setting, with errors naming the line the header opens on. The header stays in the code, so TRACEBACK line numbers in test cases were updated to count its lines.

  • Scripts assert their own expected result; the old # bench: options became the header's [bench] table (data, mount, overlay, limits), and non-[bench] keys are rejected.
  • Both interpreters receive the script's real file name, so tracebacks match the file.
  • DATA is bound in limits runs too, and only loop_mod_13 keeps a limits variant now that the csv ones are dropped.
  • CPython runs each compiled module at module scope in fresh globals, matching Monty, so local __cpython numbers will shift as module-scope variable access is slower than the old main()-wrapped locals.
  • pool.rs's scripts moved to python/pool/ so the directory scan doesn't pick them up as standalone benchmarks.