Skip to content

Commit 9358d4e

Browse files
committed
Update benchmark implementations
* Use the tokio runtime for sqlx/sea-orm as requested here: transact-rs/sqlx#1481 (comment) * Update quaint to a version that uses tokio 1.0 and reenable it in the metrics job
1 parent 53d7965 commit 9358d4e

7 files changed

Lines changed: 181 additions & 145 deletions

File tree

.github/workflows/metrics.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,21 @@ jobs:
6969
uses: actions-rs/cargo@v1
7070
with:
7171
command: bench
72-
args: --manifest-path diesel_bench/Cargo.toml --no-default-features --features "${{matrix.backend}} sqlx-bench sqlx/${{matrix.backend}} async-std rust_postgres futures sea-orm sea-orm/sqlx-${{matrix.backend}} criterion/async_std"
72+
args: --manifest-path diesel_bench/Cargo.toml --no-default-features --features "${{matrix.backend}} sqlx-bench sqlx/${{matrix.backend}} rust_postgres futures sea-orm sea-orm/sqlx-${{matrix.backend}} criterion/async_tokio quaint quaint/postgresql quaint/serde-support serde"
7373

7474
- name: Run Benchmarks (Sqlite)
7575
if: matrix.backend == 'sqlite'
7676
uses: actions-rs/cargo@v1
7777
with:
7878
command: bench
79-
args: --manifest-path diesel_bench/Cargo.toml --no-default-features --features "${{matrix.backend}} sqlx-bench sqlx/${{matrix.backend}} async-std rusqlite futures sea-orm sea-orm/sqlx-${{matrix.backend}} criterion/async_std"
79+
args: --manifest-path diesel_bench/Cargo.toml --no-default-features --features "${{matrix.backend}} sqlx-bench sqlx/${{matrix.backend}} tokio rusqlite futures sea-orm sea-orm/sqlx-${{matrix.backend}} criterion/async_tokio quaint quaint/sqlite quaint/serde-support serde"
8080

8181
- name: Run Benchmarks (Mysql)
8282
if: matrix.backend == 'mysql'
8383
uses: actions-rs/cargo@v1
8484
with:
8585
command: bench
86-
args: --manifest-path diesel_bench/Cargo.toml --no-default-features --features "${{matrix.backend}} sqlx-bench sqlx/${{matrix.backend}} async-std rustorm rustorm/with-${{matrix.backend}} rustorm_dao rust_mysql futures sea-orm sea-orm/sqlx-${{matrix.backend}} criterion/async_std"
86+
args: --manifest-path diesel_bench/Cargo.toml --no-default-features --features "${{matrix.backend}} sqlx-bench sqlx/${{matrix.backend}} tokio rustorm rustorm/with-${{matrix.backend}} rustorm_dao rust_mysql futures sea-orm sea-orm/sqlx-${{matrix.backend}} criterion/async_tokio quaint quaint/mysql quaint/serde-support serde"
8787

8888
- name: Push metrics
8989
env:

diesel_bench/Cargo.toml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,16 @@ autobenches = false
1212
[dependencies]
1313
dotenv = "0.15"
1414
criterion = "0.3.2"
15-
sqlx = {version = "0.5.0", optional = true}
16-
async-std = { version = "1.5", optional = true}
15+
sqlx = {version = "0.5.0", features = ["runtime-tokio-rustls"], optional = true}
16+
tokio = {version = "1", optional = true}
1717
rusqlite = {version = "0.25", optional = true}
1818
rust_postgres = {version = "0.19", optional = true, package = "postgres"}
1919
rust_mysql = {version = "21.0.1", optional = true, package = "mysql"}
2020
rustorm = {version = "0.20", optional = true}
2121
rustorm_dao = {version = "0.20", optional = true}
2222
quaint = {version = "0.2.0-alpha.13", optional = true}
23-
tokio = {version = "0.2", optional = true}
2423
serde = {version = "1", optional = true, features = ["derive"]}
25-
sea-orm = {version = "0.3", optional = true, features = ["runtime-async-std-rustls"]}
24+
sea-orm = {version = "0.3", optional = true, features = ["runtime-tokio-rustls"]}
2625
futures = {version = "0.3", optional = true}
2726

2827
[dependencies.diesel]
@@ -47,7 +46,7 @@ default = []
4746
postgres = ["diesel/postgres"]
4847
sqlite = ["diesel/sqlite"]
4948
mysql = ["diesel/mysql"]
50-
sqlx-bench = ["sqlx", "async-std", "sqlx/runtime-async-std-rustls"]
49+
sqlx-bench = ["sqlx", "tokio", "sqlx/runtime-tokio-rustls"]
5150

