1
use std::path::Path;
2

            
3
use criterion::{measurement::WallTime, BenchmarkGroup, BenchmarkId};
4
use rusqlite::{params, Connection};
5
use ubyte::ToByteUnit;
6

            
7
use crate::collections::ResizableDocument;
8

            
9
5
pub(super) fn save_documents(group: &mut BenchmarkGroup<WallTime>, doc: &ResizableDocument) {
10
5
    group.bench_function(BenchmarkId::new("rusqlite", doc.data.len().bytes()), |b| {
11
5
        let file = Path::new("benches-collections.sqlite3");
12
5
        if file.exists() {
13
4
            std::fs::remove_file(file).unwrap();
14
4
        }
15
        // For fair testing, this needs to use ACID-compliant settings that a
16
        // user would use in production. While a WAL might be used in
17
        // production, it alters more than just insert performance. A more
18
        // complete benchmark which includes both inserts and queries would be
19
        // better to compare roots against sqlite's WAL performance.
20
5
        let sqlite = Connection::open(file).unwrap();
21
5
        // Sets the journal to what seems to be the most optimal, safe setting
22
5
        // for @ecton. See:
23
5
        // https://www.sqlite.org/pragma.html#pragma_journal_mode
24
5
        sqlite
25
5
            .pragma_update(None, "journal_mode", &"TRUNCATE")
26
5
            .unwrap();
27
5
        // Sets synchronous to NORMAL, which "should" be safe and provides
28
5
        // better performance. See:
29
5
        // https://www.sqlite.org/pragma.html#pragma_synchronous
30
5
        sqlite
31
5
            .pragma_update(None, "synchronous", &"NORMAL")
32
5
            .unwrap();
33
5
        sqlite
34
5
            .execute(
35
5
                "create table save_documents (id integer primary key autoincrement, data BLOB)",
36
5
                [],
37
5
            )
38
5
            .unwrap();
39
5

            
40
5
        let mut prepared = sqlite
41
5
            .prepare("insert into save_documents (data) values (?)")
42
5
            .unwrap();
43
5
        b.iter(|| {
44
5
            prepared.execute(params![doc.data.as_slice()]).unwrap();
45
5
        });
46
5
    });
47
5

            
48
5
    // TODO bench read performance
49
5
    // TODO bench read + write performance (with different numbers of readers/writers)
50
5
    // TODO (once supported) bench batch saving
51
5
}