Skip to content

Commit

Permalink
add faster sqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
z-Wind committed Jul 17, 2024
1 parent 7c6fa10 commit b2ef058
Show file tree
Hide file tree
Showing 15 changed files with 2,046 additions and 96 deletions.
53 changes: 48 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@ jobs:
- name: Rust Cache dependencies
uses: Swatinem/rust-cache@v2

- name: Clippy --features sqlite,postgresql,mysql,xml
run: cargo clippy --features sqlite,postgresql,mysql,xml --all-targets -- -D warnings
- name: Clippy --features sqlite,postgresql,mysql,xml,sqlitefaster
run: cargo clippy --features sqlite,postgresql,mysql,xml,sqlitefaster --all-targets -- -D warnings

- name: Clippy --features sqlite
run: cargo clippy --features sqlite --all-targets -- -D warnings

- name: Clippy --features sqlitefaster
run: cargo clippy --features sqlitefaster --all-targets -- -D warnings

- name: Clippy --features postgresql
run: cargo clippy --features postgresql --all-targets -- -D warnings

Expand All @@ -49,12 +52,15 @@ jobs:
- name: Clippy --features xml
run: cargo clippy --features xml --all-targets -- -D warnings

- name: Clippy --features sqlite,postgresql,mysql,xml,decimal
run: cargo clippy --features sqlite,postgresql,mysql,xml,decimal --all-targets -- -D warnings
- name: Clippy --features sqlite,postgresql,mysql,xml,sqlitefaster,decimal
run: cargo clippy --features sqlite,postgresql,mysql,xml,sqlitefaster,decimal --all-targets -- -D warnings

- name: Clippy --features sqlite,decimal
run: cargo clippy --features sqlite,decimal --all-targets -- -D warnings

- name: Clippy --features sqlitefaster,decimal
run: cargo clippy --features sqlitefaster,decimal --all-targets -- -D warnings

- name: Clippy --features postgresql,decimal
run: cargo clippy --features postgresql,decimal --all-targets -- -D warnings

Expand Down Expand Up @@ -134,6 +140,43 @@ jobs:
cargo test --doc --features sqlite,decimal
test_sqlitefaster:
name: Test SQLiteFaster
needs: [lint]
runs-on: ${{ matrix.os }}
strategy:
matrix:
os:
- ubuntu-latest
- macos-latest
- windows-latest

steps:
- uses: actions/checkout@v4

- name: Rust Cache dependencies
uses: Swatinem/rust-cache@v2

- name: Install latest nextest release
uses: taiki-e/install-action@nextest

- name: Check Schema
run: cargo check --features sqlitefaster --all-targets
env:
DATABASE_URL: "file:/tests/db/sqlite/complex_sample.gnucash"

- name: Run tests --features sqlitefaster
run: cargo nextest run --config-file ${{ github.workspace }}/.github/nextest.toml --profile ci --features sqlitefaster

- name: Run tests --features sqlitefaster,decimal
run: cargo nextest run --config-file ${{ github.workspace }}/.github/nextest.toml --profile ci --features sqlitefaster,decimal

- name: Run doc tests
run: |
cargo test --doc --features sqlitefaster
cargo test --doc --features sqlitefaster,decimal
test_mysql:
name: Test MySQL
needs: [lint]
Expand Down Expand Up @@ -238,4 +281,4 @@ jobs:
- name: Run doc tests
run: |
cargo test --doc --features postgresql
cargo test --doc --features postgresql,decimal
cargo test --doc --features postgresql,decimal
11 changes: 9 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ flate2 = "1.0"
tokio = { version = "1.36", features = ["sync"] }
num-traits = "0.2"
thiserror = "1.0"
rusqlite = { version = "*", features = ["bundled", "chrono"], optional = true }

[lib]
name = "rucash"
Expand All @@ -48,6 +49,7 @@ sqlx_common = [
"sqlx/chrono",
]
sqlite = ["sqlx_common", "sqlx/sqlite"]
sqlitefaster = ["rusqlite"]
postgresql = ["sqlx_common", "sqlx/postgres"]
mysql = ["sqlx_common", "sqlx/mysql"]
xml = ["xmltree"]
Expand All @@ -56,13 +58,18 @@ decimal = ["rust_decimal"]
[[bench]]
name = "benchmark"
harness = false
required-features = ["sqlite", "xml"]
required-features = ["sqlite", "xml", "sqlitefaster"]

[[test]]
name = "sqlite"
path = "tests/sqlite.rs"
required-features = ["sqlite"]

[[test]]
name = "sqlitefaster"
path = "tests/sqlitefaster.rs"
required-features = ["sqlitefaster"]

