Benchmark for Rust and humans
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.
Let us create a simple benchmark, using crate macros only:
```rust.ignore
extern crate bma_benchmark;
use std::sync::Mutex;
let n = 100000000; let mutex = Mutex::new(0); benchmarkstart!(); for _ in 0..n { let _a = mutex.lock().unwrap(); } benchmarkprint!(n); ```
The same can also be done with a single "benchmark" macro:
```rust,ignore
extern crate bma_benchmark;
use std::sync::Mutex;
let mutex = Mutex::new(0); benchmark!(100000000, { let _a = mutex.lock().unwrap(); }); ```
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.
```rust,ignore
extern crate bma_benchmark;
use std::sync::{Mutex, RwLock};
let n = 10000000; let mutex = Mutex::new(0); let rwlock = RwLock::new(0); stagedbenchmarkstart!("mutex"); for _ in 0..n { let a = mutex.lock().unwrap(); } stagedbenchmarkfinishcurrent!(n); stagedbenchmarkstart!("rwlock-read"); for _ in 0..n { let a = rwlock.read().unwrap(); } stagedbenchmarkfinishcurrent!(n); stagedbenchmarkprint_for!("rwlock-read"); ```
The same can also be done with a couple of staged_benchmark macros:
```rust,ignore
extern crate bma_benchmark;
use std::sync::{Mutex, RwLock};
let n = 10000000; let mutex = Mutex::new(0); let rwlock = RwLock::new(0); stagedbenchmark!("mutex", n, { let _a = mutex.lock().unwrap(); }); stagedbenchmark!("rwlock-read", n, { let a = rwlock.read().unwrap(); }); stagedbenchmarkprintfor!("rwlock-read"); ```
Or split into functions with benchmark_stage attributes:
```rust,ignore use std::sync::{Mutex, RwLock};
extern crate bma_benchmark;
fn benchmark_mutex(mutex: Mutex
fn benchmark_rwlock(rwlock: RwLock
let mutex = Mutex::new(0); let rwlock = RwLock::new(0); benchmarkmutex(mutex); benchmarkrwlock(rwlock); stagedbenchmarkprint_for!("rwlock-read"); ```
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:
```rust.ignore
extern crate bma_benchmark;
use std::sync::Mutex;
let mutex = Mutex::new(0); benchmarkcheck!(10000000, { mutex.lock().isok() }); ```
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:
```rust,ignore use bma_benchmark::LatencyBenchmark;
let mut lb = LatencyBenchmark::new(); for _ in 0..1000 { lb.opstart(); lb.opfinish(); } lb.print(); ```
ignore
latency (μs) avg: 883, min: 701, max: 1_165
Need anything more complex? Check the crate docs and use structures manually.
Enjoy!