}
#[test]
fn lmdb() {
use lmdb_rs::*;
env_logger::try_init().unwrap_or(());
for i in 1..12 {
let n = i * 1_000_000;
std::fs::remove_dir_all("/tmp/sanakirja0").unwrap_or(());
std::fs::create_dir_all("/tmp/sanakirja0").unwrap();
let env = Env::new("/tmp/sanakirja0", 409_600_000, 2).unwrap();
let mut txn = Env::mut_txn_begin(&env).unwrap();
let mut db = create_db_::<MutTxn<&Env, ()>, u64, u64, P<u64, u64>>(&mut txn).unwrap();
let mut times = [0f64; 12];
let mut test = Vec::with_capacity(n);
let mut rng = rand::thread_rng();
for _ in 0..n {
use rand::Rng;
test.push((rng.gen(), rng.gen()))
}
let now = std::time::SystemTime::now();
for (k, v) in test.iter() {
assert!(put(&mut txn, &mut db, k, v).unwrap());
}
times[0] = now.elapsed().unwrap().as_secs_f64();
let now = std::time::SystemTime::now();
for (k, v) in test.iter() {
assert_eq!(btree::get(&txn, &db, &k, None).unwrap(), Some((k, v)))
}
times[1] = now.elapsed().unwrap().as_secs_f64();
let env = Env::new_anon(409_600_000, 2).unwrap();
let mut txn = Env::mut_txn_begin(&env).unwrap();
let mut db = create_db_::<MutTxn<&Env, ()>, u64, u64, P<u64, u64>>(&mut txn).unwrap();
let now = std::time::SystemTime::now();
for (k, v) in test.iter() {
assert!(put(&mut txn, &mut db, k, v).unwrap());
}
times[2] = now.elapsed().unwrap().as_secs_f64();
let now = std::time::SystemTime::now();
for (k, v) in test.iter() {
assert_eq!(btree::get(&txn, &db, &k, None).unwrap(), Some((k, v)))
}
times[3] = now.elapsed().unwrap().as_secs_f64();
let mut b = std::collections::BTreeMap::new();
let now = std::time::SystemTime::now();
for (k, v) in test.iter() {
b.insert(*k, *v);
}
times[4] = now.elapsed().unwrap().as_secs_f64();
let now = std::time::SystemTime::now();
for (k, v) in test.iter() {
assert_eq!(b.get(k), Some(v));
}
times[5] = now.elapsed().unwrap().as_secs_f64();
std::fs::remove_dir_all("/tmp/test-lmdb").unwrap_or(());
std::fs::create_dir_all("/tmp/test-lmdb").unwrap_or(());
let env = EnvBuilder::new()
.map_size(1 << 30)
.open("/tmp/test-lmdb", 0o777)
.unwrap();
let db_handle = env.get_default_db(lmdb_rs::core::DbIntKey).unwrap();
let txn = env.new_transaction().unwrap();
{
let db = txn.bind(&db_handle);
let now = std::time::SystemTime::now();
for (k, v) in test.iter() {
db.set(k, v).unwrap();
}
times[6] = now.elapsed().unwrap().as_secs_f64();
}
// Note: `commit` is choosen to be explicit as
// in case of failure it is responsibility of
// the client to handle the error
match txn.commit() {
Err(_) => panic!("failed to commit!"),
Ok(_) => (),
}
let reader = env.get_reader().unwrap();
let db = reader.bind(&db_handle);
let now = std::time::SystemTime::now();
for (k, v) in test.iter() {
let name = db.get::<u64>(k).ok();
assert_eq!(name, Some(*v))
}
times[7] = now.elapsed().unwrap().as_secs_f64();
std::fs::remove_dir_all("/tmp/test-sled").unwrap_or(());
std::fs::create_dir_all("/tmp/test-sled").unwrap_or(());
let db: sled::Db = sled::open("/tmp/test-sled").unwrap();
let now = std::time::SystemTime::now();
for (k, v) in test.iter() {
unsafe {
db.insert(
std::slice::from_raw_parts(k as *const u64 as *const u8, 8),
std::slice::from_raw_parts(v as *const u64 as *const u8, 8),
)
.unwrap();
}
}
times[8] = now.elapsed().unwrap().as_secs_f64();
let now = std::time::SystemTime::now();
for (k, _v) in test.iter() {
unsafe {
db.get(std::slice::from_raw_parts(k as *const u64 as *const u8, 8))
.unwrap();
}
}
times[9] = now.elapsed().unwrap().as_secs_f64();
{
use old_sanakirja::*;
std::fs::remove_dir_all("/tmp/sanakirja1").unwrap_or(());
std::fs::create_dir_all("/tmp/sanakirja1").unwrap();
let env = Env::new("/tmp/sanakirja1", 409_600_000).unwrap();
let mut txn = Env::mut_txn_begin(&env).unwrap();
let mut db = txn.create_db::<u64, u64>().unwrap();
let now = std::time::SystemTime::now();
let mut rng = rand::thread_rng();
for (k, v) in test.iter() {
assert!(txn.put(&mut rng, &mut db, *k, *v).unwrap());
}
times[10] = now.elapsed().unwrap().as_secs_f64();
let now = std::time::SystemTime::now();
for (k, v) in test.iter() {
assert_eq!(txn.get(&db, *k, None).unwrap(), Some(*v))
}
times[11] = now.elapsed().unwrap().as_secs_f64();
}
print!("{}", n);
for t in times.iter() {
print!(", {}", t)
}
println!();
}