diff --git a/tokio-postgres/Cargo.toml b/tokio-postgres/Cargo.toml index 5e685c46..0cd04bd0 100644 --- a/tokio-postgres/Cargo.toml +++ b/tokio-postgres/Cargo.toml @@ -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" } diff --git a/tokio-postgres/benches/bench.rs b/tokio-postgres/benches/bench.rs new file mode 100644 index 00000000..4369242f --- /dev/null +++ b/tokio-postgres/benches/bench.rs @@ -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); diff --git a/tokio-postgres/src/lib.rs b/tokio-postgres/src/lib.rs index ebffbb90..3124d2bf 100644 --- a/tokio-postgres/src/lib.rs +++ b/tokio-postgres/src/lib.rs @@ -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; diff --git a/tokio-postgres/src/query.rs b/tokio-postgres/src/query.rs index 9d3829c5..5a1b7b49 100644 --- a/tokio-postgres/src/query.rs +++ b/tokio-postgres/src/query.rs @@ -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, diff --git a/tokio-postgres/src/simple_query.rs b/tokio-postgres/src/simple_query.rs index 8f31cd37..01858374 100644 --- a/tokio-postgres/src/simple_query.rs +++ b/tokio-postgres/src/simple_query.rs @@ -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 { let buf = encode(client, query)?;