aboutsummaryrefslogtreecommitdiff
path: root/src/crates/primrose/benches/criterion_benchmark.rs
blob: 6bdc21406479ad027dcf83f4205d79deb456e313 (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
use criterion::{criterion_group, criterion_main, Criterion};

use primrose_library::traits::Container;
use primrose_library::LazyUniqueVec;
use rand::rngs::StdRng;
use rand::seq::SliceRandom;

use rand::SeedableRng;
use std::collections::{BTreeSet, HashSet};
use std::mem::size_of;

use std::vec::Vec;

// one search at the
fn gen_dataset_1() -> Vec<u32> {
    // avoid duplication
    let size = 1024 * 1024; // 1 MB
    let amount = size / size_of::<u32>();
    let mut data: Vec<u32> = (1..amount as u32).collect(); //ensure no duplication
    let mut rng = StdRng::seed_from_u64(222);
    data.shuffle(&mut rng);
    data
}

fn gen_dataset_128() -> Vec<u32> {
    // avoid duplication
    let size = 128 * 1024 * 1024; // 128 MB
    let amount = size / size_of::<u32>();
    let mut data: Vec<u32> = (1..amount as u32).collect(); //ensure no duplication
    let mut rng = StdRng::seed_from_u64(222);
    data.shuffle(&mut rng);
    data
}

fn gen_dataset_256() -> Vec<u32> {
    // avoid duplication
    let size = 256 * 1024 * 1024; // 256 MB
    let amount = size / size_of::<u32>();
    let mut data: Vec<u32> = (1..amount as u32).collect(); //ensure no duplication
    let mut rng = StdRng::seed_from_u64(222);
    data.shuffle(&mut rng);
    data
}

fn gen_dataset_512() -> Vec<u32> {
    // avoid duplication
    let size = 512 * 1024 * 1024; // 512 MB
    let amount = size / size_of::<u32>();
    let mut data: Vec<u32> = (1..amount as u32).collect(); //ensure no duplication
    let mut rng = StdRng::seed_from_u64(222);
    data.shuffle(&mut rng);
    data
}

fn btreeset_insertion_1m(c: &mut Criterion) {
    let s: &mut dyn Container<u32> = &mut BTreeSet::new();
    let data = gen_dataset_1();
    c.bench_function("btreeset insertion 1MB", |b| {
        b.iter(|| {
            for val in data.iter() {
                s.insert(*val);
            }
            s.contains(&1024);
        })
    });
}

fn btreeset_insertion_128m(c: &mut Criterion) {
    let s: &mut dyn Container<u32> = &mut BTreeSet::new();
    let data = gen_dataset_128();
    c.bench_function("btreeset insertion 128MB", |b| {
        b.iter(|| {
            for val in data.iter() {
                s.insert(*val);
            }
            s.contains(&1024);
        })
    });
}

fn btreeset_insertion_256m(c: &mut Criterion) {
    let s: &mut dyn Container<u32> = &mut BTreeSet::new();
    let data = gen_dataset_256();
    c.bench_function("btreeset insertion 256MB", |b| {
        b.iter(|| {
            for val in data.iter() {
                s.insert(*val);
            }
            s.contains(&1024);
        })
    });
}

fn btreeset_insertion_512m(c: &mut Criterion) {
    let s: &mut dyn Container<u32> = &mut BTreeSet::new();
    let data = gen_dataset_512();
    c.bench_function("btreeset insertion 512MB", |b| {
        b.iter(|| {
            for val in data.iter() {
                s.insert(*val);
            }
            s.contains(&1024);
        })
    });
}

fn hashset_insertion_1m(c: &mut Criterion) {
    let s: &mut dyn Container<u32> = &mut HashSet::new();
    let data = gen_dataset_1();
    c.bench_function("hashset insertion 1MB", |b| {
        b.iter(|| {
            for val in data.iter() {
                s.insert(*val);
            }
            s.contains(&1024);
        })
    });
}

fn hashset_insertion_128m(c: &mut Criterion) {
    let s: &mut dyn Container<u32> = &mut HashSet::new();
    let data = gen_dataset_128();
    c.bench_function("hashset insertion 128MB", |b| {
        b.iter(|| {
            for val in data.iter() {
                s.insert(*val);
            }
            s.contains(&1024);
        })
    });
}

fn hashset_insertion_256m(c: &mut Criterion) {
    let s: &mut dyn Container<u32> = &mut HashSet::new();
    let data = gen_dataset_256();
    c.bench_function("hashset insertion 256MB", |b| {
        b.iter(|| {
            for val in data.iter() {
                s.insert(*val);
            }
            s.contains(&1024);
        })
    });
}

fn hashset_insertion_512m(c: &mut Criterion) {
    let s: &mut dyn Container<u32> = &mut HashSet::new();
    let data = gen_dataset_512();
    c.bench_function("hashset insertion 512MB", |b| {
        b.iter(|| {
            for val in data.iter() {
                s.insert(*val);
            }
            s.contains(&1024);
        })
    });
}

fn lazy_uniuqe_vec_insertion_1m(c: &mut Criterion) {
    let s: &mut dyn Container<u32> = &mut LazyUniqueVec::new();
    let data = gen_dataset_1();
    c.bench_function("lazy unique vector insertion 1MB", |b| {
        b.iter(|| {
            for val in data.iter() {
                s.insert(*val);
            }
            s.contains(&1024);
        })
    });
}

fn lazy_uniuqe_vec_insertion_128m(c: &mut Criterion) {
    let s: &mut dyn Container<u32> = &mut LazyUniqueVec::new();
    let data = gen_dataset_128();
    c.bench_function("lazy unique vector insertion 128MB", |b| {
        b.iter(|| {
            for val in data.iter() {
                s.insert(*val);
            }
            s.contains(&1024);
        })
    });
}

fn lazy_uniuqe_vec_insertion_256m(c: &mut Criterion) {
    let s: &mut dyn Container<u32> = &mut LazyUniqueVec::new();
    let data = gen_dataset_256();
    c.bench_function("lazy unique vector insertion 256MB", |b| {
        b.iter(|| {
            for val in data.iter() {
                s.insert(*val);
            }
            s.contains(&1024);
        })
    });
}

fn lazy_uniuqe_vec_insertion_512m(c: &mut Criterion) {
    let s: &mut dyn Container<u32> = &mut LazyUniqueVec::new();
    let data = gen_dataset_512();
    c.bench_function("lazy unique vector insertion 512MB", |b| {
        b.iter(|| {
            for val in data.iter() {
                s.insert(*val);
            }
            s.contains(&1024);
        })
    });
}

criterion_group! {
    name = insertion_1m;
    config = Criterion::default().sample_size(10);
    targets = btreeset_insertion_1m, hashset_insertion_1m, lazy_uniuqe_vec_insertion_1m
}

criterion_group! {
    name = insertion_128m;
    config = Criterion::default().sample_size(10);
    targets = btreeset_insertion_128m, hashset_insertion_128m, lazy_uniuqe_vec_insertion_128m
}

criterion_group! {
    name = insertion_256m;
    config = Criterion::default().sample_size(10);
    targets = btreeset_insertion_256m, hashset_insertion_256m, lazy_uniuqe_vec_insertion_256m
}

criterion_group! {
    name = insertion_512m;
    config = Criterion::default().sample_size(10);
    targets = btreeset_insertion_512m, hashset_insertion_512m, lazy_uniuqe_vec_insertion_512m
}

criterion_main!(insertion_1m, insertion_128m, insertion_256m, insertion_512m);