Crate gbench[][src]

This crate provides the tools to benchmark code for further analyzation using Chrome tracing.

Examples

  • Examples of using gbench basic functionality
use gbench::{instantiate, scope};

fn main() {
    // Istantiation of the global variables
    // It is needed at the top of every program that uses gbench
    // The folder that is specified is the folder where the data
    // will be saved
    instantiate!("target/bench");
    {
        // This macro creates a variable that starts benchmarking
        // on creation and saves the result on drop.
        // The variable name is the first argument, the scope name
        // is the second.
        scope!(sc | "Scope");

        for _ in 0..1_000_000 {
            let _a = 1 + 1;
        }
    }
}
  • Example of a log! macro use
use gbench::{instantiate, log, scope};

fn main() {
    instantiate!(ginst | "target/bench");
    {
        scope!(sc | "Scope");

        for _ in 0..1_000 {
            let a = 1 + 1;

            // You can log to the file with a timestamp
            // using log! macro
            log!("A = {}", a);
        }
    }
}
  • Full example
use std::thread;

use gbench::{instantiate, scope};

fn calculate(num: f32, n: u32) -> f32 {
    (0..n)
        .fold((num, 0.0), |(x, v), _| (x + v * 0.01, v - x * 0.001))
        .0
}

fn main() {
    instantiate!("target/bench");

    scope!(program_scope | "Program scope");

    // Doing the work that needs benchmarking
    for _ in 0..5 {
        scope!(main | "Main scope");

        // Spawning a thread to do work
        let thread = thread::spawn(move || {
            // This benchmarks the scope that it is in
            scope!(child | "Child");

            calculate(1.0, 1_500_000)
        });

        // You can organize your subtasks in scopes to
        // benchmark them
        scope!(imp | "An important task");

        {
            scope!(i1 | "Important subtask");
            calculate(1.0, 300_000);
        }

        {
            scope!(i2 | "Less important subtask");
            calculate(1.0, 500_000);
        }

        // If the block of code that you need to benchmark
        // has ended you can drop the guard if the scope
        // has not ended
        drop(imp);

        // Marking the start of another task
        scope!(join | "Joining thread");

        thread.join().unwrap();

        // This line of code is unnecessary but I like
        // to keep it
        drop(join);
    }
}

Macros

instantiate

A macro for instantiating the global environment for benchmark logging.

log

A macro for logging an event.

scope

A macro for benchmarking a scope of code

Structs

Instantiator

A sctruct used for instantiating global data.

TimeScope

A sctruct used for benchmarking scopes that it is in.