From c6dc9d7099a827975beee944823448564be6caf0 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 | 31 +++++++--- benches/suites/progress.rs | 4 +- benches/suites/progress_set.rs | 109 ++++++++++++++++++++------------- benches/suites/raft.rs | 75 +++++++++-------------- benches/suites/raw_node.rs | 4 +- 5 files changed, 123 insertions(+), 100 deletions(-) diff --git a/benches/benches.rs b/benches/benches.rs index 501fd1edf..6b034e269 100644 --- a/benches/benches.rs +++ b/benches/benches.rs @@ -1,15 +1,28 @@ #![allow(dead_code)] // Due to criterion we need this to avoid warnings. -#[macro_use] extern crate criterion; +extern crate env_logger; extern crate raft; +use criterion::Criterion; +use std::time::Duration; + mod suites; -use suites::*; - -criterion_main!( - bench_raft, - bench_raw_node, - bench_progress, - bench_progress_set, -); + +pub const DEFAULT_RAFT_SETS: [(usize, usize); 4] = [(0, 0), (3, 1), (5, 2), (7, 3)]; + +fn main() { + criterion::init_logging(); + let mut c = Criterion::default() + // Configure defaults before overriding with args. + .warm_up_time(Duration::from_millis(500)) + .measurement_time(Duration::from_secs(1)) + .configure_from_args(); + + suites::bench_raft(&mut c); + suites::bench_raw_node(&mut c); + suites::bench_progress(&mut c); + suites::bench_progress_set(&mut c); + + c.final_summary(); +} diff --git a/benches/suites/progress.rs b/benches/suites/progress.rs index 030ef506c..8de69e6c2 100644 --- a/benches/suites/progress.rs +++ b/benches/suites/progress.rs @@ -1,7 +1,9 @@ use criterion::{Bencher, Criterion}; use raft::Progress; -criterion_group!(bench_progress, bench_progress_default); +pub fn bench_progress(c: &mut Criterion) { + bench_progress_default(c); +} pub fn bench_progress_default(c: &mut Criterion) { let bench = |b: &mut Bencher| { diff --git a/benches/suites/progress_set.rs b/benches/suites/progress_set.rs index 0b14e8ceb..9490a7d55 100644 --- a/benches/suites/progress_set.rs +++ b/benches/suites/progress_set.rs @@ -1,17 +1,17 @@ use criterion::{Bencher, Criterion}; use raft::ProgressSet; +use DEFAULT_RAFT_SETS; -criterion_group!( - bench_progress_set, - bench_progress_set_new, - bench_progress_set_insert_voter, - bench_progress_set_insert_learner, - bench_progress_set_promote_learner, - bench_progress_set_remove, - bench_progress_set_iter, - bench_progress_set_get, - bench_progress_set_nodes, -); +pub fn bench_progress_set(c: &mut Criterion) { + bench_progress_set_new(c); + bench_progress_set_insert_voter(c); + bench_progress_set_insert_learner(c); + bench_progress_set_promote_learner(c); + bench_progress_set_remove(c); + bench_progress_set_iter(c); + bench_progress_set_get(c); + bench_progress_set_nodes(c); +} fn quick_progress_set(voters: usize, learners: usize) -> ProgressSet { let mut set = ProgressSet::new(voters, learners); @@ -32,10 +32,12 @@ 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) { @@ -48,10 +50,13 @@ 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) { @@ -64,10 +69,13 @@ 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) { @@ -80,10 +88,13 @@ 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) { @@ -96,10 +107,13 @@ 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) { @@ -113,10 +127,13 @@ 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) { @@ -130,10 +147,13 @@ 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) { @@ -148,8 +168,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..30d7213b9 100644 --- a/benches/suites/raft.rs +++ b/benches/suites/raft.rs @@ -1,7 +1,11 @@ use criterion::{Bencher, Criterion}; use raft::{storage::MemStorage, Config, Raft}; +use DEFAULT_RAFT_SETS; -criterion_group!(bench_raft, bench_raft_new, bench_raft_campaign,); +pub fn bench_raft(c: &mut Criterion) { + bench_raft_new(c); + bench_raft_campaign(c); +} fn quick_raft(voters: usize, learners: usize) -> Raft { let id = 1; @@ -25,10 +29,12 @@ 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) { @@ -42,45 +48,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..3760463d3 100644 --- a/benches/suites/raw_node.rs +++ b/benches/suites/raw_node.rs @@ -1,7 +1,9 @@ use criterion::{Bencher, Criterion}; use raft::{storage::MemStorage, Config, RawNode}; -criterion_group!(bench_raw_node, bench_raw_node_new,); +pub fn bench_raw_node(c: &mut Criterion) { + bench_raw_node_new(c); +} fn quick_raw_node() -> RawNode { let id = 1;