[[test]]
name = "mysql"
path = "tests/mysql.rs"
Expand All @@ -81,4 +88,4 @@ required-features = ["postgresql"]
[[test]]
name = "all"
path = "tests/all.rs"
required-features = ["sqlite", "postgresql", "mysql", "xml"]
required-features = ["sqlite", "postgresql", "mysql", "xml", "sqlitefaster"]
198 changes: 117 additions & 81 deletions benches/benchmark.rs
Original file line number Diff line number Diff line change
@@ -1,81 +1,117 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};

fn uri_sqlite() -> String {
format!(
"sqlite://{}/tests/db/sqlite/complex_sample.gnucash?mode=ro",
env!("CARGO_MANIFEST_DIR")
)
}

fn uri_xml() -> String {
format!(
"{}/tests/db/xml/complex_sample.gnucash",
env!("CARGO_MANIFEST_DIR")
)
}

fn benchmark_sql_query(c: &mut Criterion) {
let rt = tokio::runtime::Runtime::new().unwrap();
let book = rt.block_on(async {
let query = rucash::SQLiteQuery::new(&uri_sqlite()).await.unwrap();
rucash::Book::new(query).await.unwrap()
});

c.bench_function("sql query", |b| {
b.to_async(&rt).iter(|| async {
book.accounts_contains_name_ignore_case(black_box("aS"))
.await
});
});
}

fn benchmark_vec_filter(c: &mut Criterion) {
let rt = tokio::runtime::Runtime::new().unwrap();
let book = rt.block_on(async {
let query = rucash::SQLiteQuery::new(&uri_sqlite()).await.unwrap();
rucash::Book::new(query).await.unwrap()
});

c.bench_function("vec filter", |b| {
b.to_async(&rt).iter(|| async {
let vec = book.accounts().await.unwrap();
let _: Vec<_> = vec
.into_iter()
.filter(|x| x.name.to_lowercase().contains(black_box("aS")))
.collect();
})
});
}

fn benchmark_xml_book(c: &mut Criterion) {
let rt = tokio::runtime::Runtime::new().unwrap();
let book = rt.block_on(async {
let query = rucash::XMLQuery::new(&uri_xml()).unwrap();
rucash::Book::new(query).await.unwrap()
});

c.bench_function("XMLBook", |b| {
b.to_async(&rt).iter(|| async { book.accounts().await })
});
}

fn benchmark_sqlite_book(c: &mut Criterion) {
let rt = tokio::runtime::Runtime::new().unwrap();
let book = rt.block_on(async {
let query = rucash::SQLiteQuery::new(&uri_sqlite()).await.unwrap();
rucash::Book::new(query).await.unwrap()
});

c.bench_function("SqliteBook", |b| {
b.to_async(&rt).iter(|| async { book.accounts().await })
});
}

criterion_group!(
benches,
benchmark_sql_query,
benchmark_vec_filter,
benchmark_xml_book,
benchmark_sqlite_book,
);
criterion_main!(benches);
use criterion::{black_box, criterion_group, criterion_main, Criterion};

fn uri_sqlite() -> String {
format!(
"sqlite://{}/tests/db/sqlite/complex_sample.gnucash?mode=ro",
env!("CARGO_MANIFEST_DIR")
)
}

fn uri_xml() -> String {
format!(
"{}/tests/db/xml/complex_sample.gnucash",
env!("CARGO_MANIFEST_DIR")
)
}

fn uri_sqlitefaster() -> String {
format!(
"file:/{}/tests/db/sqlite/complex_sample.gnucash",
env!("CARGO_MANIFEST_DIR")
)
}

fn benchmark_sql_query(c: &mut Criterion) {
let rt = tokio::runtime::Runtime::new().unwrap();
let book = rt.block_on(async {
let query = rucash::SQLiteQuery::new(&uri_sqlite()).await.unwrap();
rucash::Book::new(query).await.unwrap()
});

c.bench_function("sql query", |b| {
b.to_async(&rt).iter(|| async {
book.accounts_contains_name_ignore_case(black_box("aS"))
.await
});
});
}

fn benchmark_sql_faster_query(c: &mut Criterion) {
let rt = tokio::runtime::Runtime::new().unwrap();
let book = rt.block_on(async {
let query = rucash::SQLiteQueryFaster::new(&uri_sqlitefaster()).unwrap();
rucash::Book::new(query).await.unwrap()
});

c.bench_function("sql faster query", |b| {
b.to_async(&rt).iter(|| async {
book.accounts_contains_name_ignore_case(black_box("aS"))
.await
});
});
}

fn benchmark_vec_filter(c: &mut Criterion) {
let rt = tokio::runtime::Runtime::new().unwrap();
let book = rt.block_on(async {
let query = rucash::SQLiteQuery::new(&uri_sqlite()).await.unwrap();
rucash::Book::new(query).await.unwrap()
});

c.bench_function("vec filter", |b| {
b.to_async(&rt).iter(|| async {
let vec = book.accounts().await.unwrap();
let _: Vec<_> = vec
.into_iter()
.filter(|x| x.name.to_lowercase().contains(black_box("aS")))
.collect();
})
});
}

