mirror of
https://github.com/facebook/sapling.git
synced 2024-10-16 03:39:48 +03:00
b9b1f1e907
Summary: `Index::open` now takes too many parameters, which is not very convenient to use. Inspired by `fs::OpenOptions`, use a dedicated strut for specifying open options. Motivation: To test checksum ability more confidently, I'd like to write something that randomly mutates 1 byte from a sane index. To make sure the checksum coverage is "correct", checksum chunk size is another parameter. Reviewed By: DurhamG Differential Revision: D7464182 fbshipit-source-id: 469ce7d1cfa5de3946028418567a9f3e2bc303fa
103 lines
3.0 KiB
Rust
103 lines
3.0 KiB
Rust
extern crate indexedlog;
|
|
extern crate minibench;
|
|
extern crate rand;
|
|
extern crate tempdir;
|
|
|
|
use indexedlog::index::OpenOptions;
|
|
use minibench::{bench, elapsed};
|
|
use rand::{ChaChaRng, Rng};
|
|
use tempdir::TempDir;
|
|
|
|
const N: usize = 20480;
|
|
|
|
/// Generate random buffer
|
|
fn gen_buf(size: usize) -> Vec<u8> {
|
|
let mut buf = vec![0u8; size];
|
|
ChaChaRng::new_unseeded().fill_bytes(buf.as_mut());
|
|
buf
|
|
}
|
|
|
|
/// Default open options: 4K checksum chunk
|
|
fn open_opts() -> OpenOptions {
|
|
let mut open_opts = OpenOptions::new();
|
|
open_opts.checksum_chunk_size(4096);
|
|
open_opts
|
|
}
|
|
|
|
fn main() {
|
|
bench("index insertion", || {
|
|
let dir = TempDir::new("index").expect("TempDir::new");
|
|
let mut idx = open_opts().open(dir.path().join("i")).expect("open");
|
|
let buf = gen_buf(N * 20);
|
|
elapsed(move || {
|
|
for i in 0..N {
|
|
idx.insert(&&buf[20 * i..20 * (i + 1)], i as u64)
|
|
.expect("insert");
|
|
}
|
|
})
|
|
});
|
|
|
|
bench("index flush", || {
|
|
let dir = TempDir::new("index").expect("TempDir::new");
|
|
let mut idx = open_opts().open(dir.path().join("i")).expect("open");
|
|
let buf = gen_buf(N * 20);
|
|
for i in 0..N {
|
|
idx.insert(&&buf[20 * i..20 * (i + 1)], i as u64)
|
|
.expect("insert");
|
|
}
|
|
elapsed(|| {
|
|
idx.flush().expect("flush");
|
|
})
|
|
});
|
|
|
|
bench("index lookup (memory)", || {
|
|
let dir = TempDir::new("index").expect("TempDir::new");
|
|
let mut idx = open_opts().open(dir.path().join("i")).expect("open");
|
|
let buf = gen_buf(N * 20);
|
|
for i in 0..N {
|
|
idx.insert(&&buf[20 * i..20 * (i + 1)], i as u64)
|
|
.expect("insert");
|
|
}
|
|
elapsed(move || {
|
|
for i in 0..N {
|
|
idx.get(&&buf[20 * i..20 * (i + 1)]).expect("lookup");
|
|
}
|
|
})
|
|
});
|
|
|
|
bench("index lookup (disk, no verify)", || {
|
|
let dir = TempDir::new("index").expect("TempDir::new");
|
|
let mut idx = open_opts()
|
|
.checksum_chunk_size(0)
|
|
.open(dir.path().join("i"))
|
|
.expect("open");
|
|
let buf = gen_buf(N * 20);
|
|
for i in 0..N {
|
|
idx.insert(&&buf[20 * i..20 * (i + 1)], i as u64)
|
|
.expect("insert");
|
|
}
|
|
idx.flush().expect("flush");
|
|
elapsed(move || {
|
|
for i in 0..N {
|
|
idx.get(&&buf[20 * i..20 * (i + 1)]).expect("lookup");
|
|
}
|
|
})
|
|
});
|
|
|
|
bench("index lookup (disk, verified)", || {
|
|
let dir = TempDir::new("index").expect("TempDir::new");
|
|
let mut idx = open_opts().open(dir.path().join("i")).expect("open");
|
|
let buf = gen_buf(N * 20);
|
|
for i in 0..N {
|
|
idx.insert(&&buf[20 * i..20 * (i + 1)], i as u64)
|
|
.expect("insert");
|
|
}
|
|
idx.flush().expect("flush");
|
|
elapsed(move || {
|
|
for i in 0..N {
|
|
idx.get(&&buf[20 * i..20 * (i + 1)]).expect("lookup");
|
|
}
|
|
})
|
|
});
|
|
}
|