Debian Codegraph @

benches/benches.rs

raw symbols SCIP
1 use criterion::{black_box, criterion_group, criterion_main, Criterion, Throughput};
2 use proptest::{prelude::*, strategy::ValueTree, test_runner::TestRunner};
3 use rangemap::*;
4 use std::{any::type_name, fmt::Debug, ops::*};
5 use test_strategy::Arbitrary;
6
7 type Key = i64;
8 type Value = i64;
9
10 const COUNT: usize = 100000;
11 const OPERATIONS: usize = 100000;
12 const LOOKUPS: usize = 1000000;
13
14 #[derive(Debug, Clone, Arbitrary)]
15 enum Operation<K, V> {
16 Insert(K, V),
17 Delete(K),
18 }
19
20 fn range_inclusive_map<K: Ord + StepLite + Debug + Clone, V: Eq + Clone + Debug>(
21 values: impl Strategy<Value = (RangeInclusive<K>, V)>,
22 size: usize,
23 ) -> impl Strategy<Value = RangeInclusiveMap<K, V>> {
24 prop::collection::vec(values, size)
25 .prop_map(|ranges| ranges.into_iter().collect::<RangeInclusiveMap<K, V>>())
26 }
27
28 fn range_map<K: Ord + StepLite + Debug + Clone, V: Eq + Clone + Debug>(
29 values: impl Strategy<Value = (Range<K>, V)>,
30 size: usize,
31 ) -> impl Strategy<Value = RangeMap<K, V>> {
32 prop::collection::vec(values, size)
33 .prop_map(|ranges| ranges.into_iter().collect::<RangeMap<K, V>>())
34 }
35
36 fn criterion_benchmark(c: &mut Criterion) {
37 let mut runner = TestRunner::deterministic();
38
39 let mut group = c.benchmark_group(format!(
40 "RangeMap<{}, {}>",
41 type_name::<Key>(),
42 type_name::<Value>()
43 ));
44
45 group.throughput(Throughput::Elements(COUNT as u64));
46 group.bench_function("insert", |b| {
47 let entries = prop::collection::vec(any::<(Range<Key>, Value)>(), COUNT)
48 .new_tree(&mut runner)
49 .unwrap()
50 .current();
51 b.iter_with_large_drop(|| {
52 let mut map = RangeMap::new();
53 for (range, value) in entries.clone().into_iter() {
54 map.insert(range, value);
55 }
56 map
57 })
58 });
59
60 group.throughput(Throughput::Elements(OPERATIONS as u64));
61 group.bench_function("operations", |b| {
62 let map = range_map(any::<(Range<Key>, Value)>(), COUNT)
63 .new_tree(&mut runner)
64 .unwrap()
65 .current();
66 let operations = prop::collection::vec(any::<Operation<Range<Key>, Value>>(), OPERATIONS)
67 .new_tree(&mut runner)
68 .unwrap()
69 .current();
70 b.iter_with_large_drop(|| {
71 let mut map = map.clone();
72 for operation in operations.clone().into_iter() {
73 match operation {
74 Operation::Insert(key, value) => map.insert(key, value),
75 Operation::Delete(key) => map.remove(key),
76 }
77 }
78 map
79 })
80 });
81
82 group.throughput(Throughput::Elements(LOOKUPS as u64));
83 group.bench_function("lookups", |b| {
84 let map = range_map(any::<(Range<Key>, Value)>(), COUNT)
85 .new_tree(&mut runner)
86 .unwrap()
87 .current();
88 let lookups = prop::collection::vec(any::<Key>(), LOOKUPS)
89 .new_tree(&mut runner)
90 .unwrap()
91 .current();
92 b.iter(|| {
93 for lookup in lookups.iter() {
94 black_box(map.get(lookup));
95 }
96 })
97 });
98
99 group.finish();
100
101 let mut group = c.benchmark_group(format!(
102 "RangeInclusiveMap<{}, {}>",
103 type_name::<Key>(),
104 type_name::<Value>()
105 ));
106
107 group.throughput(Throughput::Elements(COUNT as u64));
108 group.bench_function("insert", |b| {
109 let entries = prop::collection::vec(any::<(RangeInclusive<Key>, Value)>(), COUNT)
110 .new_tree(&mut runner)
111 .unwrap()
112 .current();
113 b.iter_with_large_drop(|| {
114 let mut map = RangeInclusiveMap::new();
115 for (range, value) in entries.clone().into_iter() {
116 map.insert(range, value);
117 }
118 map
119 })
120 });
121
122 group.throughput(Throughput::Elements(OPERATIONS as u64));
123 group.bench_function("operations", |b| {
124 let map = range_inclusive_map(any::<(RangeInclusive<Key>, Value)>(), COUNT)
125 .new_tree(&mut runner)
126 .unwrap()
127 .current();
128 let operations =
129 prop::collection::vec(any::<Operation<RangeInclusive<Key>, Value>>(), OPERATIONS)
130 .new_tree(&mut runner)
131 .unwrap()
132 .current();
133 b.iter_with_large_drop(|| {
134 let mut map = map.clone();
135 for operation in operations.clone().into_iter() {
136 match operation {
137 Operation::Insert(key, value) => map.insert(key, value),
138 Operation::Delete(key) => map.remove(key),
139 }
140 }
141 map
142 })
143 });
144
145 group.throughput(Throughput::Elements(LOOKUPS as u64));
146 group.bench_function("lookups", |b| {
147 let map = range_inclusive_map(any::<(RangeInclusive<Key>, Value)>(), COUNT)
148 .new_tree(&mut runner)
149 .unwrap()
150 .current();
151 let lookups = prop::collection::vec(any::<Key>(), LOOKUPS)
152 .new_tree(&mut runner)
153 .unwrap()
154 .current();
155 b.iter(|| {
156 for lookup in lookups.iter() {
157 black_box(map.get(lookup));
158 }
159 })
160 });
161
162 group.finish();
163 }
164
165 criterion_group!(benches, criterion_benchmark);
166 criterion_main!(benches);