-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbench.rs
More file actions
334 lines (288 loc) · 10.3 KB
/
bench.rs
File metadata and controls
334 lines (288 loc) · 10.3 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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main};
use fq::FastQueue;
use std::hint::black_box;
use std::thread;
fn bench_single_threaded_push_pop(c: &mut Criterion) {
let mut group = c.benchmark_group("single_threaded_push_pop");
for size in [16, 64, 256, 1024, 4096].iter() {
group.throughput(Throughput::Elements(*size as u64));
group.bench_with_input(BenchmarkId::new("push_pop", size), size, |b, &size| {
b.iter(|| {
let (mut producer, mut consumer) = FastQueue::<u64>::new(size);
for i in 0..size {
black_box(producer.push(black_box(i as u64))).unwrap();
}
for _ in 0..size {
black_box(consumer.pop()).unwrap();
}
});
});
}
group.finish();
}
fn bench_push_only(c: &mut Criterion) {
let mut group = c.benchmark_group("push_only");
for capacity in [64, 256, 1024, 4096, 16384].iter() {
group.throughput(Throughput::Elements(*capacity as u64));
group.bench_with_input(
BenchmarkId::new("push", capacity),
capacity,
|b, &capacity| {
b.iter(|| {
let (mut producer, _consumer) = FastQueue::<u64>::new(capacity);
for i in 0..capacity {
black_box(producer.push(black_box(i as u64))).unwrap();
}
});
},
);
}
group.finish();
}
fn bench_pop_only(c: &mut Criterion) {
let mut group = c.benchmark_group("pop_only");
for capacity in [64, 256, 1024, 4096, 16384].iter() {
group.throughput(Throughput::Elements(*capacity as u64));
group.bench_with_input(
BenchmarkId::new("pop", capacity),
capacity,
|b, &capacity| {
b.iter_batched(
|| {
let (mut producer, consumer) = FastQueue::<u64>::new(capacity);
for i in 0..capacity {
producer.push(i as u64).unwrap();
}
consumer
},
|mut consumer| {
for _ in 0..capacity {
black_box(consumer.pop()).unwrap();
}
},
criterion::BatchSize::SmallInput,
);
},
);
}
group.finish();
}
fn bench_concurrent_spsc(c: &mut Criterion) {
let mut group = c.benchmark_group("concurrent_spsc");
for messages in [500, 5000, 50000].iter() {
group.throughput(Throughput::Elements(*messages as u64));
group.bench_with_input(
BenchmarkId::new("producer_consumer", messages),
messages,
|b, &messages| {
b.iter(|| {
let (mut producer, mut consumer) = FastQueue::<usize>::new(1024);
let producer_handle = thread::spawn(move || {
for i in 0..messages {
while black_box(producer.push(black_box(i as usize))).is_err() {
std::hint::spin_loop();
}
}
});
let consumer_handle = thread::spawn(move || {
let mut count = 0;
while count < messages {
if let Some(val) = black_box(consumer.pop()) {
black_box(val);
count += 1;
} else {
std::hint::spin_loop();
}
}
});
producer_handle.join().unwrap();
consumer_handle.join().unwrap();
});
},
);
}
group.finish();
}
fn bench_concurrent_spsc_large_messages(c: &mut Criterion) {
let mut group = c.benchmark_group("concurrent_spsc_large_messages");
struct LargeMessage {
_id: u128,
_id_str: String,
}
for messages in [500, 5000, 50000].iter() {
group.throughput(Throughput::Elements(*messages as u64));
group.bench_with_input(
BenchmarkId::new("producer_consumer", messages),
messages,
|b, &messages| {
b.iter(|| {
let (mut producer, mut consumer) = FastQueue::<LargeMessage>::new(1024);
let producer_handle = thread::spawn(move || {
for i in 0..messages {
while black_box(producer.push(black_box(LargeMessage {
_id: i as u128,
_id_str: format!("Message {i}"),
})))
.is_err()
{
std::hint::spin_loop();
}
}
});
let consumer_handle = thread::spawn(move || {
let mut count = 0;
while count < messages {
if let Some(val) = black_box(consumer.pop()) {
black_box(val);
count += 1;
} else {
std::hint::spin_loop();
}
}
});
producer_handle.join().unwrap();
consumer_handle.join().unwrap();
});
},
);
}
group.finish();
}
fn bench_burst_operations(c: &mut Criterion) {
let mut group = c.benchmark_group("burst_operations");
group.bench_function("burst_push_pop_16", |b| {
b.iter(|| {
let (mut producer, mut consumer) = FastQueue::<usize>::new(32);
// Burst push 16 items
for i in 0..16 {
black_box(producer.push(black_box(i))).unwrap();
}
// Burst pop 16 items
for _ in 0..16 {
black_box(consumer.pop()).unwrap();
}
});
});
group.bench_function("alternating_push_pop", |b| {
b.iter(|| {
let (mut producer, mut consumer) = FastQueue::<usize>::new(16);
for i in 0..1000 {
black_box(producer.push(black_box(i))).unwrap();
black_box(consumer.pop()).unwrap();
}
});
});
group.finish();
}
fn bench_wraparound(c: &mut Criterion) {
let mut group = c.benchmark_group("wraparound");
group.bench_function("wraparound_operations", |b| {
b.iter_batched(
|| {
let (mut producer, mut consumer) = FastQueue::<usize>::new(64);
// Fill queue to near capacity
for i in 0..60 {
producer.push(i).unwrap();
}
// Consume some to create space for wraparound
for _ in 0..30 {
consumer.pop().unwrap();
}
(producer, consumer)
},
|(mut producer, mut consumer)| {
// This will cause wraparound in the ring buffer
for i in 60..120 {
while black_box(producer.push(black_box(i))).is_err() {
black_box(consumer.pop());
}
}
// Consume remaining
while let Some(val) = black_box(consumer.pop()) {
black_box(val);
}
},
criterion::BatchSize::SmallInput,
);
});
group.finish();
}
fn bench_capacity_scaling(c: &mut Criterion) {
let mut group = c.benchmark_group("capacity_scaling");
for capacity in [16, 32, 64, 128, 256, 512, 1024, 2048, 4096].iter() {
group.bench_with_input(
BenchmarkId::new("creation_overhead", capacity),
capacity,
|b, &capacity| {
b.iter(|| {
let (_producer, _consumer) =
black_box(FastQueue::<usize>::new(black_box(capacity)));
});
},
);
}
group.finish();
}
fn bench_peek_operations(c: &mut Criterion) {
let mut group = c.benchmark_group("peek_operations");
group.bench_function("peek_vs_pop", |b| {
b.iter_batched(
|| {
let (mut producer, consumer) = FastQueue::<usize>::new(1024);
for i in 0..500 {
producer.push(i).unwrap();
}
consumer
},
|consumer| {
// Peek at all elements multiple times
for _ in 0..100 {
if let Some(val) = black_box(consumer.peek()) {
black_box(val);
}
}
},
criterion::BatchSize::SmallInput,
);
});
group.finish();
}
fn bench_len_operations(c: &mut Criterion) {
let mut group = c.benchmark_group("len_operations");
group.bench_function("len_check_overhead", |b| {
b.iter_batched(
|| {
let (mut producer, consumer) = FastQueue::<usize>::new(1024);
for i in 0..500 {
producer.push(i).unwrap();
}
(producer, consumer)
},
|(producer, consumer)| {
for _ in 0..1000 {
black_box(producer.len());
black_box(consumer.len());
black_box(producer.is_empty());
black_box(producer.is_full());
black_box(consumer.is_empty());
}
},
criterion::BatchSize::SmallInput,
);
});
group.finish();
}
criterion_group!(
benches,
bench_single_threaded_push_pop,
bench_push_only,
bench_pop_only,
bench_concurrent_spsc,
bench_concurrent_spsc_large_messages,
bench_burst_operations,
bench_wraparound,
bench_capacity_scaling,
bench_peek_operations,
bench_len_operations
);
criterion_main!(benches);