From 82ed288dc53fda9d2dc47046727b43d2c3685e53 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Fri, 27 Mar 2020 09:59:48 +1000 Subject: [PATCH] add benchmark for vec cache test storage::sqlite::bench::bench_hash_cache ... bench: 399 ns/iter (+/- 27) test storage::sqlite::bench::bench_no_cache ... bench: 4,854 ns/iter (+/- 499) test storage::sqlite::bench::bench_vec_cache ... bench: 0 ns/iter (+/- 0) --- rslib/Cargo.toml | 3 +++ rslib/src/lib.rs | 1 + rslib/src/storage/sqlite.rs | 39 +++++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/rslib/Cargo.toml b/rslib/Cargo.toml index 4cdf2491a..a3163bb79 100644 --- a/rslib/Cargo.toml +++ b/rslib/Cargo.toml @@ -5,6 +5,9 @@ edition = "2018" authors = ["Ankitects Pty Ltd and contributors"] license = "AGPL-3.0-or-later" +[features] +unstable = [] + [dependencies] nom = "5.0.1" failure = "0.1.7" diff --git a/rslib/src/lib.rs b/rslib/src/lib.rs index 87253f998..02a040cf9 100644 --- a/rslib/src/lib.rs +++ b/rslib/src/lib.rs @@ -2,6 +2,7 @@ // License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html #![deny(unused_must_use)] +#![cfg_attr(feature = "unstable", feature(test))] mod backend_proto; diff --git a/rslib/src/storage/sqlite.rs b/rslib/src/storage/sqlite.rs index e71f74cb1..d49ad3f20 100644 --- a/rslib/src/storage/sqlite.rs +++ b/rslib/src/storage/sqlite.rs @@ -379,3 +379,42 @@ impl StorageContext<'_> { Ok(*self.timing_today.as_ref().unwrap()) } } + +#[cfg(all(feature = "unstable", test))] +mod bench { + extern crate test; + use super::{CachedStatementKind, SqliteStorage}; + use std::path::Path; + use test::Bencher; + + const SQL: &str = "insert or replace into cards + (id, nid, did, ord, mod, usn, type, queue, due, ivl, factor, + reps, lapses, left, odue, odid, flags, data) + values + (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; + + #[bench] + fn bench_no_cache(b: &mut Bencher) { + let storage = SqliteStorage::open_or_create(Path::new(":memory:")).unwrap(); + b.iter(|| storage.db.prepare(SQL).unwrap()); + } + + #[bench] + fn bench_hash_cache(b: &mut Bencher) { + let storage = SqliteStorage::open_or_create(Path::new(":memory:")).unwrap(); + b.iter(|| storage.db.prepare_cached(SQL).unwrap()); + } + + #[bench] + fn bench_vec_cache(b: &mut Bencher) { + let storage = SqliteStorage::open_or_create(Path::new(":memory:")).unwrap(); + let mut ctx = storage.context(false); + b.iter(|| { + ctx.with_cached_stmt(CachedStatementKind::GetCard, SQL, |_stmt| { + test::black_box(_stmt); + Ok(()) + }) + .unwrap() + }); + } +}