Skip to content

Commit

Permalink
Refine criterion a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoverbear committed Sep 4, 2018
1 parent 705cd30 commit cea2462
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 77 deletions.
10 changes: 10 additions & 0 deletions benches/benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 2,24 @@

#[macro_use]
extern crate criterion;
extern crate env_logger;
extern crate raft;

mod suites;
use suites::*;

pub const DEFAULT_RAFT_SETS: [(usize, usize); 4] = [(0, 0), (3, 1), (5, 2), (7, 3),];

criterion_main!(
bench_raft,
bench_raw_node,
bench_progress,
bench_progress_set,
);


/// Do any common test initialization. Eg set up logging, setup fail-rs.
#[cfg(test)]
pub fn setup_for_test() {
let _ = env_logger::try_init();
}
2 changes: 2 additions & 0 deletions benches/suites/progress.rs
Original file line number Diff line number Diff line change
@@ -1,9 1,11 @@
use criterion::{Bencher, Criterion};
use raft::Progress;
use setup_for_test;

criterion_group!(bench_progress, bench_progress_default);

pub fn bench_progress_default(c: &mut Criterion) {
setup_for_test();
let bench = |b: &mut Bencher| {
// No setup.
b.iter(|| Progress::default());
Expand Down
96 changes: 64 additions & 32 deletions benches/suites/progress_set.rs
Original file line number Diff line number Diff line change
@@ -1,5 1,6 @@
use criterion::{Bencher, Criterion};
use raft::ProgressSet;
use {setup_for_test, DEFAULT_RAFT_SETS};

criterion_group!(
bench_progress_set,
Expand All @@ -25,20 26,24 @@ fn quick_progress_set(voters: usize, learners: usize) -> ProgressSet {
}

pub fn bench_progress_set_new(c: &mut Criterion) {
setup_for_test();
let bench = |voters, learners| {
move |b: &mut Bencher| {
// No setup.
b.iter(|| ProgressSet::new(voters, learners));
}
};

c.bench_function("ProgressSet::new (0, 0)", bench(0, 0));
c.bench_function("ProgressSet::new (3, 1)", bench(3, 1));
c.bench_function("ProgressSet::new (5, 2)", bench(5, 2));
c.bench_function("ProgressSet::new (7, 3)", bench(7, 3));
DEFAULT_RAFT_SETS.iter().for_each(|(voters, learners)| {
c.bench_function(
&format!("ProgressSet::new ({}, {})", voters, learners),
bench(*voters, *learners),
);
});
}

pub fn bench_progress_set_insert_voter(c: &mut Criterion) {
setup_for_test();
let bench = |voters, learners| {
move |b: &mut Bencher| {
let set = quick_progress_set(voters, learners);
Expand All @@ -48,13 53,17 @@ pub fn bench_progress_set_insert_voter(c: &mut Criterion) {
});
}
};
c.bench_function("ProgressSet::insert_voter (0, 0)", bench(0, 0));
c.bench_function("ProgressSet::insert_voter (3, 1)", bench(3, 1));
c.bench_function("ProgressSet::insert_voter (5, 2)", bench(5, 2));
c.bench_function("ProgressSet::insert_voter (7, 3)", bench(7, 3));

DEFAULT_RAFT_SETS.iter().for_each(|(voters, learners)| {
c.bench_function(
&format!("ProgressSet::insert_voter ({}, {})", voters, learners),
bench(*voters, *learners),
);
});
}

pub fn bench_progress_set_insert_learner(c: &mut Criterion) {
setup_for_test();
let bench = |voters, learners| {
move |b: &mut Bencher| {
let set = quick_progress_set(voters, learners);
Expand All @@ -64,13 73,17 @@ pub fn bench_progress_set_insert_learner(c: &mut Criterion) {
});
}
};
c.bench_function("ProgressSet::insert_learner (0, 0)", bench(0, 0));
c.bench_function("ProgressSet::insert_learner (3, 1)", bench(3, 1));
c.bench_function("ProgressSet::insert_learner (5, 2)", bench(5, 2));
c.bench_function("ProgressSet::insert_learner (7, 3)", bench(7, 3));

DEFAULT_RAFT_SETS.iter().for_each(|(voters, learners)| {
c.bench_function(
&format!("ProgressSet::insert_learner ({}, {})", voters, learners),
bench(*voters, *learners),
);
});
}

pub fn bench_progress_set_remove(c: &mut Criterion) {
setup_for_test();
let bench = |voters, learners| {
move |b: &mut Bencher| {
let set = quick_progress_set(voters, learners);
Expand All @@ -80,13 93,17 @@ pub fn bench_progress_set_remove(c: &mut Criterion) {
});
}
};
c.bench_function("ProgressSet::remove (0, 0)", bench(0, 0));
c.bench_function("ProgressSet::remove (3, 1)", bench(3, 1));
c.bench_function("ProgressSet::remove (5, 2)", bench(5, 2));
c.bench_function("ProgressSet::remove (7, 3)", bench(7, 3));

DEFAULT_RAFT_SETS.iter().for_each(|(voters, learners)| {
c.bench_function(
&format!("ProgressSet::remove ({}, {})", voters, learners),
bench(*voters, *learners),
);
});
}

