mod common;
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
use datawal::DataWal;
use tempfile::TempDir;
use crate::common::bench_tempdir;
const TOTAL_KEYS: usize = 10_000;
const LIVE_RATIOS: &[(&str, f64)] = &[("100", 1.0), ("50", 0.5), ("10", 0.1)];
const VALUE_SIZE: usize = 64;
fn key_for(i: usize) -> [u8; 16] {
let mut k = [0u8; 16];
k[..8].copy_from_slice(&(i as u64).to_le_bytes());
k
}
fn populated_with_deletes(live_ratio: f64) -> (TempDir, DataWal) {
let dir = bench_tempdir();
let mut kv = DataWal::open(dir.path()).expect("open");
let value = vec![0xCDu8; VALUE_SIZE];
for i in 0..TOTAL_KEYS {
kv.put(&key_for(i), &value).expect("put");
}
let kill_from = (TOTAL_KEYS as f64 * live_ratio) as usize;
for i in kill_from..TOTAL_KEYS {
kv.delete(&key_for(i)).expect("delete");
}
(dir, kv)
}
fn populated_with_overwrites(live_ratio: f64) -> (TempDir, DataWal) {
let dir = bench_tempdir();
let mut kv = DataWal::open(dir.path()).expect("open");
let value = vec![0xCDu8; VALUE_SIZE];
for i in 0..TOTAL_KEYS {
kv.put(&key_for(i), &value).expect("put");
}
let overwrites = ((TOTAL_KEYS as f64 / live_ratio) as usize).saturating_sub(TOTAL_KEYS);
for j in 0..overwrites {
let i = j % TOTAL_KEYS;
kv.put(&key_for(i), &value).expect("overwrite");
}
(dir, kv)
}
fn bench_compact_to_delete_heavy(c: &mut Criterion) {
let mut group = c.benchmark_group("datawal_compact_to_delete_heavy");
for &(label, ratio) in LIVE_RATIOS {
let (_src, mut kv) = populated_with_deletes(ratio);
group.bench_function(BenchmarkId::from_parameter(label), |b| {
b.iter_with_setup(bench_tempdir, |out_dir| {
let stats = kv.compact_to(out_dir.path()).expect("compact_to");
black_box(stats);
});
});
}
group.finish();
}
fn bench_compact_to_overwrite_heavy(c: &mut Criterion) {
let mut group = c.benchmark_group("datawal_compact_to_overwrite_heavy");
for &(label, ratio) in LIVE_RATIOS {
let (_src, mut kv) = populated_with_overwrites(ratio);
group.bench_function(BenchmarkId::from_parameter(label), |b| {
b.iter_with_setup(bench_tempdir, |out_dir| {
let stats = kv.compact_to(out_dir.path()).expect("compact_to");
black_box(stats);
});
});
}
group.finish();
}
fn bench_export_jsonl(c: &mut Criterion) {
let mut group = c.benchmark_group("datawal_export_jsonl");
for &(label, ratio) in LIVE_RATIOS {
let (_src, mut kv) = populated_with_deletes(ratio);
group.bench_function(BenchmarkId::from_parameter(label), |b| {
b.iter_with_setup(
|| {
let dir = bench_tempdir();
let path = dir.path().join("export.jsonl");
(dir, path)
},
|(dir, path)| {
kv.export_jsonl(&path).expect("export_jsonl");
black_box(&path);
drop(dir);
},
);
});
}
group.finish();
}
criterion_group!(
benches,
bench_compact_to_delete_heavy,
bench_compact_to_overwrite_heavy,
bench_export_jsonl
);
criterion_main!(benches);