-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathconcurrent.rs
More file actions
113 lines (102 loc) · 3.21 KB
/
concurrent.rs
File metadata and controls
113 lines (102 loc) · 3.21 KB
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
#![feature(test)]
extern crate test;
use atomic_cuckoo_filter::CuckooFilter;
use std::sync::atomic::AtomicBool;
use std::sync::{Arc, atomic::Ordering};
use std::thread;
use test::Bencher;
/// Benchmarks concurrent read performance (contains() calls) while multiple background
/// threads are also performing reads. This tests the filter's ability to handle
/// high-concurrency read workloads without contention.
///
/// Setup: 131k capacity filter with 100k pre-inserted items
/// Scenario: 10 background threads continuously calling contains() while main thread benchmarks contains()
#[bench]
fn concurrent_contains(b: &mut Bencher) {
let filter = Arc::new(
CuckooFilter::builder()
.capacity(131072)
.fingerprint_size(8)
.build()
.unwrap(),
);
let stop_flag = Arc::new(AtomicBool::new(false));
let mut handles = vec![];
// Pre-populate with 100k items (even numbers)
for i in 0..100000 {
filter.insert(&(i * 2)).unwrap();
}
// Start 10 background threads doing continuous contains() calls
for _ in 0..10 {
let f = filter.clone();
let stop = stop_flag.clone();
handles.push(thread::spawn(move || {
let mut i = 0;
while !stop.load(Ordering::Relaxed) {
if i == 200000 {
i = 0;
} else {
i += 1;
}
f.contains(&i);
}
}))
}
// Benchmark contains() calls in main thread
let mut i = 0;
b.iter(|| {
if i == 200000 {
i = 0;
} else {
i += 1;
}
filter.contains(&i);
});
// Clean up background threads
stop_flag.store(true, Ordering::Relaxed);
for h in handles {
h.join().unwrap();
}
}
/// Benchmarks contains() performance while background threads are actively
/// inserting and removing items. This tests read performance under write contention.
///
/// Setup: 131k capacity filter, initially empty
/// Scenario: 10 background threads inserting new items and removing old ones,
/// while main thread benchmarks contains() performance
#[bench]
fn concurrent_contains_under_write_contention(b: &mut Bencher) {
let filter = Arc::new(
CuckooFilter::builder()
.capacity(131072)
.fingerprint_size(8)
.build()
.unwrap(),
);
let stop_flag = Arc::new(AtomicBool::new(false));
let mut handles = vec![];
// Start 10 background threads doing insert/remove operations
for c in 0..10 {
let f = filter.clone();
let stop = stop_flag.clone();
handles.push(thread::spawn(move || {
let mut i: u16 = c;
while !stop.load(Ordering::Relaxed) {
i += 10;
let _ = f.insert(&i);
f.remove(&(i - 10000));
}
}))
}
// Benchmark contains() calls while background threads are modifying the filter
let mut i: u16 = 0;
b.iter(|| {
i += 1;
filter.contains(&i);
});
// Clean up background threads
stop_flag.store(true, Ordering::Relaxed);
for h in handles {
h.join().unwrap();
}
}