pub fn bench_progress_set_promote_learner(c: &mut Criterion) {
setup_for_test();
let bench = |voters, learners| {
move |b: &mut Bencher| {
let set = quick_progress_set(voters, learners);
Expand All @@ -96,13 113,17 @@ pub fn bench_progress_set_promote_learner(c: &mut Criterion) {
});
}
};
c.bench_function("ProgressSet::promote (0, 0)", bench(0, 0));
c.bench_function("ProgressSet::promote (3, 1)", bench(3, 1));
c.bench_function("ProgressSet::promote (5, 2)", bench(5, 2));
c.bench_function("ProgressSet::promote (7, 3)", bench(7, 3));

DEFAULT_RAFT_SETS.iter().for_each(|(voters, learners)| {
c.bench_function(
&format!("ProgressSet::promote ({}, {})", voters, learners),
bench(*voters, *learners),
);
});
}

pub fn bench_progress_set_iter(c: &mut Criterion) {
setup_for_test();
let bench = |voters, learners| {
move |b: &mut Bencher| {
let set = quick_progress_set(voters, learners);
Expand All @@ -113,13 134,17 @@ pub fn bench_progress_set_iter(c: &mut Criterion) {
});
}
};
c.bench_function("ProgressSet::iter (0, 0)", bench(0, 0));
c.bench_function("ProgressSet::iter (3, 1)", bench(3, 1));
c.bench_function("ProgressSet::iter (5, 2)", bench(5, 2));
c.bench_function("ProgressSet::iter (7, 3)", bench(7, 3));

DEFAULT_RAFT_SETS.iter().for_each(|(voters, learners)| {
c.bench_function(
&format!("ProgressSet::iter ({}, {})", voters, learners),
bench(*voters, *learners),
);
});
}

pub fn bench_progress_set_nodes(c: &mut Criterion) {
setup_for_test();
let bench = |voters, learners| {
move |b: &mut Bencher| {
let set = quick_progress_set(voters, learners);
Expand All @@ -130,13 155,17 @@ pub fn bench_progress_set_nodes(c: &mut Criterion) {
});
}
};
c.bench_function("ProgressSet::nodes (0, 0)", bench(0, 0));
c.bench_function("ProgressSet::nodes (3, 1)", bench(3, 1));
c.bench_function("ProgressSet::nodes (5, 2)", bench(5, 2));
c.bench_function("ProgressSet::nodes (7, 3)", bench(7, 3));

DEFAULT_RAFT_SETS.iter().for_each(|(voters, learners)| {
c.bench_function(
&format!("ProgressSet::nodes ({}, {})", voters, learners),
bench(*voters, *learners),
);
});
}

pub fn bench_progress_set_get(c: &mut Criterion) {
setup_for_test();
let bench = |voters, learners| {
move |b: &mut Bencher| {
let set = quick_progress_set(voters, learners);
Expand All @@ -148,8 177,11 @@ pub fn bench_progress_set_get(c: &mut Criterion) {
});
}
};
c.bench_function("ProgressSet::get (0, 0)", bench(0, 0));
c.bench_function("ProgressSet::get (3, 1)", bench(3, 1));
c.bench_function("ProgressSet::get (5, 2)", bench(5, 2));
c.bench_function("ProgressSet::get (7, 3)", bench(7, 3));

