Session

Struct Session 

Source
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

Source

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 dropped
Source

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
    }
}
Source

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();
Source

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.

Source

pub fn is_empty(&self) -> bool

Whether there is any recorded activity in this session.

Trait Implementations§

Source§

impl Debug for Session

Source§

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

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

impl Display for Session

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.