benches: add sqlite_fetch_all benchmark

adapted code from https://github.com/launchbadge/sqlx/issues/266#issuecomment-921722399
This commit is contained in:
Austin Bonander
2022-06-23 17:20:15 -07:00
parent 39eadd6d6d
commit 326964d717
5 changed files with 59 additions and 1 deletions

4
.gitignore vendored
View File

@@ -9,3 +9,7 @@ target/
# Environment
.env
# Shared-memory and WAL files created by SQLite.
*-shm
*-wal

1
Cargo.lock generated
View File

@@ -2419,6 +2419,7 @@ dependencies = [
name = "sqlx-bench"
version = "0.1.0"
dependencies = [
"chrono",
"criterion",
"dotenv",
"once_cell",

View File

@@ -33,15 +33,23 @@ runtime-tokio-rustls = [
]
postgres = ["sqlx/postgres"]
sqlite = ["sqlx/sqlite"]
[dependencies]
criterion = "0.3.3"
dotenv = "0.15.0"
once_cell = "1.4"
sqlx = { version = "0.6", path = "../", default-features = false }
sqlx = { version = "0.6", path = "../", default-features = false, features = ["macros"] }
sqlx-rt = { version = "0.6", path = "../sqlx-rt", default-features = false }
chrono = "0.4.19"
[[bench]]
name = "pg_pool"
harness = false
required-features = ["postgres"]
[[bench]]
name = "sqlite_fetch_all"
harness = false
required-features = ["sqlite"]

View File

@@ -0,0 +1,45 @@
use sqlx::{Connection, Executor};
use std::time::Instant;
#[derive(sqlx::FromRow)]
struct Test {
id: i32,
}
fn main() -> sqlx::Result<()> {
sqlx_rt::block_on(async {
let mut conn = sqlx::SqliteConnection::connect("sqlite://test.db?mode=rwc").await?;
let delete_sql = "DROP TABLE IF EXISTS test";
conn.execute(delete_sql).await?;
let create_sql = "CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY NOT NULL)";
conn.execute(create_sql).await?;
let mut tx = conn.begin().await?;
for entry in 0i32..100000 {
sqlx::query("INSERT INTO test (id) VALUES ($1)")
.bind(entry)
.execute(&mut tx)
.await?;
}
tx.commit().await?;
for _ in 0..10i8 {
let start = chrono::Utc::now();
println!(
"total: {}",
sqlx::query!("SELECT id from test")
.fetch_all(&mut conn)
.await?
.len()
);
let elapsed = chrono::Utc::now() - start;
println!("elapsed {}", elapsed);
}
Ok(())
})
}

BIN
sqlx-bench/test.db Normal file

Binary file not shown.