Skip to main content

Bencher

Struct Bencher 

Source
pub struct Bencher<'a> { /* private fields */ }
Expand description

Timing harness passed to a #[pg_bench] benchmark function.

Bench functions register exactly one timing loop with either Bencher::iter or Bencher::iter_batched.

use pgrx_bench::{Bencher, black_box};

#[pg_bench]
fn bench_normalize_phrase(b: &mut Bencher) {
    let phrase = "the quick brown fox";
    b.iter(|| crate::normalize_phrase(black_box(phrase)));
}

Implementations§

Source§

impl<'a> Bencher<'a>

Source

pub fn iter<R, F>(&mut self, routine: F)
where F: FnMut() -> R + 'a,

Registers a simple timing loop for a benchmark.

Use this when the benchmark body can reuse the same captured inputs for each iteration.

use pgrx_bench::{Bencher, black_box};

#[pg_bench]
fn bench_parse_uuid(b: &mut Bencher) {
    let input = "550e8400-e29b-41d4-a716-446655440000";
    b.iter(|| crate::parse_uuid(black_box(input)));
}
Source

pub fn iter_batched<I, R, S, F>( &mut self, setup: S, routine: F, batch_size: BatchSize, )
where I: 'static, S: FnMut() -> I + 'a, F: FnMut(I) -> R + 'a,

Registers a timing loop that performs per-batch setup outside the measured routine body.

Use this when each timing sample needs fresh inputs or temporary state.

use pgrx_bench::{BatchSize, Bencher};

#[pg_bench]
fn bench_transform_rows(b: &mut Bencher) {
    b.iter_batched(
        || (0..100).collect::<Vec<i32>>(),
        |rows| rows.into_iter().map(|value| value * 2).collect::<Vec<_>>(),
        BatchSize::SmallInput,
    );
}

Auto Trait Implementations§

§

impl<'a> Freeze for Bencher<'a>

§

impl<'a> !RefUnwindSafe for Bencher<'a>

§

impl<'a> !Send for Bencher<'a>

§

impl<'a> !Sync for Bencher<'a>

§

impl<'a> Unpin for Bencher<'a>

§

impl<'a> UnsafeUnpin for Bencher<'a>

§

impl<'a> !UnwindSafe for Bencher<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.