Skip to content

Commit

Permalink
Adds benches and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Blocs committed Feb 29, 2020
1 parent 81f1ef2 commit c262d2c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 14 deletions.
51 changes: 37 additions & 14 deletions benches/kvstore_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,50 @@ use criterion::{criterion_group, criterion_main, Criterion};

use lucid::kvstore::KvStore;

fn set_10_kb_data_without_encryption(c: &mut Criterion) {
let kv = KvStore::new(None);
const CIPHER: std::option::Option<[&str; 2]> = Some([
"123456789012345678901234123456789012345678901234",
"f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
]);

const DATA: [u8; 1000] = [42u8; 1000];

let data = [42u8; 10000];
fn set_1_kb_data(c: &mut Criterion) {
let kv = KvStore::new(CIPHER);

c.bench_function("Set 10KB (no encrytion)", |b| {
b.iter(|| kv.set("bench_one".to_string(), data.to_vec()))
c.bench_function("Set 1KB", |b| {
b.iter(|| kv.set("bench_one".to_string(), DATA.to_vec()))
});
}
fn set_10_kb_data(c: &mut Criterion) {
let kv = KvStore::new(Some([
"123456789012345678901234123456789012345678901234",
"f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
]));
fn get_1_kb_data(c: &mut Criterion) {
let kv = KvStore::new(CIPHER);

let k = String::from("bench_one");
kv.set(k.clone(), DATA.to_vec());

c.bench_function("Get 1KB", |b| b.iter(|| kv.get(k.clone())));
}

let data = [42u8; 10000];
fn set_1_kb_data_without_encryption(c: &mut Criterion) {
let kv = KvStore::new(None);

c.bench_function("Set 10KB", |b| {
b.iter(|| kv.set("bench_one".to_string(), data.to_vec()))
c.bench_function("Set 1KB (w/o encrytion)", |b| {
b.iter(|| kv.set("bench_one".to_string(), DATA.to_vec()))
});
}
fn get_1_kb_data_without_encryption(c: &mut Criterion) {
let kv = KvStore::new(None);

let k = String::from("bench_one");
kv.set(k.clone(), DATA.to_vec());

c.bench_function("Get 1KB (w/o encryption)", |b| b.iter(|| kv.get(k.clone())));
}

criterion_group!(benches, set_10_kb_data, set_10_kb_data_without_encryption);
criterion_group!(
benches,
set_1_kb_data,
get_1_kb_data,
set_1_kb_data_without_encryption,
get_1_kb_data_without_encryption
);
criterion_main!(benches);
32 changes: 32 additions & 0 deletions tests/kvstore_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use lucid::kvstore::KvStore;

const CIPHER: std::option::Option<[&str; 2]> = Some([
"123456789012345678901234123456789012345678901234",
"f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
]);

const DATA: [u8; 512] = [42u8; 512];

const KEY = String::from("test_value");

fn init_kv() -> KvStore {
let kv = KvStore::new(CIPHER);
kv.set(KEY.clone(), DATA.to_vec());
kv
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn get_returns_a_value() {
let kv = init_kv();
let value = kv.get(KEY.clone());

match value {
Some(v) => assert_eq!(v.data, DATA.to_vec()),
None => panic!("No value found"),
}
}
}

0 comments on commit c262d2c

Please sign in to comment.