Skip to content

Use a macro for defining benchmarks #1464

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions collector/benchlib/src/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,24 @@ fn run_benchmark(args: BenchmarkArgs, benchmarks: BenchmarkMap) -> anyhow::Resul
Ok(())
}

/// Adds a single benchmark to the benchmark suite.
/// ```ignore
/// use benchlib::define_benchmark;
///
/// define_benchmark!(suite, my_bench, {
/// || do_something()
/// });
/// ```
#[macro_export]
macro_rules! define_benchmark {
($suite:expr, $name:ident, $fun:expr) => {
let func = move || $fun;
$suite.register(stringify!($name), func);
};
}

pub use define_benchmark;

/// Tests if the name of the benchmark passes through the include and exclude filter flags.
fn passes_filter(name: &str, exclude: Option<&str>, include: Option<&str>) -> bool {
match (exclude, include) {
Expand Down
44 changes: 23 additions & 21 deletions collector/runtime-benchmarks/bufreader/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,36 @@
use benchlib::benchmark::{black_box, BenchmarkSuite};
use snap::{read::FrameDecoder, write::FrameEncoder};
use std::io::{BufRead, BufReader, Write};

use snap::{read::FrameDecoder, write::FrameEncoder};

use benchlib::benchmark::{benchmark_suite, black_box};
use benchlib::define_benchmark;

const BYTES: usize = 64 * 1024 * 1024;

fn main() {
let mut suite = BenchmarkSuite::new();

// Inspired by https://github.com/rust-lang/rust/issues/102727
// The pattern we want is a BufReader which wraps a Read impl where one Read::read call will
// never fill the whole BufReader buffer.
suite.register("bufreader-snappy", || {
let data = vec![0u8; BYTES];
move || {
let mut compressed = Vec::new();
FrameEncoder::new(&mut compressed)
.write_all(&data[..])
.unwrap();
let mut reader = BufReader::with_capacity(BYTES, FrameDecoder::new(&compressed[..]));
benchmark_suite(|suite| {
define_benchmark!(suite, bufreader_snappy, {
let data = vec![0u8; BYTES];
move || {
let mut compressed = Vec::new();
FrameEncoder::new(&mut compressed)
.write_all(&data[..])
.unwrap();
let mut reader =
BufReader::with_capacity(BYTES, FrameDecoder::new(&compressed[..]));

while let Ok(buf) = reader.fill_buf() {
if buf.is_empty() {
break;
while let Ok(buf) = reader.fill_buf() {
if buf.is_empty() {
break;
}
black_box(buf);
let len = buf.len();
reader.consume(len);
}
black_box(buf);
let len = buf.len();
reader.consume(len);
}
}
});
});

suite.run().unwrap();
}
26 changes: 14 additions & 12 deletions collector/runtime-benchmarks/hashmap/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
use benchlib;
use benchlib::benchmark::BenchmarkSuite;
use benchlib::benchmark::benchmark_suite;
use benchlib::define_benchmark;

fn main() {
let mut suite = BenchmarkSuite::new();

/// Measures how long does it take to insert 10 thousand numbers into a `hashbrown` hashmap.
suite.register("hashmap-insert-10k", || {
let mut map =
hashbrown::HashMap::with_capacity_and_hasher(10000, fxhash::FxBuildHasher::default());
move || {
for index in 0..10000 {
map.insert(index, index);
benchmark_suite(|suite| {
// Measures how long does it take to insert 10 thousand numbers into a `hashbrown` hashmap.
define_benchmark!(suite, hashmap_insert_10k, {
let mut map = hashbrown::HashMap::with_capacity_and_hasher(
10000,
fxhash::FxBuildHasher::default(),
);
move || {
for index in 0..10000 {
map.insert(index, index);
}
}
}
});
});
suite.run().unwrap();
}
3 changes: 2 additions & 1 deletion collector/runtime-benchmarks/nbody/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
//! Code taken from https://github.com/prestontw/rust-nbody

use benchlib::benchmark::benchmark_suite;
use benchlib::define_benchmark;

mod nbody;

fn main() {
benchmark_suite(|suite| {
suite.register("nbody-10k", || {
define_benchmark!(suite, nbody_10k, {
let mut nbody_10k = nbody::init(10000);
|| {
for _ in 0..10 {
Expand Down