mirror of
https://github.com/launchbadge/sqlx.git
synced 2026-04-19 13:15:50 +00:00
benches: add sqlite_fetch_all benchmark
adapted code from https://github.com/launchbadge/sqlx/issues/266#issuecomment-921722399
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -9,3 +9,7 @@ target/
|
||||
|
||||
# Environment
|
||||
.env
|
||||
|
||||
# Shared-memory and WAL files created by SQLite.
|
||||
*-shm
|
||||
*-wal
|
||||
|
||||
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -2419,6 +2419,7 @@ dependencies = [
|
||||
name = "sqlx-bench"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"criterion",
|
||||
"dotenv",
|
||||
"once_cell",
|
||||
|
||||
@@ -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"]
|
||||
|
||||
45
sqlx-bench/benches/sqlite_fetch_all.rs
Normal file
45
sqlx-bench/benches/sqlite_fetch_all.rs
Normal 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
BIN
sqlx-bench/test.db
Normal file
Binary file not shown.
Reference in New Issue
Block a user