2023-08-29 06:26:36 +00:00
|
|
|
use codspeed_criterion_compat::{criterion_group, criterion_main, BatchSize, Bencher, Criterion};
|
2021-05-16 07:11:37 +00:00
|
|
|
|
2024-01-30 11:45:06 +00:00
|
|
|
use pyo3::prelude::*;
|
2021-05-16 07:11:37 +00:00
|
|
|
|
2022-03-23 07:07:28 +00:00
|
|
|
fn bench_clean_acquire_gil(b: &mut Bencher<'_>) {
|
2021-05-16 07:11:37 +00:00
|
|
|
// Acquiring first GIL will also create a "clean" GILPool, so this measures the Python overhead.
|
2022-08-14 06:44:11 +00:00
|
|
|
b.iter(|| Python::with_gil(|_| {}));
|
2021-05-16 07:11:37 +00:00
|
|
|
}
|
|
|
|
|
2022-03-23 07:07:28 +00:00
|
|
|
fn bench_dirty_acquire_gil(b: &mut Bencher<'_>) {
|
2023-11-16 15:41:58 +00:00
|
|
|
let obj: PyObject = Python::with_gil(|py| py.None().into());
|
2021-05-16 07:11:37 +00:00
|
|
|
b.iter_batched(
|
|
|
|
|| {
|
|
|
|
// Clone and drop an object so that the GILPool has work to do.
|
|
|
|
let _ = obj.clone();
|
|
|
|
},
|
2022-08-14 06:44:11 +00:00
|
|
|
|_| Python::with_gil(|_| {}),
|
2021-05-16 07:11:37 +00:00
|
|
|
BatchSize::NumBatches(1),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn criterion_benchmark(c: &mut Criterion) {
|
|
|
|
c.bench_function("clean_acquire_gil", bench_clean_acquire_gil);
|
|
|
|
c.bench_function("dirty_acquire_gil", bench_dirty_acquire_gil);
|
|
|
|
}
|
|
|
|
|
|
|
|
criterion_group!(benches, criterion_benchmark);
|
|
|
|
criterion_main!(benches);
|