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
impl Operation
Sourcepub fn measure_thread(&self) -> ThreadSpan
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 iterationFor 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 1000Sourcepub fn measure_process(&self) -> ProcessSpan
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 iterationFor 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 1000Sourcepub fn mean(&self) -> u64
pub fn mean(&self) -> u64
Calculates the mean bytes allocated per iteration.
Returns 0 if no iterations have been recorded.
Sourcepub fn total_bytes_allocated(&self) -> u64
pub fn total_bytes_allocated(&self) -> u64
Returns the total bytes allocated across all iterations.