From e9a234005c59c009183416b90db061e11e6ba6ee Mon Sep 17 00:00:00 2001 From: Hoverbear Date: Tue, 4 Sep 2018 12:24:49 -0700 Subject: [PATCH] Refine criterion a bit --- benches/benches.rs | 9 ++++ benches/suites/progress.rs | 2 + benches/suites/progress_set.rs | 96 ++++++++++++++++++++++------------ benches/suites/raft.rs | 72 ++++++++++--------------- benches/suites/raw_node.rs | 2 + 5 files changed, 104 insertions(+), 77 deletions(-) diff --git a/benches/benches.rs b/benches/benches.rs index 501fd1edf..4f62ab043 100644 --- a/benches/benches.rs +++ b/benches/benches.rs @@ -2,14 +2,23 @@ #[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(); +} diff --git a/benches/suites/progress.rs b/benches/suites/progress.rs index 030ef506c..407bb2c19 100644 --- a/benches/suites/progress.rs +++ b/benches/suites/progress.rs @@ -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()); diff --git a/benches/suites/progress_set.rs b/benches/suites/progress_set.rs index 0b14e8ceb..cc40b86fe 100644 --- a/benches/suites/progress_set.rs +++ b/benches/suites/progress_set.rs @@ -1,5 +1,6 @@ use criterion::{Bencher, Criterion}; use raft::ProgressSet; +use {setup_for_test, DEFAULT_RAFT_SETS}; criterion_group!( bench_progress_set, @@ -25,6 +26,7 @@ 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. @@ -32,13 +34,16 @@ pub fn bench_progress_set_new(c: &mut Criterion) { } }; - 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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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), + ); + }); } diff --git a/benches/suites/raft.rs b/benches/suites/raft.rs index 3e38a4a9c..49b47284f 100644 --- a/benches/suites/raft.rs +++ b/benches/suites/raft.rs @@ -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,); @@ -18,6 +19,7 @@ fn quick_raft(voters: usize, learners: usize) -> Raft { } pub fn bench_raft_new(c: &mut Criterion) { + setup_for_test(); let bench = |voters, learners| { move |b: &mut Bencher| { // No setup. @@ -25,13 +27,16 @@ pub fn bench_raft_new(c: &mut Criterion) { } }; - 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(|| { @@ -42,45 +47,22 @@ 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()), + ); + } + }); } diff --git a/benches/suites/raw_node.rs b/benches/suites/raw_node.rs index bbc55d0f3..32996c7fb 100644 --- a/benches/suites/raw_node.rs +++ b/benches/suites/raw_node.rs @@ -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,); @@ -13,6 +14,7 @@ fn quick_raw_node() -> RawNode { } pub fn bench_raw_node_new(c: &mut Criterion) { + setup_for_test(); let bench = |b: &mut Bencher| { // No setup. b.iter(|| quick_raw_node());