#[macro_use] extern crate criterion; extern crate serde; extern crate serde_derive; extern crate serde_json; extern crate tempfile; use chgk_ledb_lib::db; use std::path::Path; use db::{Reader, Writer, WriterOpts}; use criterion::{BatchSize, Criterion}; use tempfile::{tempdir, NamedTempFile}; use serde_derive::{Deserialize, Serialize}; #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] struct TestData { num1: u64, num2: u64, test: String, } const N: usize = 4096; fn gen_data(count: usize) -> impl Iterator { (0..count).map(|i| 143 + i as u64).map(|i| TestData { num1: i, num2: (i * 100) ^ 0xDF0E441122334455, test: "test ---- Test ____".repeat(123 + i as usize % 15), }) } fn prepare_db_writer>(path: P) -> Writer { 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 = NamedTempFile::new_in(dir.path()) .expect("new tempfile") .into_temp_path(); let mut writer = prepare_db_writer(&tmpfile); let mut items_iter = gen_data(N).collect::>().into_iter(); writer.load(&mut items_iter).unwrap(); writer.finish().unwrap(); c.bench_function("read", |b| { b.iter_batched( || { let reader: Reader = Reader::new(&tmpfile, 2048).expect("new reader"); reader }, |reader| { for item in reader { drop(item); } }, BatchSize::SmallInput, ) }); } fn db_write(c: &mut Criterion) { let dir = tempdir().expect("tempdir"); c.bench_function("write", |b| { b.iter_batched( || { let tmpfile = NamedTempFile::new_in(dir.path()) .expect("new tempfile") .into_temp_path(); let src = gen_data(N).collect::>().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);