chgk_ledb/benches/db_bench.rs
Dmitry 7efd03d624
All checks were successful
continuous-integration/drone/push Build is passing
add db_bench
2022-10-16 01:29:37 +03:00

94 lines
2.4 KiB
Rust

#[macro_use]
extern crate criterion;
#[path = "../src/db.rs"]
mod db;
use std::{path::PathBuf};
use db::{Reader, Writer, WriterOpts};
use criterion::{BatchSize, Criterion};
use tempfile::tempdir;
#[derive(bincode::Encode, bincode::Decode, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
struct TestData {
num: u128,
test: String,
}
const N: usize = 4096;
fn gen_data(count: usize) -> impl Iterator<Item = TestData> {
(0..count)
.into_iter()
.map(|i| 143 + i as u128)
.map(|i| TestData {
num: i,
test: "test ---- Test ____".repeat(123 + i as usize % 15),
})
}
fn prepare_db_writer(path: &PathBuf) -> Writer<TestData> {
let opts = WriterOpts {
compress_lvl: 1,
data_buf_size: 100 * 1024 * 1024,
out_buf_size: 100 * 1024 * 1024,
current_buf_size: 10240,
};
Writer::new(path, opts).expect("new writer")
}
fn db_read(c: &mut Criterion) {
let dir = tempdir().expect("tempdir");
let tmpfile = dir.path().join("test.tmp");
let mut writer = prepare_db_writer(&tmpfile);
let mut items_iter = gen_data(N).collect::<Vec<TestData>>().into_iter();
writer.load(&mut items_iter).unwrap();
writer.finish().unwrap();
c.bench_function("read", |b| {
b.iter_batched(
|| {
let reader: Reader<TestData> = Reader::new(&tmpfile, 2048).expect("new reader");
reader
},
|mut reader| {
let mut reader_iter = reader.iter();
while let Some(item) = reader_iter.next() {
drop(item);
}
},
BatchSize::SmallInput,
)
});
}
fn db_write(c: &mut Criterion) {
let dir = tempdir().expect("tempdir");
let tmpfile = dir.path().join("test.tmp");
c.bench_function("write", |b| {
b.iter_batched(
|| {
let src = gen_data(N).collect::<Vec<TestData>>().into_iter();
let writer = prepare_db_writer(&tmpfile);
(src, writer)
},
|(mut src, mut writer)| {
writer.load(&mut src).unwrap();
writer.finish().unwrap();
},
BatchSize::SmallInput,
)
});
}
fn config() -> Criterion {
Criterion::default().sample_size(40)
}
criterion_group!{name=benches; config = config(); targets = db_read, db_write}
criterion_main!(benches);