Skip to content

Commit

Permalink
Refactor benchmark code
Browse files Browse the repository at this point in the history
This will make it easier to test compaction on rocksdb
  • Loading branch information
cberner committed Dec 29, 2024
1 parent 51ca54c commit ec48f3d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 56 deletions.
14 changes: 12 additions & 2 deletions benches/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 }
}
}
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions benches/int_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ fn benchmark<T: BenchDatabase>(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)
};

Expand Down
4 changes: 2 additions & 2 deletions benches/large_values_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ fn benchmark<T: BenchDatabase>(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)
};

Expand Down
90 changes: 40 additions & 50 deletions benches/lmdb_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ fn make_rng_shards(shards: usize, elements: usize) -> Vec<fastrand::Rng> {
rngs
}

fn benchmark<T: BenchDatabase + Send + Sync>(db: T) -> Vec<(String, ResultType)> {
fn benchmark<T: BenchDatabase + Send + Sync>(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();
Expand Down Expand Up @@ -285,6 +285,38 @@ fn benchmark<T: BenchDatabase + Send + Sync>(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
}

Expand Down Expand Up @@ -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 = {
Expand All @@ -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 = {
Expand All @@ -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 = {
Expand All @@ -405,29 +409,15 @@ 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 = {
let tmpfile: NamedTempFile = NamedTempFile::new_in(&tmpdir).unwrap();
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();
Expand Down

0 comments on commit ec48f3d

Please sign in to comment.