rust-postgres/tokio-postgres/benches/bench.rs

60 lines
1.9 KiB
Rust
Raw Normal View History

2019-10-16 01:17:10 +00:00
use criterion::{criterion_group, criterion_main, Criterion};
use futures::channel::oneshot;
use futures::executor;
use std::sync::Arc;
use std::time::Instant;
use tokio::runtime::Runtime;
use tokio_postgres::{Client, NoTls};
fn setup() -> (Client, Runtime) {
2019-11-27 00:40:36 +00:00
let mut runtime = Runtime::new().unwrap();
2019-10-16 01:17:10 +00:00
let (client, conn) = runtime
.block_on(tokio_postgres::connect(
"host=localhost port=5433 user=postgres",
NoTls,
))
.unwrap();
runtime.spawn(async { conn.await.unwrap() });
(client, runtime)
}
fn query_prepared(c: &mut Criterion) {
2019-11-27 00:40:36 +00:00
let (client, mut runtime) = setup();
2019-10-16 01:17:10 +00:00
let statement = runtime.block_on(client.prepare("SELECT $1::INT8")).unwrap();
c.bench_function("runtime_block_on", move |b| {
b.iter(|| {
runtime
.block_on(client.query(&statement, &[&1i64]))
.unwrap()
})
});
2019-11-27 00:40:36 +00:00
let (client, mut runtime) = setup();
2019-10-16 01:17:10 +00:00
let statement = runtime.block_on(client.prepare("SELECT $1::INT8")).unwrap();
c.bench_function("executor_block_on", move |b| {
2019-10-19 02:33:33 +00:00
b.iter(|| executor::block_on(client.query(&statement, &[&1i64])).unwrap())
2019-10-16 01:17:10 +00:00
});
2019-11-27 00:40:36 +00:00
let (client, mut runtime) = setup();
2019-10-16 01:17:10 +00:00
let client = Arc::new(client);
let statement = runtime.block_on(client.prepare("SELECT $1::INT8")).unwrap();
c.bench_function("spawned", move |b| {
b.iter_custom(|iters| {
let (tx, rx) = oneshot::channel();
let client = client.clone();
let statement = statement.clone();
runtime.spawn(async move {
let start = Instant::now();
for _ in 0..iters {
client.query(&statement, &[&1i64]).await.unwrap();
}
tx.send(start.elapsed()).unwrap();
});
executor::block_on(rx).unwrap()
})
});
}
criterion_group!(benches, query_prepared);
criterion_main!(benches);