#benchmark

bma-benchmark

Benchmark for Rust and humans

20 releases

0.0.20 Jan 1, 2023
0.0.19 Aug 26, 2022
0.0.18 Dec 24, 2021
0.0.17 Sep 14, 2021

#43 in Profiling

Download history 17/week @ 2023-06-05 15/week @ 2023-06-12 87/week @ 2023-06-19 32/week @ 2023-06-26 40/week @ 2023-07-03 32/week @ 2023-07-10 26/week @ 2023-07-17 55/week @ 2023-07-24 15/week @ 2023-07-31 22/week @ 2023-08-07 51/week @ 2023-08-14 11/week @ 2023-08-21 62/week @ 2023-08-28 53/week @ 2023-09-04 25/week @ 2023-09-11 23/week @ 2023-09-18

163 downloads per month
Used in 4 crates

MIT license

80KB
517 lines

bma-benchmark

Benchmark for Rust and humans

What is this for

I like testing different libraries, crates and algorithms. I do benchmarks on prototypes almost every day and decided to make a simple dedicated crate for that. Here we go: https://crates.io/crates/bma-benchmark

The benchmark engine is very simple to launch and outputs all required data in a pretty colored readable format.

How to use

Let us create a simple benchmark, using crate macros only:

#[macro_use]
extern crate bma_benchmark;

use std::sync::Mutex;

let n = 100_000_000;
let mutex = Mutex::new(0);
benchmark_start!();
for _ in 0..n {
    let _a = mutex.lock().unwrap();
}
benchmark_print!(n);

The same can also be done with a single "benchmark" macro:

#[macro_use]
extern crate bma_benchmark;

use std::sync::Mutex;

let mutex = Mutex::new(0);
benchmark!(100_000_000, {
    let _a = mutex.lock().unwrap();
    });

Simple benchmark result

Pretty cool, isn't it? Let us create a more complex staged benchmark and compare e.g. Mutex vs RwLock. Staged benchmarks display a comparison table. If the reference stage is specified, the table also contains speed difference for all others.

#[macro_use]
extern crate bma_benchmark;

use std::sync::{Mutex, RwLock};

let n = 10_000_000;
let mutex = Mutex::new(0);
let rwlock = RwLock::new(0);
staged_benchmark_start!("mutex");
for _ in 0..n {
    let _a = mutex.lock().unwrap();
}
staged_benchmark_finish_current!(n);
staged_benchmark_start!("rwlock-read");
for _ in 0..n {
    let _a = rwlock.read().unwrap();
}
staged_benchmark_finish_current!(n);
staged_benchmark_print_for!("rwlock-read");

The same can also be done with a couple of staged_benchmark macros:

#[macro_use]
extern crate bma_benchmark;

use std::sync::{Mutex, RwLock};

let n = 10_000_000;
let mutex = Mutex::new(0);
let rwlock = RwLock::new(0);
staged_benchmark!("mutex", n, {
    let _a = mutex.lock().unwrap();
});
staged_benchmark!("rwlock-read", n, {
    let _a = rwlock.read().unwrap();
});
staged_benchmark_print_for!("rwlock-read");

Or split into functions with benchmark_stage attributes:

use std::sync::{Mutex, RwLock};

#[macro_use]
extern crate bma_benchmark;

#[benchmark_stage(i=10_000_000)]
fn benchmark_mutex(mutex: Mutex<u64>) {
    let _a = mutex.lock().unwrap();
}

#[benchmark_stage(i=10_000_000,name="rwlock-read")]
fn benchmark_rwlock(rwlock: RwLock<u64>) {
    let _a = rwlock.read().unwrap();
}

let mutex = Mutex::new(0);
let rwlock = RwLock::new(0);
benchmark_mutex(mutex);
benchmark_rwlock(rwlock);
staged_benchmark_print_for!("rwlock-read");

Simple benchmark result

Errors

The macros benchmark_print, staged_benchmark_finish and staged_benchmark_finish_current accept error count as an additional parameter.

For code blocks, macros benchmark_check and staged_benchmark_check can be used. In this case, a statement MUST return true for the normal execution and false for errors:

#[macro_use]
extern crate bma_benchmark;

use std::sync::Mutex;

let mutex = Mutex::new(0);
benchmark_check!(10_000_000, {
    mutex.lock().is_ok()
    });

The benchmark_stage attribute has check option, which behaves similarly. If used, the function body MUST (not return but) END with a bool as well.

If any errors are reported, additional columns appear, success count, error count and error rate:

Simple benchmark result

Latency benchmarks

use bma_benchmark::LatencyBenchmark;

let mut lb = LatencyBenchmark::new();
for _ in 0..1000 {
    lb.op_start();
    // do something
    lb.op_finish();
}
lb.print();
latency (μs) avg: 883, min: 701, max: 1_165

Need anything more complex? Check the crate docs and use structures manually.

Enjoy!

Dependencies

~3–14MB
~162K SLoC