Skip to content

Commit

Permalink
Add len() benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
cberner committed Mar 22, 2024
1 parent 5c398a5 commit 7377b3b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
28 changes: 27 additions & 1 deletion benches/common.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use redb::TableDefinition;
use redb::{AccessGuard, ReadableTable};
use redb::{ReadableTableMetadata, TableDefinition};
use rocksdb::{Direction, IteratorMode, TransactionDB, TransactionOptions, WriteOptions};
use sanakirja::btree::page_unsized;
use sanakirja::{Commit, RootDb};
Expand Down Expand Up @@ -52,6 +52,7 @@ pub trait BenchReadTransaction {
fn get_reader(&self) -> Self::T<'_>;
}

#[allow(clippy::len_without_is_empty)]
pub trait BenchReader {
type Output<'out>: AsRef<[u8]> + 'out
where
Expand All @@ -63,6 +64,8 @@ pub trait BenchReader {
fn get<'a>(&'a self, key: &[u8]) -> Option<Self::Output<'a>>;

fn range_from<'a>(&'a self, start: &'a [u8]) -> Self::Iterator<'a>;

fn len(&self) -> u64;
}

pub trait BenchIterator {
Expand Down Expand Up @@ -132,6 +135,10 @@ impl BenchReader for RedbBenchReader {
let iter = self.table.range(key..).unwrap();
RedbBenchIterator { iter }
}

fn len(&self) -> u64 {
self.table.len().unwrap()
}
}

pub struct RedbBenchIterator<'a> {
Expand Down Expand Up @@ -255,6 +262,10 @@ impl<'db> BenchReader for SledBenchReader<'db> {
let iter = self.db.range(key..);
SledBenchIterator { iter }
}

fn len(&self) -> u64 {
self.db.len() as u64
}
}

pub struct SledBenchIterator {
Expand Down Expand Up @@ -416,6 +427,11 @@ impl<'txn, 'db> BenchReader for LmdbRkvBenchReader<'txn, 'db> {

LmdbRkvBenchIterator { iter }
}

fn len(&self) -> u64 {
use lmdb::Transaction;
self.txn.stat(self.db).unwrap().entries() as u64
}
}

pub struct LmdbRkvBenchIterator<'a> {
Expand Down Expand Up @@ -526,6 +542,10 @@ impl<'db, 'txn> BenchReader for RocksdbBenchReader<'db, 'txn> {

RocksdbBenchIterator { iter }
}

fn len(&self) -> u64 {
self.snapshot.iterator(IteratorMode::Start).count() as u64
}
}

pub struct RocksdbBenchIterator<'a> {
Expand Down Expand Up @@ -662,6 +682,12 @@ impl<'db, 'txn> BenchReader for SanakirjaBenchReader<'db, 'txn> {

SanakirjaBenchIterator { iter }
}

fn len(&self) -> u64 {
sanakirja::btree::iter(self.txn, &self.table, None)
.unwrap()
.count() as u64
}
}

pub struct SanakirjaBenchIterator<'db, 'txn> {
Expand Down
10 changes: 10 additions & 0 deletions benches/lmdb_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,16 @@ fn benchmark<T: BenchDatabase + Send + Sync>(db: T) -> Vec<(String, Duration)> {

let txn = db.read_transaction();
{
{
let start = Instant::now();
let len = txn.get_reader().len();
assert_eq!(len, ELEMENTS as u64 + 100_000 + 100);
let end = Instant::now();
let duration = end - start;
println!("{}: len() in {}ms", T::db_type_name(), duration.as_millis());
results.push(("len()".to_string(), duration));
}

for _ in 0..ITERATIONS {
let mut rng = make_rng();
let start = Instant::now();
Expand Down

0 comments on commit 7377b3b

Please sign in to comment.