Operation

Struct Operation 

Source
pub struct Operation { /* private fields */ }
Expand description

A measurement handle for tracking mean memory allocation per operation across multiple iterations.

This utility is particularly useful for benchmarking scenarios where you want to understand the mean memory footprint and allocation behavior of repeated operations. It tracks both the number of bytes allocated and the count of allocations. Operations share data with their parent session via reference counting, and data is merged when the operation is dropped.

Multiple operations with the same name can be created concurrently.

§Examples

use alloc_tracker::{Allocator, Operation, Session};

#[global_allocator]
static ALLOCATOR: Allocator<std::alloc::System> = Allocator::system();

let session = Session::new();
let mean_calc = session.operation("string_allocations");

// Simulate multiple operations
for i in 0..5 {
    let _span = mean_calc.measure_process();
    let _data = vec![0; i + 1]; // Allocate different amounts
}

let mean_bytes = mean_calc.mean();
println!("Mean allocation: {} bytes per operation", mean_bytes);

Implementations§

Source§

impl Operation

Source

pub fn measure_thread(&self) -> ThreadSpan

Creates a span that tracks thread allocations from creation until it is dropped.

This method tracks allocations made by the current thread only. Use this when you want to measure allocations for single-threaded operations or when you want to track per-thread allocation usage.

The span defaults to 1 iteration but can be changed using the iterations() method.

§Examples
use alloc_tracker::{Allocator, Session};

#[global_allocator]
static ALLOCATOR: Allocator<std::alloc::System> = Allocator::system();

let session = Session::new();
let operation = session.operation("thread_work");
{
    let _span = operation.measure_thread();
    // Perform some allocation in this thread
    let _data = vec![1, 2, 3, 4, 5];
} // Thread allocations are tracked for 1 iteration

For batch operations:

use alloc_tracker::{Allocator, Session};

#[global_allocator]
static ALLOCATOR: Allocator<std::alloc::System> = Allocator::system();

let session = Session::new();
let operation = session.operation("batch_work");
{
    let _span = operation.measure_thread().iterations(1000);
    for _ in 0..1000 {
        // Perform the same operation 1000 times
        let _data = vec![42];
    }
} // Total allocation is measured once and divided by 1000
Source

pub fn measure_process(&self) -> ProcessSpan

Creates a span that tracks process allocations from creation until it is dropped.

This method tracks allocations made by the entire process (all threads). Use this when you want to measure total allocations including multi-threaded operations or when you want to track overall process allocation usage.

The span defaults to 1 iteration but can be changed using the iterations() method.

§Examples
use alloc_tracker::{Allocator, Session};

#[global_allocator]
static ALLOCATOR: Allocator<std::alloc::System> = Allocator::system();

let session = Session::new();
let operation = session.operation("process_work");
{
    let _span = operation.measure_process();
    // Perform some allocation that might span threads
    let _data = vec![1, 2, 3, 4, 5];
} // Total process allocations are tracked for 1 iteration

For batch operations:

use alloc_tracker::{Allocator, Session};

#[global_allocator]
static ALLOCATOR: Allocator<std::alloc::System> = Allocator::system();

let session = Session::new();
let operation = session.operation("batch_work");
{
    let _span = operation.measure_process().iterations(1000);
    for _ in 0..1000 {
        // Perform the same operation 1000 times
        let _data = vec![42];
    }
} // Total allocation is measured once and divided by 1000
Source

pub fn mean(&self) -> u64

Calculates the mean bytes allocated per iteration.

Returns 0 if no iterations have been recorded.

Source

pub fn total_bytes_allocated(&self) -> u64

Returns the total bytes allocated across all iterations.

Trait Implementations§

Source§

impl Debug for Operation

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Operation

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.