Skip to main content

Profiler

Struct Profiler 

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

The Profiler struct is used to trace events.

The events will be stored on disk, if the profiler instance is dropped.

Implementations§

Source§

impl Profiler

Source

pub fn from_path(path: impl Into<PathBuf>) -> Result<Self, Box<dyn Error>>

Creates a new Profiler with the given path.

The profiling results will be stored at <path>.events, <path>.strings, etc.

§Errors

The method will fail if the path does not exist or some other error occurrs while initializing the profiler.

Source

pub fn from_name(name: impl AsRef<str>) -> Result<Self, Box<dyn Error>>

Creates a new Profiler from a given application name.

The profiling results will be stored at ./trace/<name>-<pid>.events, etc.

§Errors

The method will fail if the path does not exist or some other error occurrs while initializing the profiler.

Examples found in repository?
examples/simple.rs (line 10)
9fn main() {
10    let profiler = Profiler::from_name("parser").expect("failed to create profiler");
11    profiler.enable();
12
13    let result = parse_expression(&profiler, "1234");
14    match result {
15        Ok(expr) => println!("got expr: {:?}", expr),
16        Err(msg) => eprintln!("failed to parse expr: {}", msg),
17    }
18}
Source

pub fn enable(&self)

Enables the Profiler.

A Profiler, that is created via from_path or from_name, is disabled by default so you have to enable it first using this method.

Examples found in repository?
examples/simple.rs (line 11)
9fn main() {
10    let profiler = Profiler::from_name("parser").expect("failed to create profiler");
11    profiler.enable();
12
13    let result = parse_expression(&profiler, "1234");
14    match result {
15        Ok(expr) => println!("got expr: {:?}", expr),
16        Err(msg) => eprintln!("failed to parse expr: {}", msg),
17    }
18}
Source

pub fn disable(&self)

Disables the Profiler.

This method will make the profiler stop recording any new events but will still write all stored results to disk if dropped.

Source

pub fn trace(&self, category: &str, label: &str) -> Guard<'_>

Starts profiling an event with the given category and label.

Examples found in repository?
examples/simple.rs (line 22)
20fn parse_expression(profiler: &Profiler, input: &str) -> Result<Expr, &'static str> {
21    // We will trace the `parse_expression` function using this.
22    let _p = profiler.trace("parsing", "expression");
23
24    input
25        .parse::<i64>()
26        .map(|num| Expr::Number(num))
27        .or_else(|_| {
28            if input.chars().all(char::is_alphabetic) {
29                Ok(Expr::Identifier(input.to_string()))
30            } else {
31                Err("invalid identifier")
32            }
33        })
34        .map_err(|_| "invalid expression")
35}

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, 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.