This crate provides the tools to benchmark code for
further analyzation using different tools.
Writers System
This crate uses writers system to manage collected data.
This means that all the macros that collect data during
program running push this data in form of BenchData enums
to a shared storage. At the end of the program all the data is given
to the instances of Writer that are given at the initialization.
Examples
Examples of using gbench basic functionality
use gbench::{instantiate, scope, ChromeTracing};
fn main() {
instantiate!(ChromeTracing("target/bench"));
{
scope!(sc | "Scope");
for _ in 0..1_000_000 {
let _a = 1 + 1;
}
}
}
Example of a log! macro use
use gbench::{instantiate, log, scope, ChromeTracing};
fn main() {
instantiate!(ChromeTracing("target/bench"));
{
scope!(sc | "Scope");
for _ in 0..1_000 {
let a = 1 + 1;
log!("A = {}", a);
}
}
}
Example of a count! macro and CsvWriter writer use
use gbench::{count, instantiate, scope, ChromeTracing, CsvWriter};
fn main() {
instantiate!(ChromeTracing("target/bench"), CsvWriter("target/bench"));
{
scope!(sc | "Scope");
for i in 0..1000 {
let val = i * i;
count! {
"Actual value" => {
"val" => val
},
"I" => {
"i" => i
}
}
}
}
}
Full example
use gbench::{instantiate, scope, ChromeTracing};
use std::thread;
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!(ChromeTracing("target/bench"));
scope!(program_scope | "Program scope");
for _ in 0..5 {
scope!(main | "Main scope");
let thread = thread::spawn(move || {
scope!(child | "Child");
calculate(1.0, 1_500_000)
});
scope!(imp | "An important task");
{
scope!(i1 | "Important subtask");
calculate(1.0, 300_000);
}
{
scope!(i2 | "Less important subtask");
calculate(1.0, 500_000);
}
drop(imp);
scope!(join | "Joining thread");
thread.join().unwrap();
drop(join);
}
}