forked from jonhoo/evmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
244 lines (227 loc) · 7.76 KB
/
main.rs
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
236
237
238
239
240
241
242
243
244
use chashmap::CHashMap;
use clap::{crate_version, value_t};
use clap::{App, Arg};
use rand::prelude::*;
use std::collections::HashMap;
use std::sync;
use std::thread;
use std::time;
fn main() {
let matches = App::new("Concurrent HashMap Benchmarker")
.version(crate_version!())
.about(
"Benchmark multiple implementations of concurrent HashMaps with varying read/write load",
)
.arg(
Arg::with_name("readers")
.short("r")
.long("readers")
.help("Set the number of readers")
.required(true)
.takes_value(true),
)
.arg(
Arg::with_name("compare")
.short("c")
.help("Also benchmark Arc<RwLock<HashMap>> and CHashMap"),
)
.arg(
Arg::with_name("eventual")
.short("e")
.takes_value(true)
.default_value("1")
.value_name("N")
.help("Refresh evmap every N writes"),
)
.arg(
Arg::with_name("writers")
.short("w")
.long("writers")
.required(true)
.help("Set the number of writers")
.takes_value(true),
)
.arg(
Arg::with_name("distribution")
.short("d")
.long("dist")
.possible_values(&["uniform", "skewed"])
.default_value("uniform")
.help("Set the distribution for reads and writes")
.takes_value(true),
)
.get_matches();
let refresh = value_t!(matches, "eventual", usize).unwrap_or_else(|e| e.exit());
let readers = value_t!(matches, "readers", usize).unwrap_or_else(|e| e.exit());
let writers = value_t!(matches, "writers", usize).unwrap_or_else(|e| e.exit());
let dist = matches.value_of("distribution").unwrap_or("uniform");
let dur = time::Duration::from_secs(5);
let dur_in_ns = dur.as_secs() * 1_000_000_000_u64 + dur.subsec_nanos() as u64;
let dur_in_s = dur_in_ns as f64 / 1_000_000_000_f64;
let span = 10000;
let stat = |var: &str, op, results: Vec<(_, usize)>| {
for (i, res) in results.into_iter().enumerate() {
println!(
"{:2} {:2} {:10} {:10} {:8.0} ops/s {} {}",
readers,
writers,
dist,
var,
res.1 as f64 / dur_in_s as f64,
op,
i
)
}
};
let mut join = Vec::with_capacity(readers + writers);
// first, benchmark Arc<RwLock<HashMap>>
if matches.is_present("compare") {
let map: HashMap<usize, usize> = HashMap::with_capacity(5_000_000);
let map = sync::Arc::new(parking_lot::RwLock::new(map));
let start = time::Instant::now();
let end = start + dur;
join.extend((0..readers).into_iter().map(|_| {
let map = map.clone();
let dist = dist.to_owned();
thread::spawn(move || drive(map, end, dist, false, span))
}));
join.extend((0..writers).into_iter().map(|_| {
let map = map.clone();
let dist = dist.to_owned();
thread::spawn(move || drive(map, end, dist, true, span))
}));
let (wres, rres): (Vec<_>, _) = join
.drain(..)
.map(|jh| jh.join().unwrap())
.partition(|&(write, _)| write);
stat("std", "write", wres);
stat("std", "read", rres);
}
// then, benchmark Arc<CHashMap>
if matches.is_present("compare") {
let map: CHashMap<usize, usize> = CHashMap::with_capacity(5_000_000);
let map = sync::Arc::new(map);
let start = time::Instant::now();
let end = start + dur;
join.extend((0..readers).into_iter().map(|_| {
let map = map.clone();
let dist = dist.to_owned();
thread::spawn(move || drive(map, end, dist, false, span))
}));
join.extend((0..writers).into_iter().map(|_| {
let map = map.clone();
let dist = dist.to_owned();
thread::spawn(move || drive(map, end, dist, true, span))
}));
let (wres, rres): (Vec<_>, _) = join
.drain(..)
.map(|jh| jh.join().unwrap())
.partition(|&(write, _)| write);
stat("chashmap", "write", wres);
stat("chashmap", "read", rres);
}
// finally, benchmark evmap
{
let (w, r) = evmap::Options::default()
.with_capacity(5_000_000)
.construct();
let w = sync::Arc::new(parking_lot::Mutex::new((w, 0, refresh)));
let start = time::Instant::now();
let end = start + dur;
join.extend((0..readers).into_iter().map(|_| {
let map = EvHandle::Read(r.clone());
let dist = dist.to_owned();
thread::spawn(move || drive(map, end, dist, false, span))
}));
join.extend((0..writers).into_iter().map(|_| {
let map = EvHandle::Write(w.clone());
let dist = dist.to_owned();
thread::spawn(move || drive(map, end, dist, true, span))
}));
let (wres, rres): (Vec<_>, _) = join
.drain(..)
.map(|jh| jh.join().unwrap())
.partition(|&(write, _)| write);
let n = if refresh == 1 {
"evmap".to_owned()
} else {
format!("evmap-refresh{}", refresh)
};
stat(&n, "write", wres);
stat(&n, "read", rres);
}
}
trait Backend {
fn b_get(&self, key: usize) -> usize;
fn b_put(&mut self, key: usize, value: usize);
}
fn drive<B: Backend>(
mut backend: B,
end: time::Instant,
dist: String,
write: bool,
span: usize,
) -> (bool, usize) {
let mut ops = 0;
let skewed = dist == "skewed";
let mut t_rng = rand::thread_rng();
let zipf = zipf::ZipfDistribution::new(span, 1.03).unwrap();
while time::Instant::now() < end {
// generate both so that overhead is always the same
let id_uniform: usize = t_rng.gen_range(0, span as usize);
let id_skewed: usize = zipf.sample(&mut t_rng);
let id = if skewed { id_skewed } else { id_uniform };
if write {
backend.b_put(id, t_rng.gen());
} else {
backend.b_get(id);
}
ops += 1;
}
(write, ops)
}
impl Backend for sync::Arc<CHashMap<usize, usize>> {
fn b_get(&self, key: usize) -> usize {
self.get(&key).map(|v| *v).unwrap_or(0)
}
fn b_put(&mut self, key: usize, value: usize) {
self.insert(key, value);
}
}
impl Backend for sync::Arc<parking_lot::RwLock<HashMap<usize, usize>>> {
fn b_get(&self, key: usize) -> usize {
self.read().get(&key).map(|&v| v).unwrap_or(0)
}
fn b_put(&mut self, key: usize, value: usize) {
self.write().insert(key, value);
}
}
enum EvHandle {
Read(evmap::handles::ReadHandle<usize, usize>),
Write(sync::Arc<parking_lot::Mutex<(evmap::handles::WriteHandle<usize, usize>, usize, usize)>>),
}
impl Backend for EvHandle {
fn b_get(&self, key: usize) -> usize {
if let EvHandle::Read(ref r) = *self {
r.get(&key)
.map(|v| v.iter().next().cloned().unwrap())
.unwrap_or(0)
} else {
unreachable!();
}
}
fn b_put(&mut self, key: usize, value: usize) {
if let EvHandle::Write(ref w) = *self {
let mut w = w.lock();
w.0.update(key, value);
w.1 += 1;
if w.1 == w.2 {
w.1 = 0;
w.0.publish();
}
} else {
unreachable!();
}
}
}