Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chain microbenchmarks: run the chains once before benching to avoid measuring init time #1668

Merged
merged 2 commits into from
Jun 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 41 additions & 37 deletions shotover/benches/benches/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ fn criterion_benchmark(c: &mut Criterion) {

group.bench_function("loopback", |b| {
b.to_async(&rt).iter_batched(
|| BenchInput {
chain: chain.build(TransformContextBuilder::new_test()),
wrapper: wrapper.clone(),
},
|| BenchInput::new_pre_used(&chain, &wrapper),
BenchInput::bench,
BatchSize::SmallInput,
)
Expand All @@ -58,10 +55,7 @@ fn criterion_benchmark(c: &mut Criterion) {

group.bench_function("nullsink", |b| {
b.to_async(&rt).iter_batched(
|| BenchInput {
chain: chain.build(TransformContextBuilder::new_test()),
wrapper: wrapper.clone(),
},
|| BenchInput::new_pre_used(&chain, &wrapper),
BenchInput::bench,
BatchSize::SmallInput,
)
Expand Down Expand Up @@ -96,10 +90,7 @@ fn criterion_benchmark(c: &mut Criterion) {

group.bench_function("redis_filter", |b| {
b.to_async(&rt).iter_batched(
|| BenchInput {
chain: chain.build(TransformContextBuilder::new_test()),
wrapper: wrapper.clone(),
},
|| BenchInput::new_pre_used(&chain, &wrapper),
BenchInput::bench,
BatchSize::SmallInput,
)
Expand All @@ -125,10 +116,7 @@ fn criterion_benchmark(c: &mut Criterion) {

group.bench_function("redis_cluster_ports_rewrite", |b| {
b.to_async(&rt).iter_batched(
|| BenchInput {
chain: chain.build(TransformContextBuilder::new_test()),
wrapper: wrapper.clone(),
},
|| BenchInput::new_pre_used(&chain, &wrapper),
BenchInput::bench,
BatchSize::SmallInput,
)
Expand Down Expand Up @@ -172,10 +160,7 @@ fn criterion_benchmark(c: &mut Criterion) {

group.bench_function("cassandra_request_throttling_unparsed", |b| {
b.to_async(&rt).iter_batched(
|| BenchInput {
chain: chain.build(TransformContextBuilder::new_test()),
wrapper: wrapper.clone(),
},
|| BenchInput::new_pre_used(&chain, &wrapper),
BenchInput::bench,
BatchSize::SmallInput,
)
Expand Down Expand Up @@ -226,10 +211,7 @@ fn criterion_benchmark(c: &mut Criterion) {

group.bench_function("cassandra_rewrite_peers_passthrough", |b| {
b.to_async(&rt).iter_batched(
|| BenchInput {
chain: chain.build(TransformContextBuilder::new_test()),
wrapper: wrapper.clone(),
},
|| BenchInput::new_pre_used(&chain, &wrapper),
BenchInput::bench,
BatchSize::SmallInput,
)
Expand Down Expand Up @@ -272,10 +254,7 @@ fn criterion_benchmark(c: &mut Criterion) {

group.bench_function("cassandra_protect_unprotected", |b| {
b.to_async(&rt).iter_batched(
|| BenchInput {
chain: chain.build(TransformContextBuilder::new_test()),
wrapper: wrapper.clone(),
},
|| BenchInput::new_pre_used(&chain, &wrapper),
BenchInput::bench,
BatchSize::SmallInput,
)
Expand All @@ -287,10 +266,7 @@ fn criterion_benchmark(c: &mut Criterion) {

group.bench_function("cassandra_protect_protected", |b| {
b.to_async(&rt).iter_batched(
|| BenchInput {
chain: chain.build(TransformContextBuilder::new_test()),
wrapper: wrapper.clone(),
},
|| BenchInput::new_pre_used(&chain, &wrapper),
BenchInput::bench,
BatchSize::SmallInput,
)
Expand Down Expand Up @@ -320,12 +296,17 @@ fn criterion_benchmark(c: &mut Criterion) {
"127.0.0.1:6379".parse().unwrap(),
);

group.bench_function("query_counter", |b| {
group.bench_function("query_counter_fresh", |b| {
b.to_async(&rt).iter_batched(
|| BenchInput {
chain: chain.build(TransformContextBuilder::new_test()),
wrapper: wrapper.clone(),
},
|| BenchInput::new_fresh(&chain, &wrapper),
BenchInput::bench,
BatchSize::SmallInput,
)
});

group.bench_function("query_counter_pre_used", |b| {
b.to_async(&rt).iter_batched(
|| BenchInput::new_pre_used(&chain, &wrapper),
BenchInput::bench,
BatchSize::SmallInput,
)
Expand Down Expand Up @@ -366,6 +347,29 @@ struct BenchInput<'a> {
}

impl<'a> BenchInput<'a> {
// Setup the bench such that the chain is completely fresh
fn new_fresh(chain: &TransformChainBuilder, wrapper: &Wrapper<'a>) -> Self {
BenchInput {
chain: chain.build(TransformContextBuilder::new_test()),
wrapper: wrapper.clone(),
}
}

// Setup the bench such that the chain has already had the test wrapper passed through it.
// This ensures that any adhoc setup for that message type has been performed.
// This is a more realistic bench for typical usage.
fn new_pre_used(chain: &TransformChainBuilder, wrapper: &Wrapper<'a>) -> Self {
let mut chain = chain.build(TransformContextBuilder::new_test());

// Run the chain once so we are measuring the chain once each transform has been fully initialized
futures::executor::block_on(chain.process_request(wrapper.clone())).unwrap();

BenchInput {
chain,
wrapper: wrapper.clone(),
}
}

async fn bench(mut self) -> (Vec<Message>, TransformChain) {
// Return both the chain itself and the response to avoid measuring the time to drop the values in the benchmark
(
Expand Down