1
#[cfg(feature = "compression")]
2
use bonsaidb::local::config::Compression;
3
use bonsaidb::{
4
    core::{connection::Connection, test_util::TestDirectory},
5
    local::{
6
        config::{Builder, StorageConfiguration},
7
        Database,
8
    },
9
};
10
use criterion::{measurement::WallTime, BenchmarkGroup, BenchmarkId};
11
use ubyte::ToByteUnit;
12

            
13
use crate::collections::ResizableDocument;
14

            
15
10
async fn save_document(doc: &ResizableDocument, db: &Database) {
16
10
    db.collection::<ResizableDocument>()
17
10
        .push(doc)
18
10
        .await
19
10
        .unwrap();
20
10
}
21

            
22
#[cfg_attr(not(feature = "compression"), allow(unused_mut))]
23
5
pub(super) fn save_documents(group: &mut BenchmarkGroup<WallTime>, doc: &ResizableDocument) {
24
5
    let path = TestDirectory::new("benches-basics.bonsaidb");
25
5
    let mut configs = vec![("bonsaidb-local", StorageConfiguration::new(&path))];
26
5
    #[cfg(feature = "compression")]
27
5
    {
28
5
        configs.push((
29
5
            "bonsaidb-local+lz4",
30
5
            StorageConfiguration::new(&path).default_compression(Compression::Lz4),
31
5
        ))
32
    }
33
15
    for (label, config) in configs {
34
10
        group.bench_function(BenchmarkId::new(label, doc.data.len().bytes()), |b| {
35
10
            let runtime = tokio::runtime::Runtime::new().unwrap();
36
10
            let db = runtime
37
10
                .block_on(Database::open::<ResizableDocument>(config.clone()))
38
10
                .unwrap();
39
10
            b.to_async(&runtime).iter(|| save_document(doc, &db));
40
10
        });
41
10
    }
42

            
43
    // TODO bench read performance
44
    // TODO bench read + write performance (with different numbers of readers/writers)
45
    // TODO (once supported) bench batch saving
46
5
}