@@ -2,6 +2,7 @@ use criterion::Bencher;
22use sea_orm:: entity:: * ;
33use sea_orm:: query:: * ;
44use sea_orm:: DatabaseConnection ;
5+ use tokio:: runtime:: Runtime ;
56
67mod comments;
78mod posts;
@@ -12,14 +13,16 @@ use self::posts::Entity as Post;
1213use 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
8798async fn insert_users (
@@ -101,22 +112,22 @@ async fn insert_users(
101112}
102113
103114pub 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
118129pub 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
147157pub 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