1
use std::path::Path;
2

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

            
8
use crate::collections::ResizableDocument;
9

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

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

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