1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use rand::prelude::*;
use rand_isaac::isaac::IsaacRng;
#[derive(Clone, Debug)]
pub struct KvGenerator {
key_len: usize,
value_len: usize,
rng: IsaacRng,
}
impl KvGenerator {
pub fn new(key_len: usize, value_len: usize) -> KvGenerator {
KvGenerator {
key_len,
value_len,
rng: SeedableRng::from_entropy(),
}
}
pub fn with_seed(key_len: usize, value_len: usize, seed: u64) -> KvGenerator {
KvGenerator {
key_len,
value_len,
rng: IsaacRng::seed_from_u64(seed),
}
}
pub fn generate(self, n: usize) -> Vec<(Vec<u8>, Vec<u8>)> {
self.take(n).collect()
}
}
impl Iterator for KvGenerator {
type Item = (Vec<u8>, Vec<u8>);
fn next(&mut self) -> Option<(Vec<u8>, Vec<u8>)> {
let mut k = vec![0; self.key_len];
self.rng.fill_bytes(&mut k);
let mut v = vec![0; self.value_len];
self.rng.fill_bytes(&mut v);
Some((k, v))
}
}
#[cfg(test)]
mod tests {
use super::*;
use test::Bencher;
#[bench]
fn bench_kv_generator(b: &mut Bencher) {
let mut g = KvGenerator::new(100, 1000);
b.iter(|| g.next());
}
}