Mercurial > lbo > hg > leveldb-rs
changeset 564:648cd1834556
Add stress test binary
This writes and reads back large numbers of keys, testing mostly for crashes.
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Thu, 07 Jul 2022 10:45:09 -0700 |
parents | 63b1f80e40d5 |
children | 370e82ed1c29 |
files | Cargo.toml examples/stresstest/Cargo.toml examples/stresstest/src/main.rs |
diffstat | 3 files changed, 70 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/Cargo.toml Fri Jul 01 21:02:27 2022 -0700 +++ b/Cargo.toml Thu Jul 07 10:45:09 2022 -0700 @@ -30,5 +30,5 @@ path = "src/benches/maps_bench.rs" [workspace] -members = ["examples/write-a-lot", "examples/leveldb-tool", "examples/word-analyze", "examples/kvserver"] +members = ["examples/write-a-lot", "examples/leveldb-tool", "examples/word-analyze", "examples/kvserver", "examples/stresstest"]
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/stresstest/Cargo.toml Thu Jul 07 10:45:09 2022 -0700 @@ -0,0 +1,12 @@ +[package] +name = "stresstest" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +rand = "0.8.5" + +rusty-leveldb = { path = "../../" } +time-test = "0.2.3"
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/stresstest/src/main.rs Thu Jul 07 10:45:09 2022 -0700 @@ -0,0 +1,57 @@ +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); + } +}