Skip to content

Commit 2f2a536

Browse files
authored
ci: expand pyclass benchmarks (#5816)
* ci: expand pyclass benchmarks * fix resource issue in bench_str
1 parent fe00ce7 commit 2f2a536

1 file changed

Lines changed: 42 additions & 3 deletions

File tree

pyo3-benches/benches/bench_pyclass.rs

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use codspeed_criterion_compat::{criterion_group, criterion_main, Bencher, Criterion};
1+
use codspeed_criterion_compat::{criterion_group, criterion_main, BatchSize, Bencher, Criterion};
2+
use pyo3::conversion::IntoPyObjectExt;
3+
use pyo3::types::PyInt;
24
use pyo3::{impl_::pyclass::LazyTypeObject, prelude::*};
35

46
/// This is a feature-rich class instance used to benchmark various parts of the pyclass lifecycle.
@@ -37,10 +39,47 @@ pub fn first_time_init(b: &mut Bencher<'_>) {
3739
});
3840
}
3941

40-
fn criterion_benchmark(c: &mut Criterion) {
42+
pub fn bench_pyclass(c: &mut Criterion) {
43+
c.bench_function("bench_pyclass_create", |b| {
44+
Python::attach(|py| {
45+
b.iter_batched(
46+
|| vec![1, 2, 3],
47+
|elements| {
48+
MyClass::new(elements).into_py_any(py).unwrap();
49+
},
50+
BatchSize::SmallInput,
51+
);
52+
});
53+
});
54+
c.bench_function("bench_call", |b| {
55+
Python::attach(|py| {
56+
b.iter_batched(
57+
|| {
58+
(
59+
MyClass::new(vec![1, 2, 3]).into_py_any(py).unwrap(),
60+
PyInt::new(py, 4),
61+
)
62+
},
63+
|(inst, arg)| {
64+
inst.call1(py, (arg,)).unwrap();
65+
},
66+
BatchSize::SmallInput,
67+
);
68+
});
69+
});
70+
c.bench_function("bench_str", |b| {
71+
Python::attach(|py| {
72+
let inst = MyClass::new(vec![1, 2, 3]).into_py_any(py).unwrap();
73+
let bound = inst.bind(py);
74+
b.iter(|| bound.str());
75+
});
76+
});
77+
}
78+
79+
fn bench_first_time_init(c: &mut Criterion) {
4180
c.bench_function("first_time_init", first_time_init);
4281
}
4382

44-
criterion_group!(benches, criterion_benchmark);
83+
criterion_group!(benches, bench_first_time_init, bench_pyclass);
4584

4685
criterion_main!(benches);

0 commit comments

Comments
 (0)