From ec48f3db76f397cacc5364fd8c2322c165c9b1ea Mon Sep 17 00:00:00 2001 From: Christopher Berner Date: Sun, 29 Dec 2024 14:52:02 -0800 Subject: [PATCH] Refactor benchmark code This will make it easier to test compaction on rocksdb --- benches/common.rs | 14 ++++- benches/int_benchmark.rs | 4 +- benches/large_values_benchmark.rs | 4 +- benches/lmdb_benchmark.rs | 90 ++++++++++++++----------------- 4 files changed, 56 insertions(+), 56 deletions(-) diff --git a/benches/common.rs b/benches/common.rs index c7d663f8..bd4b9e81 100644 --- a/benches/common.rs +++ b/benches/common.rs @@ -23,6 +23,11 @@ pub trait BenchDatabase { fn write_transaction(&self) -> Self::W<'_>; fn read_transaction(&self) -> Self::R<'_>; + + // Returns a boolean indicating whether compaction is supported + fn compact(&mut self) -> bool { + false + } } pub trait BenchWriteTransaction { @@ -77,12 +82,12 @@ pub trait BenchIterator { } pub struct RedbBenchDatabase<'a> { - db: &'a redb::Database, + db: &'a mut redb::Database, } impl<'a> RedbBenchDatabase<'a> { #[allow(dead_code)] - pub fn new(db: &'a redb::Database) -> Self { + pub fn new(db: &'a mut redb::Database) -> Self { RedbBenchDatabase { db } } } @@ -104,6 +109,11 @@ impl<'a> BenchDatabase for RedbBenchDatabase<'a> { let txn = self.db.begin_read().unwrap(); RedbBenchReadTransaction { txn } } + + fn compact(&mut self) -> bool { + self.db.compact().unwrap(); + true + } } pub struct RedbBenchReadTransaction { diff --git a/benches/int_benchmark.rs b/benches/int_benchmark.rs index 4ba25430..40d55b27 100644 --- a/benches/int_benchmark.rs +++ b/benches/int_benchmark.rs @@ -60,8 +60,8 @@ fn benchmark(db: T) -> Vec<(&'static str, Duration)> { fn main() { let redb_results = { let tmpfile: NamedTempFile = NamedTempFile::new_in(current_dir().unwrap()).unwrap(); - let db = redb::Database::create(tmpfile.path()).unwrap(); - let table = RedbBenchDatabase::new(&db); + let mut db = redb::Database::create(tmpfile.path()).unwrap(); + let table = RedbBenchDatabase::new(&mut db); benchmark(table) }; diff --git a/benches/large_values_benchmark.rs b/benches/large_values_benchmark.rs index 37789905..781af434 100644 --- a/benches/large_values_benchmark.rs +++ b/benches/large_values_benchmark.rs @@ -71,8 +71,8 @@ fn benchmark(db: T) -> Vec<(&'static str, Duration)> { fn main() { let redb_latency_results = { let tmpfile: NamedTempFile = NamedTempFile::new_in(current_dir().unwrap()).unwrap(); - let db = redb::Database::builder().create(tmpfile.path()).unwrap(); - let table = RedbBenchDatabase::new(&db); + let mut db = redb::Database::builder().create(tmpfile.path()).unwrap(); + let table = RedbBenchDatabase::new(&mut db); benchmark(table) }; diff --git a/benches/lmdb_benchmark.rs b/benches/lmdb_benchmark.rs index 8ac3c14e..b7d635e7 100644 --- a/benches/lmdb_benchmark.rs +++ b/benches/lmdb_benchmark.rs @@ -73,10 +73,10 @@ fn make_rng_shards(shards: usize, elements: usize) -> Vec { rngs } -fn benchmark(db: T) -> Vec<(String, ResultType)> { +fn benchmark(db: T, path: &Path) -> Vec<(String, ResultType)> { let mut rng = make_rng(); let mut results = Vec::new(); - let db = Arc::new(db); + let mut db = Arc::new(db); let start = Instant::now(); let mut txn = db.write_transaction(); @@ -285,6 +285,38 @@ fn benchmark(db: T) -> Vec<(String, ResultType)> ); results.push(("removals".to_string(), ResultType::Duration(duration))); + let start = Instant::now(); + if Arc::get_mut(&mut db).unwrap().compact() { + let end = Instant::now(); + let duration = end - start; + println!( + "{}: Compacted in {}ms", + T::db_type_name(), + duration.as_millis() + ); + results.push(("compaction".to_string(), ResultType::Duration(duration))); + } else { + results.push(("compaction".to_string(), ResultType::NA)); + } + { + let mut txn = db.write_transaction(); + let mut inserter = txn.get_inserter(); + let (key, value) = gen_pair(&mut rng); + inserter.insert(&key, &value).unwrap(); + drop(inserter); + txn.commit().unwrap(); + } + let size = database_size(path); + println!( + "{}: Final file size {}ms", + T::db_type_name(), + ResultType::SizeInBytes(size) + ); + results.push(( + "size after bench".to_string(), + ResultType::SizeInBytes(size), + )); + results } @@ -336,22 +368,8 @@ fn main() { .set_cache_size(CACHE_SIZE) .create(tmpfile.path()) .unwrap(); - let table = RedbBenchDatabase::new(&db); - let mut results = benchmark(table); - - let start = Instant::now(); - db.compact().unwrap(); - let end = Instant::now(); - let duration = end - start; - println!("redb: Compacted in {}ms", duration.as_millis()); - results.push(("compaction".to_string(), ResultType::Duration(duration))); - - let size = database_size(tmpfile.path()); - results.push(( - "size after bench".to_string(), - ResultType::SizeInBytes(size), - )); - results + let table = RedbBenchDatabase::new(&mut db); + benchmark(table, tmpfile.path()) }; let lmdb_results = { @@ -363,14 +381,7 @@ fn main() { .unwrap() }; let table = HeedBenchDatabase::new(&env); - let mut results = benchmark(table); - results.push(("compaction".to_string(), ResultType::NA)); - let size = database_size(tmpfile.path()); - results.push(( - "size after bench".to_string(), - ResultType::SizeInBytes(size), - )); - results + benchmark(table, tmpfile.path()) }; let rocksdb_results = { @@ -385,14 +396,7 @@ fn main() { let db = rocksdb::TransactionDB::open(&opts, &Default::default(), tmpfile.path()).unwrap(); let table = RocksdbBenchDatabase::new(&db); - let mut results = benchmark(table); - results.push(("compaction".to_string(), ResultType::NA)); - let size = database_size(tmpfile.path()); - results.push(( - "size after bench".to_string(), - ResultType::SizeInBytes(size), - )); - results + benchmark(table, tmpfile.path()) }; let sled_results = { @@ -405,14 +409,7 @@ fn main() { .unwrap(); let table = SledBenchDatabase::new(&db, tmpfile.path()); - let mut results = benchmark(table); - results.push(("compaction".to_string(), ResultType::NA)); - let size = database_size(tmpfile.path()); - results.push(( - "size after bench".to_string(), - ResultType::SizeInBytes(size), - )); - results + benchmark(table, tmpfile.path()) }; let sanakirja_results = { @@ -420,14 +417,7 @@ fn main() { fs::remove_file(tmpfile.path()).unwrap(); let db = sanakirja::Env::new(tmpfile.path(), 4096 * 1024 * 1024, 2).unwrap(); let table = SanakirjaBenchDatabase::new(&db); - let mut results = benchmark(table); - results.push(("compaction".to_string(), ResultType::NA)); - let size = database_size(tmpfile.path()); - results.push(( - "size after bench".to_string(), - ResultType::SizeInBytes(size), - )); - results + benchmark(table, tmpfile.path()) }; fs::remove_dir_all(&tmpdir).unwrap();