|
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; |
2 | 4 | use pyo3::{impl_::pyclass::LazyTypeObject, prelude::*}; |
3 | 5 |
|
4 | 6 | /// 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<'_>) { |
37 | 39 | }); |
38 | 40 | } |
39 | 41 |
|
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) { |
41 | 80 | c.bench_function("first_time_init", first_time_init); |
42 | 81 | } |
43 | 82 |
|
44 | | -criterion_group!(benches, criterion_benchmark); |
| 83 | +criterion_group!(benches, bench_first_time_init, bench_pyclass); |
45 | 84 |
|
46 | 85 | criterion_main!(benches); |
0 commit comments