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 5, 2018
1 parent 2442bef commit c6dc9d7
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 100 deletions.
31 changes: 22 additions & 9 deletions benches/benches.rs
Original file line number Diff line number Diff line change
@@ -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();
}
4 changes: 3 additions & 1 deletion benches/suites/progress.rs
Original file line number Diff line number Diff line change
@@ -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| {
Expand Down
109 changes: 66 additions & 43 deletions benches/suites/progress_set.rs
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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),
);
});
}
75 changes: 29 additions & 46 deletions benches/suites/raft.rs
Original file line number Diff line number Diff line change
@@ -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<MemStorage> {
let id = 1;
Expand All @@ -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) {
Expand All @@ -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()),
);
}
});
}
4 changes: 3 additions & 1 deletion benches/suites/raw_node.rs
Original file line number Diff line number Diff line change
@@ -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<MemStorage> {
let id = 1;
Expand Down

0 comments on commit c6dc9d7

Please sign in to comment.