ref bench

This commit is contained in:
Dmitry Belyaev 2023-07-19 14:40:25 +03:00
commit cc5ab52f32
Signed by: b4tman
GPG Key ID: 41A00BF15EA7E5F3

View File

@ -7,12 +7,12 @@ extern crate serde_json;
extern crate tempfile; extern crate tempfile;
use chgk_ledb_lib::db; use chgk_ledb_lib::db;
use std::path::PathBuf; use std::path::Path;
use db::{Reader, Writer, WriterOpts}; use db::{Reader, Writer, WriterOpts};
use criterion::{BatchSize, Criterion}; use criterion::{BatchSize, Criterion};
use tempfile::tempdir; use tempfile::{tempdir, NamedTempFile};
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
@ -37,17 +37,14 @@ struct TestData {
const N: usize = 4096; const N: usize = 4096;
fn gen_data(count: usize) -> impl Iterator<Item = TestData> { fn gen_data(count: usize) -> impl Iterator<Item = TestData> {
(0..count) (0..count).map(|i| 143 + i as u64).map(|i| TestData {
.into_iter()
.map(|i| 143 + i as u64)
.map(|i| TestData {
num1: i, num1: i,
num2: i * 100 ^ 0xDF0E441122334455, num2: (i * 100) ^ 0xDF0E441122334455,
test: "test ---- Test ____".repeat(123 + i as usize % 15), test: "test ---- Test ____".repeat(123 + i as usize % 15),
}) })
} }
fn prepare_db_writer(path: &PathBuf) -> Writer<TestData> { fn prepare_db_writer<P: AsRef<Path>>(path: P) -> Writer<TestData> {
let opts = WriterOpts { let opts = WriterOpts {
compress_lvl: 1, compress_lvl: 1,
data_buf_size: 100 * 1024 * 1024, data_buf_size: 100 * 1024 * 1024,
@ -60,7 +57,9 @@ fn prepare_db_writer(path: &PathBuf) -> Writer<TestData> {
fn db_read(c: &mut Criterion) { fn db_read(c: &mut Criterion) {
let dir = tempdir().expect("tempdir"); let dir = tempdir().expect("tempdir");
let tmpfile = dir.path().join("test.tmp"); let tmpfile = NamedTempFile::new_in(dir.path())
.expect("new tempfile")
.into_temp_path();
let mut writer = prepare_db_writer(&tmpfile); let mut writer = prepare_db_writer(&tmpfile);
let mut items_iter = gen_data(N).collect::<Vec<TestData>>().into_iter(); let mut items_iter = gen_data(N).collect::<Vec<TestData>>().into_iter();
@ -85,10 +84,13 @@ fn db_read(c: &mut Criterion) {
fn db_write(c: &mut Criterion) { fn db_write(c: &mut Criterion) {
let dir = tempdir().expect("tempdir"); let dir = tempdir().expect("tempdir");
let tmpfile = dir.path().join("test.tmp");
c.bench_function("write", |b| { c.bench_function("write", |b| {
b.iter_batched( b.iter_batched(
|| { || {
let tmpfile = NamedTempFile::new_in(dir.path())
.expect("new tempfile")
.into_temp_path();
let src = gen_data(N).collect::<Vec<TestData>>().into_iter(); let src = gen_data(N).collect::<Vec<TestData>>().into_iter();
let writer = prepare_db_writer(&tmpfile); let writer = prepare_db_writer(&tmpfile);
(src, writer) (src, writer)