use criterion::{Bencher, Criterion, criterion_group, criterion_main};
use rustis::{
Error,
client::Client,
commands::StringCommands,
resp::{Value, cmd},
};
use std::{future::IntoFuture, hint::black_box, time::Duration};
fn current_thread_runtime() -> tokio::runtime::Runtime {
let mut builder = tokio::runtime::Builder::new_current_thread();
builder.enable_io();
builder.enable_time();
builder.build().unwrap()
}
async fn get_rustis_client() -> Client {
let redis_host = std::env::var("REDIS_HOST").unwrap_or_else(|_| "127.0.0.1".to_string());
let client = Client::connect(redis_host).await.unwrap();
client.set("key", "value").await.unwrap();
client
}
fn bench_ergonomic_round_trip(b: &mut Bencher) {
let runtime = current_thread_runtime();
let client = runtime.block_on(get_rustis_client());
b.iter(|| {
runtime
.block_on(async {
let _: String = client.get("key").await?;
Ok::<_, Error>(())
})
.unwrap()
});
}
fn bench_generic_round_trip(b: &mut Bencher) {
let runtime = current_thread_runtime();
let client = runtime.block_on(get_rustis_client());
b.iter(|| {
runtime
.block_on(async {
let _: String = client.send(cmd("GET").key("key"), None).await?;
Ok::<_, Error>(())
})
.unwrap()
});
}
fn bench_ergonomic_prepare(b: &mut Bencher) {
let runtime = current_thread_runtime();
let client = runtime.block_on(get_rustis_client());
b.iter(|| {
let future = client.get::<String>("key").into_future();
black_box(&future);
drop(future);
});
}
fn bench_generic_prepare(b: &mut Bencher) {
let runtime = current_thread_runtime();
let client = runtime.block_on(get_rustis_client());
b.iter(|| {
let future = client.send::<Value>(cmd("GET").key("key"), None);
black_box(&future);
drop(future);
});
}
fn bench_into_future(c: &mut Criterion) {
let mut group = c.benchmark_group("into_future");
group
.measurement_time(Duration::from_secs(10))
.bench_function("ergonomic_round_trip", bench_ergonomic_round_trip)
.bench_function("generic_round_trip", bench_generic_round_trip)
.bench_function("ergonomic_prepare", bench_ergonomic_prepare)
.bench_function("generic_prepare", bench_generic_prepare);
group.finish();
}
criterion_group!(bench, bench_into_future);
criterion_main!(bench);