Add some simple benchmarks

This commit is contained in:
Steven Fackler 2019-10-15 18:17:10 -07:00
parent 2a8f7bdd96
commit 9d2ec747ef
5 changed files with 72 additions and 6 deletions

View File

@ -13,6 +13,10 @@ categories = ["database"]
[lib]
test = false
[[bench]]
name = "bench"
harness = false
[package.metadata.docs.rs]
all-features = true
@ -47,6 +51,7 @@ tokio = { version = "=0.2.0-alpha.6", default-features = false, features = ["io"
[dev-dependencies]
tokio = "=0.2.0-alpha.6"
env_logger = "0.5"
criterion = "0.3"
bit-vec-06 = { version = "0.6", package = "bit-vec" }
chrono-04 = { version = "0.4", package = "chrono" }

View File

@ -0,0 +1,61 @@
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) {
let runtime = Runtime::new().unwrap();
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) {
let (client, runtime) = setup();
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()
})
});
let (client, runtime) = setup();
let statement = runtime.block_on(client.prepare("SELECT $1::INT8")).unwrap();
c.bench_function("executor_block_on", move |b| {
b.iter(|| {
executor::block_on(client.query(&statement, &[&1i64])).unwrap()
})
});
let (client, runtime) = setup();
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);

View File

@ -110,12 +110,12 @@ pub use crate::connection::Connection;
use crate::error::DbError;
pub use crate::error::Error;
pub use crate::portal::Portal;
pub use crate::query::RowStream;
pub use crate::row::{Row, SimpleQueryRow};
pub use crate::simple_query::SimpleQueryStream;
#[cfg(feature = "runtime")]
pub use crate::socket::Socket;
pub use crate::statement::{Column, Statement};
pub use crate::query::RowStream;
pub use crate::simple_query::SimpleQueryStream;
#[cfg(feature = "runtime")]
use crate::tls::MakeTlsConnect;
pub use crate::tls::NoTls;

View File

@ -1,16 +1,16 @@
use crate::client::{InnerClient, Responses};
use pin_project::pin_project;
use crate::codec::FrontendMessage;
use crate::connection::RequestMessages;
use crate::types::{IsNull, ToSql};
use crate::{Error, Portal, Row, Statement};
use bytes::{Bytes, BytesMut};
use futures::{ready, Stream};
use pin_project::pin_project;
use postgres_protocol::message::backend::Message;
use postgres_protocol::message::frontend;
use std::marker::PhantomPinned;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::marker::PhantomPinned;
pub async fn query<'a, I>(
client: &InnerClient,

View File

@ -5,13 +5,13 @@ use crate::{Error, SimpleQueryMessage, SimpleQueryRow};
use bytes::Bytes;
use fallible_iterator::FallibleIterator;
use futures::{ready, Stream};
use pin_project::pin_project;
use postgres_protocol::message::backend::Message;
use postgres_protocol::message::frontend;
use std::marker::PhantomPinned;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
use std::marker::PhantomPinned;
use pin_project::pin_project;
pub async fn simple_query(client: &InnerClient, query: &str) -> Result<SimpleQueryStream, Error> {
let buf = encode(client, query)?;