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)
This commit is contained in:
Damien Elmes 2020-03-27 09:59:48 +10:00
parent 2f4e35d566
commit 82ed288dc5
3 changed files with 43 additions and 0 deletions

View File

@ -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"

View File

@ -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;

View File

@ -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()
});
}
}