aboutsummaryrefslogtreecommitdiffstats
path: root/log_db/benches/utils.rs
blob: a62162efbe09a75a890b087774b86ed9c418d46b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use log_db::*;
use rand::distributions::Alphanumeric;
use rand::Rng;
use std::fmt::Debug;
use std::io;

// Function to generate a random integer
pub fn random_int(from: i64, to: i64) -> i64 {
    let mut rng = rand::thread_rng();
    rng.gen_range(from..to)
}

// Function to generate a random string
pub fn random_string(len: usize) -> String {
    let mut rng = rand::thread_rng();
    (0..len).map(|_| rng.sample(Alphanumeric) as char).collect()
}

// Function to generate random bytes
pub fn random_bytes(len: usize) -> Vec<u8> {
    let mut rng = rand::thread_rng();
    (0..len).map(|_| rng.gen()).collect()
}

// Function to generate a random record
pub fn random_record(from_id: i64, to_id: i64) -> Record {
    Record {
        values: vec![
            Value::Int(random_int(from_id, to_id)), // Random int value between 0..1000
            Value::String(random_string(5)),        // Random string of length 5
            Value::Bytes(random_bytes(10)),         // Random bytes of length 10
        ],
    }
}

pub fn prefill_db<T: Eq + Clone + Debug>(
    db: &mut DB<T>,
    n_records: usize,
    compact: bool,
) -> Result<(), io::Error> {
    for _ in 0..n_records {
        let record = random_record(0, n_records as i64);
        db.upsert(&record)?;
        if compact {
            db.do_maintenance_tasks()?;
        }
    }

    Ok(())
}