view examples/stresstest/src/main.rs @ 566:bc90b6a413d5

Attempt fixing tests on windows based on path separators
author Lewin Bormann <lbo@spheniscida.de>
date Fri, 08 Jul 2022 13:04:30 -0700
parents 648cd1834556
children 68887f0a3e83
line wrap: on
line source

use rand::distributions::{Alphanumeric, DistString};
use rusty_leveldb::{Options, DB};

const KEY_LEN: usize = 4;
const VAL_LEN: usize = 8;

fn gen_string(n: usize) -> String {
    Alphanumeric
        .sample_string(&mut rand::thread_rng(), n)
        .to_lowercase()
}

fn write(db: &mut DB, n: usize) {
    time_test::time_test!("write");
    for i in 0..n {
        let (k, v) = (gen_string(KEY_LEN), gen_string(VAL_LEN));

        db.put(k.as_bytes(), v.as_bytes()).unwrap();
    }

    {
        time_test::time_test!("write-flush");
        db.flush().unwrap();
    }
}

fn read(db: &mut DB, n: usize) -> usize {
    let mut succ = 0;
    time_test::time_test!("read");
    for i in 0..n {
        let k = gen_string(KEY_LEN);

        if let Some(v) = db.get(k.as_bytes()) {
            succ += 1;
        }
    }
    succ
}

fn main() {
    let N = 100_000;
    let m = 10;
    let path = "stresstestdb";
    let mut entries = 0;

    for i in 0..m {
        let mut opt = Options::default();
        opt.compression_type = rusty_leveldb::CompressionType::CompressionSnappy;
        let mut db = DB::open(path, opt).unwrap();
        write(&mut db, N);
        entries += N;
        println!("Wrote {} entries ({}/{})", entries, i + 1, m);

        let s = read(&mut db, N);
        println!("Read back {} entries (found {}) ({}/{})", N, s, i + 1, m);
    }
}