5251
[patch.crates-io]
53-
quaint = {git = "https://github.com/prisma/quaint", rev = "ec9384f"}
52+
quaint = {git = "https://github.com/prisma/quaint", rev = "e077df3"}

diesel_bench/Readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ To enable other crates add the following features:
3333

3434
* `SQLx: ` `sqlx-bench sqlx/$backend $backend`
3535
* `Rustorm`: `rustorm rustorm/with-$backend rustorm_dao $backend`
36-
* `SeaORM`: `sea-orm sea-orm/sqlx-$backend sqlx async-std criterion/async_std futures $backend`
36+
* `SeaORM`: `sea-orm sea-orm/sqlx-$backend sqlx tokio criterion/tokio futures $backend`
3737
* `Quaint`: `quaint quaint/$backend tokio quaint/serde-support serde $backend`
38-
* `Postgres`: `rust-postgres $backend`
38+
* `Postgres`: `rust_postgres $backend`
3939
* `Rusqlite`: `rusqlite $backend`
4040
* `Mysql`: `rust-mysql $backend`
4141

diesel_bench/benches/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ fn bench_medium_complex_query(c: &mut Criterion) {
148148
crate::quaint_benches::bench_medium_complex_query(b, *i);
149149
});
150150

151-
#[cfg(feature = "sqlx-bench")]
151+
#[cfg(all(feature = "sqlx-bench", not(feature = "sqlite")))]
152152
group.bench_with_input(
153153
BenchmarkId::new("sqlx_query_as_macro", size),
154154
size,

diesel_bench/benches/quaint_benches.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,20 @@ fn connect(rt: &mut Runtime) -> Quaint {
5858

5959
let conn = rt.block_on({
6060
async {
61-
let conn = Quaint::new(&db_url).await.unwrap();
61+
let conn;
62+
63+
#[cfg(feature = "sqlite")]
64+
{
65+
conn = Quaint::new_in_memory().unwrap();
66+
}
67+
68+
#[cfg(not(feature = "sqlite"))]
69+
{
70+
conn = Quaint::new(&db_url).await.unwrap();
71+
}
6272

6373
if cfg!(feature = "sqlite") {
74+
#[cfg(feature = "sqlite")]
6475
for migration in super::SQLITE_MIGRATION_SQL {
6576
conn.execute_raw(migration, &[]).await.unwrap();
6677
}

diesel_bench/benches/sea_orm_benches/mod.rs

Lines changed: 91 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use criterion::Bencher;
22
use sea_orm::entity::*;
33
use sea_orm::query::*;
44
use sea_orm::DatabaseConnection;
5+
use tokio::runtime::Runtime;
56

67
mod comments;
78
mod posts;
@@ -12,14 +13,16 @@ use self::posts::Entity as Post;
1213
use self::users::Entity as User;
1314

1415
#[cfg(feature = "postgres")]
15-
fn connection() -> (sqlx::PgPool, DatabaseConnection) {
16+
fn connection() -> (sqlx::PgPool, DatabaseConnection, Runtime) {
1617
use sea_orm::SqlxPostgresConnector;
1718

19+
let rt = Runtime::new().expect("Failed to start runtime");
20+
1821
dotenv::dotenv().ok();
1922
let connection_url = dotenv::var("PG_DATABASE_URL")
2023
.or_else(|_| dotenv::var("DATABASE_URL"))
2124
.expect("DATABASE_URL must be set in order to run tests");
22-
async_std::task::block_on(async {
25+
let (pool, db) = rt.block_on(async {
2326
use sqlx::Executor;
2427
let pool = sqlx::PgPool::connect(&connection_url).await.unwrap();
2528
pool.execute("TRUNCATE TABLE comments CASCADE;")
@@ -30,19 +33,23 @@ fn connection() -> (sqlx::PgPool, DatabaseConnection) {
3033

3134
let db = SqlxPostgresConnector::from_sqlx_postgres_pool(pool.clone());
3235
(pool, db)
33-
})
36+
});
37+
38+
(pool, db, rt)
3439
}
3540

3641
#[cfg(feature = "mysql")]
37-
fn connection() -> (sqlx::MySqlPool, DatabaseConnection) {
38-
use sea_orm::SqlxMySqlConnector;
42+
fn connection() -> (sqlx::MySqlPool, DatabaseConnection, Runtime) {
3943
use futures::StreamExt;
44+
use sea_orm::SqlxMySqlConnector;
45+
46+
let rt = Runtime::new().expect("Failed to start runtime");
4047

4148
dotenv::dotenv().ok();
4249
let connection_url = dotenv::var("MYSQL_DATABASE_URL")
4350
.or_else(|_| dotenv::var("DATABASE_URL"))
4451
.expect("DATABASE_URL must be set in order to run tests");
45-
async_std::task::block_on(async {
52+
let (pool, db) = rt.block_on(async {
4653
use sqlx::Executor;
4754
let pool = sqlx::MySqlPool::connect(&connection_url).await.unwrap();
4855
let mut result_stream = pool.execute_many(
@@ -51,23 +58,25 @@ fn connection() -> (sqlx::MySqlPool, DatabaseConnection) {
5158
TRUNCATE TABLE posts;\
5259
TRUNCATE TABLE users;\
5360
SET FOREIGN_KEY_CHECKS = 1;\
54-
"
61+
",
5562
);
5663
while let Some(e) = result_stream.next().await {
5764
let _ = e.unwrap();
5865
}
5966
let db = SqlxMySqlConnector::from_sqlx_mysql_pool(pool.clone());
6067
(pool, db)
61-
})
68+
});
69+
(pool, db, rt)
6270
}
6371

6472
#[cfg(feature = "sqlite")]
65-
fn connection() -> (sqlx::SqlitePool, DatabaseConnection) {
73+
fn connection() -> (sqlx::SqlitePool, DatabaseConnection, Runtime) {
6674
use sea_orm::SqlxSqliteConnector;
6775

76+
let rt = Runtime::new().expect("Failed to start runtime");
6877
dotenv::dotenv().ok();
6978

70-
async_std::task::block_on(async {
79+
let (pool, db) = rt.block_on(async {
7180
use sqlx::Executor;
7281
let pool = sqlx::SqlitePool::connect("sqlite::memory:").await.unwrap();
7382

@@ -81,7 +90,9 @@ fn connection() -> (sqlx::SqlitePool, DatabaseConnection) {
8190

8291
let db = SqlxSqliteConnector::from_sqlx_sqlite_pool(pool.clone());
8392
(pool, db)
84-
})
93+
});
94+
95+
(pool, db, rt)
8596
}
8697

8798
async fn insert_users(
@@ -101,22 +112,22 @@ async fn insert_users(
101112
}
102113

103114
pub fn bench_trivial_query(b: &mut Bencher, size: usize) {
104-
let (pool, conn) = connection();
115+
let (pool, conn, rt) = connection();
105116

106-
async_std::task::block_on(async {
117+
rt.block_on(async {
107118
insert_users(size, &conn, |_| None).await;
108119
});
109120

110-
b.to_async(criterion::async_executor::AsyncStdExecutor)
121+
b.to_async(&rt)
111122
.iter(|| async { User::find().all(&conn).await.unwrap() });
112123

113-
async_std::task::block_on(async {
124+
rt.block_on(async {
114125
pool.close().await;
115126
})
116127
}
117128

118129
pub fn bench_medium_complex_query(b: &mut Bencher, size: usize) {
119-
let (pool, conn) = connection();
130+
let (pool, conn, rt) = connection();
120131

121132
let hair_color_callback = |i| {
122133
Some(if i % 2 == 0 {
@@ -125,34 +136,32 @@ pub fn bench_medium_complex_query(b: &mut Bencher, size: usize) {
125136
String::from("brown")
126137
})
127138
};
128-
async_std::task::block_on(async {
139+
rt.block_on(async {
129140
insert_users(size, &conn, hair_color_callback).await;
130141
});
131142

132-
b.to_async(criterion::async_executor::AsyncStdExecutor)
133-
.iter(|| async {
134-
let r: Vec<(self::users::Model, Option<self::posts::Model>)> = User::find()
135-
.find_also_related(Post)
136-
.all(&conn)
137-
.await
138-
.unwrap();
139-
r
140-
});
141-
142-
async_std::task::block_on(async {
143+
b.to_async(&rt).iter(|| async {
144+
let r: Vec<(self::users::Model, Option<self::posts::Model>)> = User::find()
145+
.find_also_related(Post)
146+
.all(&conn)
147+
.await
148+
.unwrap();
149+
r
150+
});
151+
152+
rt.block_on(async {
143153
pool.close().await;
144154
})
145155
}
146156

147157
pub fn bench_insert(b: &mut Bencher, size: usize) {
148-
let (pool, conn) = connection();
158+
let (pool, conn, rt) = connection();
149159

150-
b.to_async(criterion::async_executor::AsyncStdExecutor)
151-
.iter(|| async {
152-
insert_users(size, &conn, |_| Some(String::from("hair_color"))).await;
153-
});
160+
b.to_async(&rt).iter(|| async {
161+
insert_users(size, &conn, |_| Some(String::from("hair_color"))).await;
162+
});
154163

155-
async_std::task::block_on(async {
164+
rt.block_on(async {
156165
pool.close().await;
157166
})
158167
}
@@ -165,9 +174,9 @@ pub fn loading_associations_sequentially(b: &mut Bencher) {
165174
const USER_NUMBER: usize = 100;
166175

167176
// SETUP A TON OF DATA
168-
let (pool, conn) = connection();
177+
let (pool, conn, rt) = connection();
169178

170-
async_std::task::block_on(async {
179+
rt.block_on(async {
171180
insert_users(USER_NUMBER, &conn, |i| {
172181
Some(if i % 2 == 0 {
173182
"black".to_owned()
@@ -213,54 +222,53 @@ pub fn loading_associations_sequentially(b: &mut Bencher) {
213222
});
214223

215224
// ACTUAL BENCHMARK
216-
b.to_async(criterion::async_executor::AsyncStdExecutor)
217-
.iter(|| async {
218-
use std::collections::HashMap;
219-
220-
let res: Vec<(self::users::Model, Vec<self::posts::Model>)> = User::find()
221-
.find_with_related(Post)
222-
.all(&conn)
223-
.await
224-
.unwrap();
225-
226-
let post_ids = res
227-
.iter()
228-
.flat_map(|(_, posts)| posts.iter().map(|post| post.id))
229-
.collect::<Vec<_>>();
230-
231-
let comments = Comment::find()
232-
.filter(self::comments::Column::PostId.is_in(post_ids))
233-
.all(&conn)
234-
.await
235-
.unwrap();
236-
237-
let mut lookup = HashMap::new();
238-
239-
for comment in comments {
240-
lookup
241-
.entry(comment.post_id)
242-
.or_insert(Vec::new())
243-
.push(comment);
244-
}
245-
246-
res.into_iter()
247-
.map(|(user, posts)| {
248-
let posts = posts
249-
.into_iter()
250-
.map(|post| {
251-
let post_id = post.id;
252-
(post, lookup[&post_id].clone())
253-
})
254-
.collect();
255-
(user, posts)
256-
})
257-
.collect::<Vec<(
258-
self::users::Model,
259-
Vec<(self::posts::Model, Vec<self::comments::Model>)>,
260-
)>>()
261-
});
225+
b.to_async(&rt).iter(|| async {
226+
use std::collections::HashMap;
227+
228+
let res: Vec<(self::users::Model, Vec<self::posts::Model>)> = User::find()
229+
.find_with_related(Post)
230+
.all(&conn)
231+
.await
232+
.unwrap();
233+
234+
let post_ids = res
235+
.iter()
236+
.flat_map(|(_, posts)| posts.iter().map(|post| post.id))
237+
.collect::<Vec<_>>();
238+
239+
let comments = Comment::find()
240+
.filter(self::comments::Column::PostId.is_in(post_ids))
241+
.all(&conn)
242+
.await
243+
.unwrap();
244+
245+
let mut lookup = HashMap::new();
246+
247+
for comment in comments {
248+
lookup
249+
.entry(comment.post_id)
250+
.or_insert(Vec::new())
251+
.push(comment);
252+
}
253+
254+
res.into_iter()
255+
.map(|(user, posts)| {
256+
let posts = posts
257+
.into_iter()
258+
.map(|post| {
259+
let post_id = post.id;
260+
(post, lookup[&post_id].clone())
261+
})
262+
.collect();
263+
(user, posts)
264+
})
265+
.collect::<Vec<(
266+
self::users::Model,
267+
Vec<(self::posts::Model, Vec<self::comments::Model>)>,
268+
)>>()
269+
});
262270

263-
async_std::task::block_on(async {
271+
rt.block_on(async {
264272
pool.close().await;
265273
})
266274
}

0 commit comments

Comments
 (0)