pub struct Session { /* private fields */ }Expand description
Manages allocation tracking session state and contains operations.
§Examples
use alloc_tracker::{Allocator, Session};
#[global_allocator]
static ALLOCATOR: Allocator<std::alloc::System> = Allocator::system();
let session = Session::new();
let string_op = session.operation("do_stuff_with_strings");
{
let _span = string_op.measure_process().iterations(3);
for _ in 0..3 {
let _data = String::from("example string allocation");
}
}
// Output statistics of all operations to console.
// Using print_to_stdout() here is important in benchmarks because it will
// print nothing if no spans were recorded, not even an empty line, which can
// be functionally critical for benchmark harness behavior.
session.print_to_stdout();Implementations§
Source§impl Session
impl Session
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new allocation tracking session.
§Examples
use alloc_tracker::{Allocator, Session};
#[global_allocator]
static ALLOCATOR: Allocator<std::alloc::System> = Allocator::system();
let session = Session::new();
// Allocation tracking is now enabled
// Session will disable tracking when droppedSourcepub fn operation(&self, name: impl Into<String>) -> Operation
pub fn operation(&self, name: impl Into<String>) -> Operation
Creates or retrieves an operation with the given name.
If an operation with the given name already exists, its existing statistics are preserved
and any consecutive or concurrent use of multiple such Operation instances will merge
the data sets.
§Examples
use alloc_tracker::{Allocator, Session};
#[global_allocator]
static ALLOCATOR: Allocator<std::alloc::System> = Allocator::system();
let session = Session::new();
let string_op = session.operation("string_operations");
{
let _span = string_op.measure_process().iterations(3);
for _ in 0..3 {
let _s = String::from("test"); // This allocation will be tracked
}
}Sourcepub fn to_report(&self) -> Report
pub fn to_report(&self) -> Report
Creates a thread-safe report from this session.
The report contains a snapshot of all memory allocation statistics captured by this session.
§Examples
use alloc_tracker::{Allocator, Session};
#[global_allocator]
static ALLOCATOR: Allocator<std::alloc::System> = Allocator::system();
let session = Session::new();
let operation = session.operation("test_work");
{
let _span = operation.measure_process();
let _data = vec![1, 2, 3]; // This allocates memory
}
let report = session.to_report();
report.print_to_stdout();Sourcepub fn print_to_stdout(&self)
pub fn print_to_stdout(&self)
Prints the allocation statistics of all operations to stdout.
This is a convenience method equivalent to self.to_report().print_to_stdout().
Prints nothing if no spans were captured. This may indicate that the session
was part of a “list available benchmarks” probe run instead of some real activity,
in which case printing anything might violate the output protocol the tool is speaking.