Skip to content

Commit ec6b9d5

Browse files
committed
Run benchmark workloads with consistent script namespaces
Assisted-by: Codex:gpt-6
1 parent d4b43b8 commit ec6b9d5

3 files changed

Lines changed: 42 additions & 26 deletions

File tree

‎benches/README.md‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ Two things differ when the benchmarks run under CodSpeed:
4242
- The CPython comparison benchmarks are skipped. They are useful to compare RustPython against
4343
CPython locally, but CodSpeed tracks the evolution of RustPython itself, and running them would
4444
double the duration of an already slow instrumented run.
45-
- The microbenchmarks using `ITERATIONS` run with a single value instead of five. The criterion
46-
benchmark id does not include the iteration count, so all five sizes are reported under the same
47-
name.
45+
- The microbenchmarks using `ITERATIONS` run with a single value of 1,000 instead of five sizes.
46+
CodSpeed retains the existing benchmark names; local runs include the iteration count in each
47+
benchmark id.
4848

4949
### Adding a benchmark
5050

@@ -54,6 +54,9 @@ in two ways:
5454
1. The time to parse the file to AST
5555
2. The time it takes to execute the file
5656

57+
Execution includes compilation and runs each iteration with a fresh namespace whose `__name__`
58+
is `"__main__"`, so script entry points run in both RustPython and CPython.
59+
5760
### Adding a micro benchmark
5861

5962
Micro benchmarks are small snippets of code added under the `microbenchmarks/` directory. A microbenchmark file has
@@ -73,7 +76,7 @@ len(a_list)
7376
```
7477

7578
Only `len(a_list)` will be timed. Setup or benchmarked code can optionally reference a variable called `ITERATIONS`. If
76-
present then the benchmark code will be invoked 5 times with `ITERATIONS` set to a value between 100 and 1,000. For
79+
present then the benchmark runs at five sizes: 100, 300, 500, 700 and 900 iterations locally. For
7780
example:
7881

7982
```python

