1
use criterion::{criterion_group, criterion_main, BatchSize, Criterion};
2
use rand::prelude::SliceRandom;
3
use rand::thread_rng;
4

            
5
1
fn benchmark(c: &mut Criterion) {
6
1
    push(c);
7
1
    get(c);
8
1
}
9

            
10
1
fn push(c: &mut Criterion) {
11
1
    let mut group = c.benchmark_group("push");
12
1

            
13
1
    let mut entries = Vec::new();
14
2002
    for i in 0_u32..=2000 {
15
2001
        entries.push(i);
16
2001
    }
17
2001
    for i in (0_u32..=2000).rev() {
18
2001
        entries.push(i);
19
2001
    }
20

            
21
1
    group.bench_function("ordered-lru", |b| {
22
1
        let mut lru = lrumap::LruBTreeMap::new(1000);
23
1
        b.iter_batched(
24
1
            || entries.clone(),
25
1
            |batch: Vec<u32>| {
26
4003
                for i in batch {
27
4002
                    lru.push(i, i);
28
4002
                }
29
1
            },
30
1
            BatchSize::NumIterations(u64::try_from(entries.len()).unwrap()),
31
1
        )
32
1
    });
33
1

            
34
1
    group.bench_function("hashed-lru", |b| {
35
1
        let mut lru = lrumap::LruHashMap::new(1000);
36
1
        b.iter_batched(
37
1
            || entries.clone(),
38
1
            |batch: Vec<u32>| {
39
4003
                for i in batch {
40
4002
                    lru.push(i, i);
41
4002
                }
42
1
            },
43
1
            BatchSize::NumIterations(u64::try_from(entries.len()).unwrap()),
44
1
        )
45
1
    });
46
1

            
47
1
    group.bench_function("lru", |b| {
48
1
        let mut lru = lru::LruCache::new(1000);
49
1
        b.iter_batched(
50
1
            || &entries[..],
51
1
            |batch: &[u32]| {
52
4003
                for i in batch {
53
4002
                    lru.push(i, i);
54
4002
                }
55
1
            },
56
1
            BatchSize::NumIterations(u64::try_from(entries.len()).unwrap()),
57
1
        )
58
1
    });
59
1
}
60

            
61
1
fn get(c: &mut Criterion) {
62
1
    let mut group = c.benchmark_group("get");
63
1

            
64
1
    let mut ordered = lrumap::LruBTreeMap::new(1000);
65
1
    let mut unordered = lrumap::LruHashMap::new(1000);
66
1
    let mut lru = lru::LruCache::new(1000);
67
1
    let mut indicies = Vec::new();
68
1002
    for i in 0_u32..=1000 {
69
1001
        indicies.push(i);
70
1001
        ordered.push(i, i);
71
1001
        unordered.push(i, i);
72
1001
        lru.push(i, i);
73
1001
    }
74

            
75
1
    indicies.shuffle(&mut thread_rng());
76
1

            
77
1
    group.bench_function("ordered-lru", |b| {
78
1
        b.iter_batched(
79
1
            || &indicies[..],
80
1
            |indicies: &[u32]| {
81
1002
                for i in indicies {
82
1001
                    ordered.get(i);
83
1001
                }
84
1
            },
85
1
            BatchSize::NumIterations(u64::try_from(indicies.len()).unwrap()),
86
1
        )
87
1
    });
88
1

            
89
1
    group.bench_function("hashed-lru", |b| {
90
1
        b.iter_batched(
91
1
            || &indicies[..],
92
1
            |indicies: &[u32]| {
93
1002
                for i in indicies {
94
1001
                    unordered.get(i);
95
1001
                }
96
1
            },
97
1
            BatchSize::NumIterations(u64::try_from(indicies.len()).unwrap()),
98
1
        )
99
1
    });
100
1

            
101
1
    group.bench_function("lru", |b| {
102
1
        b.iter_batched(
103
1
            || &indicies[..],
104
1
            |indicies: &[u32]| {
105
1002
                for i in indicies {
106
1001
                    lru.get(i);
107
1001
                }
108
1
            },
109
1
            BatchSize::NumIterations(u64::try_from(indicies.len()).unwrap()),
110
1
        )
111
1
    });
112
1
}
113

            
114
criterion_group!(suite, benchmark);
115
criterion_main!(suite);