Skip to content

Commit 75c0c02

Browse files
committed
Ensure PG gives consistent benchmark results
When we added the 10k users to the database, even though the transaction would never be committed, the query planner would still account for them. This would mean that the max cost would continuously rise, thinking there might be 100s of thousands of rows. This led to `SELECT * FROM users` when the table was empty to take 70ms to run (when even with 10k users it should be sub millisecond). I'm not sure exactly what it was doing that was so slow, but if I had to guess I'd say that it was probably allocating a lot more memory than it needed to. By truncating the table at the beginning of each transaction, we ensure the query planner operates on 10k rows max, giving us consistent results. It should be noted that this isn't indicitive of any deeper flaw in the framework. This gets cleaned up by the periodic vacuum that PG runs, and in reality creating 10k rows in a transaction and then rolling back multiple times per second is not realistic.
1 parent 604b154 commit 75c0c02

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

diesel_tests/tests/bench.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,21 @@ extern crate test;
77
mod schema;
88

99
use self::test::Bencher;
10-
use self::schema::*;
10+
use self::schema::{users, NewUser, User, Post, TestConnection, posts, batch_insert};
1111
use diesel::*;
1212

13+
#[cfg(not(feature = "sqlite"))]
14+
fn connection() -> TestConnection {
15+
let conn = schema::connection();
16+
conn.execute("TRUNCATE TABLE USERS").unwrap();
17+
conn
18+
}
19+
20+
#[cfg(feature = "sqlite")]
21+
fn connection() -> TestConnection {
22+
schema::connection()
23+
}
24+
1325
#[bench]
1426
fn bench_selecting_0_rows_with_trivial_query(b: &mut Bencher) {
1527
let conn = connection();

0 commit comments

Comments
 (0)