Skip to content

Commit 6ef1dd2

Browse files
committed
fmt: cargo fmt
1 parent e6c6e76 commit 6ef1dd2

File tree

2 files changed

+203
-175
lines changed

2 files changed

+203
-175
lines changed

benches/bench.rs

Lines changed: 126 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
use criterion::{black_box, criterion_group, criterion_main, Criterion, BenchmarkId, Throughput};
1+
use criterion::{BenchmarkId, Criterion, Throughput, black_box, criterion_group, criterion_main};
22
use fq::FastQueue;
33
use std::thread;
44

55
fn bench_single_threaded_push_pop(c: &mut Criterion) {
66
let mut group = c.benchmark_group("single_threaded_push_pop");
7-
7+
88
for size in [16, 64, 256, 1024, 4096].iter() {
99
group.throughput(Throughput::Elements(*size as u64));
10-
10+
1111
group.bench_with_input(BenchmarkId::new("push_pop", size), size, |b, &size| {
1212
b.iter(|| {
1313
let (mut producer, mut consumer) = FastQueue::<u64>::new(size);
14-
14+
1515
for i in 0..size {
1616
producer.push(black_box(i as u64)).unwrap();
1717
}
18-
18+
1919
for _ in 0..size {
2020
black_box(consumer.pop().unwrap());
2121
}
@@ -27,84 +27,96 @@ fn bench_single_threaded_push_pop(c: &mut Criterion) {
2727

2828
fn bench_push_only(c: &mut Criterion) {
2929
let mut group = c.benchmark_group("push_only");
30-
30+
3131
for capacity in [64, 256, 1024, 4096, 16384].iter() {
3232
group.throughput(Throughput::Elements(*capacity as u64));
33-
34-
group.bench_with_input(BenchmarkId::new("push", capacity), capacity, |b, &capacity| {
35-
b.iter(|| {
36-
let (mut producer, _consumer) = FastQueue::<u64>::new(capacity);
37-
38-
for i in 0..capacity {
39-
producer.push(black_box(i as u64)).unwrap();
40-
}
41-
});
42-
});
33+
34+
group.bench_with_input(
35+
BenchmarkId::new("push", capacity),
36+
capacity,
37+
|b, &capacity| {
38+
b.iter(|| {
39+
let (mut producer, _consumer) = FastQueue::<u64>::new(capacity);
40+
41+
for i in 0..capacity {
42+
producer.push(black_box(i as u64)).unwrap();
43+
}
44+
});
45+
},
46+
);
4347
}
4448
group.finish();
4549
}
4650

4751
fn bench_pop_only(c: &mut Criterion) {
4852
let mut group = c.benchmark_group("pop_only");
49-
53+
5054
for capacity in [64, 256, 1024, 4096, 16384].iter() {
5155
group.throughput(Throughput::Elements(*capacity as u64));
52-
53-
group.bench_with_input(BenchmarkId::new("pop", capacity), capacity, |b, &capacity| {
54-
b.iter_batched(
55-
|| {
56-
let (mut producer, consumer) = FastQueue::<u64>::new(capacity);
57-
for i in 0..capacity {
58-
producer.push(i as u64).unwrap();
59-
}
60-
consumer
61-
},
62-
|mut consumer| {
63-
for _ in 0..capacity {
64-
black_box(consumer.pop().unwrap());
65-
}
66-
},
67-
criterion::BatchSize::SmallInput,
68-
);
69-
});
56+
57+
group.bench_with_input(
58+
BenchmarkId::new("pop", capacity),
59+
capacity,
60+
|b, &capacity| {
61+
b.iter_batched(
62+
|| {
63+
let (mut producer, consumer) = FastQueue::<u64>::new(capacity);
64+
for i in 0..capacity {
65+
producer.push(i as u64).unwrap();
66+
}
67+
consumer
68+
},
69+
|mut consumer| {
70+
for _ in 0..capacity {
71+
black_box(consumer.pop().unwrap());
72+
}
73+
},
74+
criterion::BatchSize::SmallInput,
75+
);
76+
},
77+
);
7078
}
7179
group.finish();
7280
}
7381

7482
fn bench_concurrent_spsc(c: &mut Criterion) {
7583
let mut group = c.benchmark_group("concurrent_spsc");
76-
84+
7785
for messages in [1000, 10000, 100000].iter() {
7886
group.throughput(Throughput::Elements(*messages as u64));
79-
80-
group.bench_with_input(BenchmarkId::new("producer_consumer", messages), messages, |b, &messages| {
81-
b.iter(|| {
82-
let (mut producer, mut consumer) = FastQueue::<u64>::new(1024);
83-
84-
let producer_handle = thread::spawn(move || {
85-
for i in 0..messages {
86-
while producer.push(black_box(i as u64)).is_err() {
87-
std::hint::spin_loop();
87+
88+
group.bench_with_input(
89+
BenchmarkId::new("producer_consumer", messages),
90+
messages,
91+
|b, &messages| {
92+
b.iter(|| {
93+
let (mut producer, mut consumer) = FastQueue::<u64>::new(1024);
94+
95+
let producer_handle = thread::spawn(move || {
96+
for i in 0..messages {
97+
while producer.push(black_box(i as u64)).is_err() {
98+
std::hint::spin_loop();
99+
}
88100
}
89-
}
90-
});
91-
92-
let consumer_handle = thread::spawn(move || {
93-
let mut count = 0;
94-
while count < messages {
95-
if let Some(val) = consumer.pop() {
96-
black_box(val);
97-
count += 1;
98-
} else {
99-
std::hint::spin_loop();
101+
});
102+
103+
let consumer_handle = thread::spawn(move || {
104+
let mut count = 0;
105+
while count < messages {
106+
if let Some(val) = consumer.pop() {
107+
black_box(val);
108+
count += 1;
109+
} else {
110+
std::hint::spin_loop();
111+
}
100112
}
101-
}
113+
});
114+
115+
producer_handle.join().unwrap();
116+
consumer_handle.join().unwrap();
102117
});
103-
104-
producer_handle.join().unwrap();
105-
consumer_handle.join().unwrap();
106-
});
107-
});
118+
},
119+
);
108120
}
109121
group.finish();
110122
}
@@ -116,81 +128,88 @@ fn bench_concurrent_spsc_large_messages(c: &mut Criterion) {
116128
val1: u128,
117129
val2: String,
118130
}
119-
131+
120132
for messages in [1000, 10000, 100000].iter() {
121133
group.throughput(Throughput::Elements(*messages as u64));
122-
123-
group.bench_with_input(BenchmarkId::new("producer_consumer", messages), messages, |b, &messages| {
124-
b.iter(|| {
125-
let (mut producer, mut consumer) = FastQueue::<LargeMessage>::new(1024);
126-
127-
let producer_handle = thread::spawn(move || {
128-
for i in 0..messages {
129-
while producer.push(black_box(LargeMessage {
130-
val1: i as u128,
131-
val2: format!("Message {}", i),
132-
})).is_err() {
133-
std::hint::spin_loop();
134+
135+
group.bench_with_input(
136+
BenchmarkId::new("producer_consumer", messages),
137+
messages,
138+
|b, &messages| {
139+
b.iter(|| {
140+
let (mut producer, mut consumer) = FastQueue::<LargeMessage>::new(1024);
141+
142+
let producer_handle = thread::spawn(move || {
143+
for i in 0..messages {
144+
while producer
145+
.push(black_box(LargeMessage {
146+
val1: i as u128,
147+
val2: format!("Message {}", i),
148+
}))
149+
.is_err()
150+
{
151+
std::hint::spin_loop();
152+
}
134153
}
135-
}
136-
});
137-
138-
let consumer_handle = thread::spawn(move || {
139-
let mut count = 0;
140-
while count < messages {
141-
if let Some(val) = consumer.pop() {
142-
black_box(val);
143-
count += 1;
144-
} else {
145-
std::hint::spin_loop();
154+
});
155+
156+
let consumer_handle = thread::spawn(move || {
157+
let mut count = 0;
158+
while count < messages {
159+
if let Some(val) = consumer.pop() {
160+
black_box(val);
161+
count += 1;
162+
} else {
163+
std::hint::spin_loop();
164+
}
146165
}
147-
}
166+
});
167+
168+
producer_handle.join().unwrap();
169+
consumer_handle.join().unwrap();
148170
});
149-
150-
producer_handle.join().unwrap();
151-
consumer_handle.join().unwrap();
152-
});
153-
});
171+
},
172+
);
154173
}
155174
group.finish();
156175
}
157176

158177
fn bench_burst_operations(c: &mut Criterion) {
159178
let mut group = c.benchmark_group("burst_operations");
160-
179+
161180
group.bench_function("burst_push_pop_16", |b| {
162181
b.iter(|| {
163182
let (mut producer, mut consumer) = FastQueue::<u64>::new(32);
164-
183+
165184
// Burst push 16 items
166185
for i in 0..16 {
167186
producer.push(black_box(i)).unwrap();
168187
}
169-
188+
170189
// Burst pop 16 items
171190
for _ in 0..16 {
172191
black_box(consumer.pop().unwrap());
173192
}
174193
});
175194
});
176-
195+
177196
group.bench_function("alternating_push_pop", |b| {
178197
b.iter(|| {
179198
let (mut producer, mut consumer) = FastQueue::<u64>::new(16);
180-
199+
181200
for i in 0..1000 {
182201
producer.push(black_box(i)).unwrap();
183202
black_box(consumer.pop().unwrap());
184203
}
185204
});
186205
});
187-
206+
188207
group.finish();
189208
}
190209

191210
fn bench_wraparound(c: &mut Criterion) {
192211
let mut group = c.benchmark_group("wraparound");
193-
212+
194213
group.bench_function("wraparound_operations", |b| {
195214
b.iter_batched(
196215
|| {
@@ -212,7 +231,7 @@ fn bench_wraparound(c: &mut Criterion) {
212231
black_box(consumer.pop());
213232
}
214233
}
215-
234+
216235
// Consume remaining
217236
while let Some(val) = consumer.pop() {
218237
black_box(val);
@@ -221,13 +240,13 @@ fn bench_wraparound(c: &mut Criterion) {
221240
criterion::BatchSize::SmallInput,
222241
);
223242
});
224-
243+
225244
group.finish();
226245
}
227246

228247
fn bench_capacity_scaling(c: &mut Criterion) {
229248
let mut group = c.benchmark_group("capacity_scaling");
230-
249+
231250
for capacity in [16, 32, 64, 128, 256, 512, 1024, 2048, 4096].iter() {
232251
group.bench_with_input(
233252
BenchmarkId::new("creation_overhead", capacity),
@@ -239,13 +258,13 @@ fn bench_capacity_scaling(c: &mut Criterion) {
239258
},
240259
);
241260
}
242-
261+
243262
group.finish();
244263
}
245264

246265
fn bench_peek_operations(c: &mut Criterion) {
247266
let mut group = c.benchmark_group("peek_operations");
248-
267+
249268
group.bench_function("peek_vs_pop", |b| {
250269
b.iter_batched(
251270
|| {
@@ -266,13 +285,13 @@ fn bench_peek_operations(c: &mut Criterion) {
266285
criterion::BatchSize::SmallInput,
267286
);
268287
});
269-
288+
270289
group.finish();
271290
}
272291

273292
fn bench_len_operations(c: &mut Criterion) {
274293
let mut group = c.benchmark_group("len_operations");
275-
294+
276295
group.bench_function("len_check_overhead", |b| {
277296
b.iter_batched(
278297
|| {
@@ -294,7 +313,7 @@ fn bench_len_operations(c: &mut Criterion) {
294313
criterion::BatchSize::SmallInput,
295314
);
296315
});
297-
316+
298317
group.finish();
299318
}
300319

0 commit comments

Comments
 (0)