fn benchmark_xml_book(c: &mut Criterion) {
let rt = tokio::runtime::Runtime::new().unwrap();
let book = rt.block_on(async {
let query = rucash::XMLQuery::new(&uri_xml()).unwrap();
rucash::Book::new(query).await.unwrap()
});

c.bench_function("XMLBook", |b| {
b.to_async(&rt).iter(|| async { book.accounts().await })
});
}

fn benchmark_sqlite_book(c: &mut Criterion) {
let rt = tokio::runtime::Runtime::new().unwrap();
let book = rt.block_on(async {
let query = rucash::SQLiteQuery::new(&uri_sqlite()).await.unwrap();
rucash::Book::new(query).await.unwrap()
});

c.bench_function("SqliteBook", |b| {
b.to_async(&rt).iter(|| async { book.accounts().await })
});
}

fn benchmark_sqlitefaster_book(c: &mut Criterion) {
let rt = tokio::runtime::Runtime::new().unwrap();
let book = rt.block_on(async {
let query = rucash::SQLiteQueryFaster::new(&uri_sqlitefaster()).unwrap();
rucash::Book::new(query).await.unwrap()
});

c.bench_function("SqliteFasterBook", |b| {
b.to_async(&rt).iter(|| async { book.accounts().await })
});
}

criterion_group!(
benches,
benchmark_sql_query,
benchmark_sql_faster_query,
benchmark_vec_filter,
benchmark_xml_book,
benchmark_sqlite_book,
benchmark_sqlitefaster_book,
);
criterion_main!(benches);
16 changes: 9 additions & 7 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,27 @@ all: test build
build:
cargo build
test:
cargo test --features sqlite,postgresql,mysql,xml
cargo test --features sqlite,postgresql,mysql,xml,sqlitefaster
cargo test --features sqlite
cargo test --features sqlitefaster
cargo test --features postgresql
cargo test --features mysql
cargo test --features xml
cargo test --features sqlite,postgresql,mysql,xml,decimal
cargo test --features sqlite,postgresql,mysql,xml,sqlitefaster,decimal
cargo test --features sqlite,decimal
cargo test --features sqlitefaster,decimal
cargo test --features postgresql,decimal
cargo test --features mysql,decimal
cargo test --features xml,decimal
clean:
cargo clean
bench:
cargo bench --features sqlite,xml
cargo bench --features sqlite,xml,sqlitefaster
check:
cargo check --features sqlite,postgresql,mysql,xml --all-targets
cargo clippy --features sqlite,postgresql,mysql,xml --all-targets
cargo check --features sqlite,postgresql,mysql,xml,decimal --all-targets
cargo clippy --features sqlite,postgresql,mysql,xml,decimal --all-targets
cargo check --features sqlite,postgresql,mysql,xml,sqlitefaster --all-targets
cargo clippy --features sqlite,postgresql,mysql,xml,sqlitefaster --all-targets
cargo check --features sqlite,postgresql,mysql,xml,sqlitefaster,decimal --all-targets
cargo clippy --features sqlite,postgresql,mysql,xml,sqlitefaster,decimal --all-targets
checkschema:
export DATABASE_URL=sqlite://tests/db/sqlite/complex_sample.gnucash?mode=ro
cargo check --features sqlite,schema --all-targets
Expand Down
4 changes: 4 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ pub enum Error {
#[error("SQLx error: {0}")]
Sql(#[from] sqlx::Error),

#[cfg(feature = "sqlitefaster")]
#[error("rusqlite error: {0}")]
Rusqlite(#[from] rusqlite::Error),

#[cfg(feature = "xml")]
#[error("XML error: {0}")]
XML(#[from] xmltree::Error),
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ pub use query::mysql::MySQLQuery;
pub use query::postgresql::PostgreSQLQuery;
#[cfg(feature = "sqlite")]
pub use query::sqlite::SQLiteQuery;
#[cfg(feature = "sqlitefaster")]
pub use query::sqlitefaster::SQLiteQuery as SQLiteQueryFaster;
#[cfg(feature = "xml")]
pub use query::xml::XMLQuery;
pub use query::Query;
2 changes: 2 additions & 0 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ pub(crate) mod mysql;
pub(crate) mod postgresql;
#[cfg(feature = "sqlite")]
pub(crate) mod sqlite;
#[cfg(feature = "sqlitefaster")]
pub(crate) mod sqlitefaster;
#[cfg(feature = "xml")]
pub(crate) mod xml;

Expand Down
Loading

0 comments on commit b2ef058

Please sign in to comment.