#![allow(
missing_docs,
reason = "the criterion_group macro generates the bench entry point, which cannot carry a doc comment"
)]
use std::collections::BTreeMap;
use std::hint::black_box;
use criterion::{BatchSize, Criterion, Throughput, criterion_group, criterion_main};
use simraft_core::{
Action, ClientId, ClusterConfig, Config, Event, LogIndex, Message, Node, NodeId, PersistKind,
Role,
};
fn cluster() -> BTreeMap<NodeId, Node> {
let cfg = ClusterConfig::new([NodeId(1), NodeId(2), NodeId(3)], Vec::new())
.expect("config validates");
let mut nodes = BTreeMap::new();
for id in [1_u64, 2, 3] {
nodes.insert(
NodeId(id),
Node::new(NodeId(id), Config::default(), cfg.clone(), 7).expect("node builds"),
);
}
nodes
}
fn elect(nodes: &mut BTreeMap<NodeId, Node>, id: NodeId) {
let node = nodes.get_mut(&id).expect("node exists");
node.step(Event::Tick { now: 1_000_000 });
for peer in [NodeId(2), NodeId(3)] {
let term = node.current_term();
node.step(Event::Message {
from: peer,
msg: Message::RequestPreVoteResponse {
term,
vote_granted: true,
},
});
if node.role() == Role::Candidate {
break;
}
}
node.step(Event::Persisted {
kind: PersistKind::HardState,
up_to_index: LogIndex::ZERO,
});
for peer in [NodeId(2), NodeId(3)] {
let term = node.current_term();
node.step(Event::Message {
from: peer,
msg: Message::RequestVoteResponse {
term,
vote_granted: true,
},
});
if node.role() == Role::Leader {
break;
}
}
assert_eq!(node.role(), Role::Leader, "the bench cluster elects");
let last = node.log().last_index();
node.step(Event::Persisted {
kind: PersistKind::Entries,
up_to_index: last,
});
}
fn route(nodes: &mut BTreeMap<NodeId, Node>, from: NodeId, actions: Vec<Action>) {
let mut queue: Vec<(NodeId, Event)> = Vec::new();
let collect = |queue: &mut Vec<(NodeId, Event)>, source: NodeId, actions: Vec<Action>| {
for action in actions {
match action {
Action::Send { to, msg } => {
queue.push((to, Event::Message { from: source, msg }));
}
Action::PersistHardState { .. } => queue.push((
source,
Event::Persisted {
kind: PersistKind::HardState,
up_to_index: LogIndex::ZERO,
},
)),
Action::PersistEntries { .. } => queue.push((
source,
Event::Persisted {
kind: PersistKind::Entries,
up_to_index: LogIndex::ZERO,
},
)),
_ => {}
}
}
};
collect(&mut queue, from, actions);
while let Some((to, event)) = queue.pop() {
let event = match event {
Event::Persisted {
kind: PersistKind::Entries,
..
} => Event::Persisted {
kind: PersistKind::Entries,
up_to_index: nodes.get(&to).expect("node").log().last_index(),
},
other => other,
};
let actions = nodes.get_mut(&to).expect("node exists").step(event);
collect(&mut queue, to, actions);
}
}
fn bench_replication(c: &mut Criterion) {
{
let mut group = c.benchmark_group("replication");
group.throughput(Throughput::Elements(1));
group.bench_function("propose_commit_round", |b| {
let mut nodes = cluster();
elect(&mut nodes, NodeId(1));
let mut seq = 0_u64;
b.iter(|| {
seq += 1;
let actions =
nodes
.get_mut(&NodeId(1))
.expect("leader exists")
.step(Event::Propose {
client_id: ClientId(1),
seq,
payload: black_box(vec![1_u8; 32]),
});
route(&mut nodes, NodeId(1), actions);
black_box(nodes.get(&NodeId(1)).expect("leader").commit_index())
});
});
group.finish();
}
}
fn bench_step_latency(c: &mut Criterion) {
let mut group = c.benchmark_group("step_latency");
group.bench_function("follower_append_entries", |b| {
let mut nodes = cluster();
elect(&mut nodes, NodeId(1));
nodes
.get_mut(&NodeId(1))
.expect("leader exists")
.step(Event::Propose {
client_id: ClientId(1),
seq: 1,
payload: vec![9_u8; 32],
});
let last = nodes
.get(&NodeId(1))
.expect("leader exists")
.log()
.last_index();
let actions = nodes
.get_mut(&NodeId(1))
.expect("leader exists")
.step(Event::Persisted {
kind: PersistKind::Entries,
up_to_index: last,
});
let message = actions
.iter()
.find_map(|a| match a {
Action::Send {
to,
msg: m @ Message::AppendEntries { .. },
} if *to == NodeId(2) => Some(m.clone()),
_ => None,
})
.expect("the leader sends AppendEntries");
b.iter(|| {
let out = nodes
.get_mut(&NodeId(2))
.expect("follower")
.step(Event::Message {
from: NodeId(1),
msg: black_box(message.clone()),
});
black_box(out.len())
});
});
group.finish();
}
fn bench_replication_batched(c: &mut Criterion) {
{
let mut group = c.benchmark_group("replication_batched");
group.throughput(Throughput::Elements(32));
group.bench_function("propose_commit_batch_32", |b| {
let mut nodes = cluster();
elect(&mut nodes, NodeId(1));
let mut seq = 0_u64;
b.iter(|| {
let mut actions = Vec::new();
for _ in 0..32 {
seq += 1;
actions.extend(nodes.get_mut(&NodeId(1)).expect("leader exists").step(
Event::Propose {
client_id: ClientId(1),
seq,
payload: black_box(vec![1_u8; 32]),
},
));
}
route(&mut nodes, NodeId(1), actions);
black_box(nodes.get(&NodeId(1)).expect("leader").commit_index())
});
});
group.finish();
}
}
fn bench_replication_catch_up(c: &mut Criterion) {
{
let mut group = c.benchmark_group("replication_catch_up");
group.throughput(Throughput::Elements(10_000));
group.sample_size(10);
group.bench_function("catch_up_ten_thousand_entries", |b| {
b.iter_batched(
|| {
let mut nodes = cluster();
elect(&mut nodes, NodeId(1));
let term = nodes.get(&NodeId(1)).expect("leader exists").current_term();
nodes
.get_mut(&NodeId(1))
.expect("leader exists")
.step(Event::Message {
from: NodeId(2),
msg: Message::AppendEntriesResponse {
term,
success: true,
match_index: LogIndex(1),
conflict_index: LogIndex::ZERO,
conflict_term: simraft_core::Term::ZERO,
read_round: 0,
},
});
for seq in 1..=10_000_u64 {
let leader = nodes.get_mut(&NodeId(1)).expect("leader exists");
leader.step(Event::Propose {
client_id: ClientId(1),
seq,
payload: vec![1_u8; 32],
});
let last = leader.log().last_index();
leader.step(Event::Persisted {
kind: PersistKind::Entries,
up_to_index: last,
});
}
nodes
},
|mut nodes| {
let target = nodes
.get(&NodeId(1))
.expect("leader exists")
.log()
.last_index();
let mut now = 1_000_000_u64;
for _ in 0..10_000 {
now += 50;
let actions = nodes
.get_mut(&NodeId(1))
.expect("leader exists")
.step(Event::Tick { now });
route(&mut nodes, NodeId(1), actions);
if nodes.get(&NodeId(3)).expect("follower").log().last_index() == target {
break;
}
}
black_box(nodes.get(&NodeId(3)).expect("follower").log().last_index())
},
BatchSize::SmallInput,
);
});
group.finish();
}
}
criterion_group!(
benches,
bench_replication,
bench_step_latency,
bench_replication_batched,
bench_replication_catch_up
);
criterion_main!(benches);