‎benches/execution.rs‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use criterion::{
77
Bencher, BenchmarkGroup, BenchmarkId, Criterion, Throughput, criterion_group, criterion_main,
88
measurement::WallTime,
99
};
10+
use pyo3::types::{PyCode, PyCodeInput, PyCodeMethods, PyDict, PyDictMethods};
1011
use rustpython_compiler::Mode;
1112
use rustpython_vm::{Interpreter, PyResult, Settings};
1213
use std::{collections::HashMap, path::Path};
@@ -25,9 +26,14 @@ fn bench_cpython_code(b: &mut Bencher<'_>, source: &str) {
2526
let c_str_source = c_str_source_head.as_c_str();
2627
pyo3::Python::attach(|py| {
2728
b.iter(|| {
28-
let module = pyo3::types::PyModule::from_code(py, c_str_source, c"", c"")
29+
let code = PyCode::compile(py, c_str_source, c"<benchmark>", PyCodeInput::File)
30+
.expect("Error compiling source");
31+
let globals = PyDict::new(py);
32+
globals.set_item("__name__", "__main__").unwrap();
33+
let result = code
34+
.run(Some(&globals), None)
2935
.expect("Error running source");
30-
black_box(module);
36+
black_box(result);
3137
})
3238
})
3339
}
@@ -46,6 +52,10 @@ fn bench_rustpython_code(b: &mut Bencher<'_>, name: &str, source: &str) {
4652
b.iter(|| {
4753
let code = vm.compile(source, Mode::Exec, name).unwrap();
4854
let scope = vm.new_scope_with_builtins();
55+
scope
56+
.globals
57+
.set_item("__name__", vm.ctx.new_str("__main__").into(), vm)
58+
.unwrap();
4959
let res: PyResult = vm.run_code_obj(code, scope);
5060
vm.unwrap_pyresult(res);
5161
})

‎benches/microbenchmarks.rs‎

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,8 @@ fn bench_cpython_code(group: &mut BenchmarkGroup<'_, WallTime>, bench: &MicroBen
4545
pyo3::types::PyModule::import(py, "builtins").expect("Failed to import builtins");
4646
let exec = builtins.getattr("exec").expect("no exec in builtins");
4747

48-
let bench_func = |(globals, locals): &mut (
49-
pyo3::Bound<'_, pyo3::types::PyDict>,
50-
pyo3::Bound<'_, pyo3::types::PyDict>,
51-
)| {
52-
let res = exec.call((&code, &*globals, &*locals), None);
48+
let bench_func = |globals: &mut pyo3::Bound<'_, pyo3::types::PyDict>| {
49+
let res = exec.call((&code, &*globals), None);
5350
if let Err(e) = res {
5451
e.print(py);
5552
panic!("Error running microbenchmark")
@@ -58,23 +55,24 @@ fn bench_cpython_code(group: &mut BenchmarkGroup<'_, WallTime>, bench: &MicroBen
5855

5956
let bench_setup = |iterations| {
6057
let globals = pyo3::types::PyDict::new(py);
61-
let locals = pyo3::types::PyDict::new(py);
58+
globals.set_item("__name__", "__main__").unwrap();
6259
if let Some(idx) = iterations {
6360
globals.set_item("ITERATIONS", idx).unwrap();
6461
}
6562

66-
let res = exec.call((&setup_code, &globals, &locals), None);
63+
let res = exec.call((&setup_code, &globals), None);
6764
if let Err(e) = res {
6865
e.print(py);
6966
panic!("Error running microbenchmark setup code")
7067
}
71-
(globals, locals)
68+
globals
7269
};
7370

7471
if bench.iterate {
7572
for idx in iteration_counts() {
7673
group.throughput(Throughput::Elements(idx as u64));
77-
group.bench_with_input(BenchmarkId::new("cpython", &bench.name), &idx, |b, idx| {
74+
let id = BenchmarkId::new("cpython", format!("{}[{idx}]", bench.name));
75+
group.bench_with_input(id, &idx, |b, idx| {
7876
b.iter_batched_ref(
7977
|| bench_setup(Some(*idx)),
8078
bench_func,
@@ -128,6 +126,10 @@ fn bench_rustpython_code(group: &mut BenchmarkGroup<'_, WallTime>, bench: &Micro
128126

129127
let bench_setup = |iterations| {
130128
let scope = vm.new_scope_with_builtins();
129+
scope
130+
.globals
131+
.set_item("__name__", vm.ctx.new_str("__main__").into(), vm)
132+
.unwrap();
131133
if let Some(idx) = iterations {
132134
scope
133135
.locals
@@ -145,17 +147,18 @@ fn bench_rustpython_code(group: &mut BenchmarkGroup<'_, WallTime>, bench: &Micro
145147
if bench.iterate {
146148
for idx in iteration_counts() {
147149
group.throughput(Throughput::Elements(idx as u64));
148-
group.bench_with_input(
149-
BenchmarkId::new("rustpython", &bench.name),
150-
&idx,
151-
|b, idx| {
152-
b.iter_batched(
153-
|| bench_setup(Some(*idx)),
154-
bench_func,
155-
BatchSize::LargeInput,
156-
);
157-
},
158-
);
150+
let name = if is_codspeed() {
151+
bench.name.clone()
152+
} else {
153+
format!("{}[{idx}]", bench.name)
154+
};
155+
group.bench_with_input(BenchmarkId::new("rustpython", name), &idx, |b, idx| {
156+
b.iter_batched(
157+
|| bench_setup(Some(*idx)),
158+
bench_func,
159+
BatchSize::LargeInput,
160+
);
161+
});
159162
}
160163
} else {
161164
group.bench_function(BenchmarkId::new("rustpython", &bench.name), move |b| {

0 commit comments

Comments
 (0)