DEFAULT_RAFT_SETS.iter().for_each(|(voters, learners)| {
c.bench_function(
&format!("ProgressSet::get ({}, {})", voters, learners),
bench(*voters, *learners),
);
});
}
69 changes: 24 additions & 45 deletions benches/suites/raft.rs
Original file line number Diff line number Diff line change
@@ -1,5 1,6 @@
use criterion::{Bencher, Criterion};
use raft::{storage::MemStorage, Config, Raft};
use {setup_for_test, DEFAULT_RAFT_SETS};

criterion_group!(bench_raft, bench_raft_new, bench_raft_campaign,);

Expand All @@ -18,20 19,24 @@ fn quick_raft(voters: usize, learners: usize) -> Raft<MemStorage> {
}

pub fn bench_raft_new(c: &mut Criterion) {
setup_for_test();
let bench = |voters, learners| {
move |b: &mut Bencher| {
// No setup.
b.iter(|| quick_raft(voters, learners));
}
};

c.bench_function("Raft::new (0, 0)", bench(0, 0));
c.bench_function("Raft::new (3, 1)", bench(3, 1));
c.bench_function("Raft::new (5, 2)", bench(5, 2));
c.bench_function("Raft::new (7, 3)", bench(7, 3));
DEFAULT_RAFT_SETS.iter().for_each(|(voters, learners)| {
c.bench_function(
&format!("Raft::new ({}, {})", voters, learners),
bench(*voters, *learners),
);
});
}

pub fn bench_raft_campaign(c: &mut Criterion) {
setup_for_test();
let bench = |voters, learners, variant| {
move |b: &mut Bencher| {
b.iter(|| {
Expand All @@ -42,45 47,19 @@ pub fn bench_raft_campaign(c: &mut Criterion) {
}
};

// We don't want to make `raft::raft` public at this point.
let pre_election = b"CampaignPreElection";
let election = b"CampaignElection";
let transfer = b"CampaignTransfer";

c.bench_function(
"Raft::campaign (3, 1, pre_election)",
bench(3, 1, &pre_election[..]),
);
c.bench_function(
"Raft::campaign (3, 1, election)",
bench(3, 1, &election[..]),
);
c.bench_function(
"Raft::campaign (3, 1, transfer)",
bench(3, 1, &transfer[..]),
);
c.bench_function(
"Raft::campaign (5, 2, pre_election)",
bench(5, 2, &pre_election[..]),
);
c.bench_function(
"Raft::campaign (5, 2, election)",
bench(5, 2, &election[..]),
);
c.bench_function(
"Raft::campaign (5, 2, transfer)",
bench(5, 2, &transfer[..]),
);
c.bench_function(
"Raft::campaign (7, 3, pre_election)",
bench(7, 3, &pre_election[..]),
);
c.bench_function(
"Raft::campaign (7, 3, election)",
bench(7, 3, &election[..]),
);
c.bench_function(
"Raft::campaign (7, 3, transfer)",
bench(7, 3, &transfer[..]),
);
DEFAULT_RAFT_SETS.iter().skip(1).for_each(|(voters, learners)| {
// We don't want to make `raft::raft` public at this point.
let msgs = [
"CampaignPreElection",
"CampaignElection",
"CampaignTransfer",
];
// Skip the first since it's 0,0
for msg in msgs.iter() {
c.bench_function(
&format!("Raft::campaign ({}, {}, {})", voters, learners, msg),
bench(*voters, *learners, msg.as_bytes()),
);
}
});
}
2 changes: 2 additions & 0 deletions benches/suites/raw_node.rs
Original file line number Diff line number Diff line change
@@ -1,5 1,6 @@
use criterion::{Bencher, Criterion};
use raft::{storage::MemStorage, Config, RawNode};
use setup_for_test;

criterion_group!(bench_raw_node, bench_raw_node_new,);

Expand All @@ -13,6 14,7 @@ fn quick_raw_node() -> RawNode<MemStorage> {
}

pub fn bench_raw_node_new(c: &mut Criterion) {
setup_for_test();
let bench = |b: &mut Bencher| {
// No setup.
b.iter(|| quick_raw_node());
Expand Down

0 comments on commit cea2462

Please sign in to comment.