Skip to content

Commit 668321d

Browse files
committed
[eclipse-iceoryx#139] Add bitset benchmark
1 parent 66d1d78 commit 668321d

File tree

3 files changed

+97
-1
lines changed

3 files changed

+97
-1
lines changed

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ members = [
2020

2121
"examples",
2222

23-
"benchmarks/publish-subscribe"
23+
"benchmarks/publish-subscribe",
24+
"benchmarks/mpmc-bitset"
2425
]
2526

2627
[workspace.package]

benchmarks/mpmc-bitset/Cargo.toml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "mpmc-bitset-benchmark"
3+
description = "iceoryx2: [internal] benchmark for the mpmc::bitset"
4+
categories = { workspace = true }
5+
edition = { workspace = true }
6+
homepage = { workspace = true }
7+
keywords = { workspace = true }
8+
license = { workspace = true }
9+
repository = { workspace = true }
10+
rust-version = { workspace = true }
11+
version = { workspace = true }
12+
13+
[dependencies]
14+
iceoryx2-bb-lock-free = { workspace = true }

benchmarks/mpmc-bitset/src/main.rs

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright (c) 2024 Contributors to the Eclipse Foundation
2+
//
3+
// See the NOTICE file(s) distributed with this work for additional
4+
// information regarding copyright ownership.
5+
//
6+
// This program and the accompanying materials are made available under the
7+
// terms of the Apache Software License 2.0 which is available at
8+
// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
9+
// which is available at https://opensource.org/licenses/MIT.
10+
//
11+
// SPDX-License-Identifier: Apache-2.0 OR MIT
12+
13+
use std::{sync::Barrier, time::Instant};
14+
15+
use iceoryx2_bb_lock_free::mpmc::bit_set::BitSet;
16+
17+
struct Config {
18+
capacity: usize,
19+
iterations: usize,
20+
}
21+
22+
fn perform_benchmark(config: Config) {
23+
let sut_a_b = BitSet::new(config.capacity);
24+
let sut_b_a = BitSet::new(config.capacity);
25+
// worst case bit - iterate over all bitset to see if its set
26+
let bit = config.capacity - 1;
27+
let barrier = Barrier::new(3);
28+
29+
std::thread::scope(|s| {
30+
let t1 = s.spawn(|| {
31+
barrier.wait();
32+
sut_a_b.set(bit);
33+
34+
for _ in 0..config.iterations {
35+
let mut has_update = false;
36+
while !has_update {
37+
sut_b_a.reset_all(|_| {
38+
has_update = true;
39+
});
40+
}
41+
sut_a_b.set(bit);
42+
}
43+
});
44+
45+
let t2 = s.spawn(|| {
46+
barrier.wait();
47+
for _ in 0..config.iterations {
48+
let mut has_update = false;
49+
while !has_update {
50+
sut_a_b.reset_all(|_| {
51+
has_update = true;
52+
});
53+
}
54+
sut_b_a.set(bit);
55+
}
56+
});
57+
58+
std::thread::sleep(std::time::Duration::from_millis(100));
59+
let start = Instant::now();
60+
barrier.wait();
61+
62+
t1.join().expect("thread failure");
63+
t2.join().expect("thread failure");
64+
65+
let stop = start.elapsed();
66+
println!(
67+
"Capacity: {}, Iterations: {}, Time: {}, Latency: {} ns",
68+
config.capacity,
69+
config.iterations,
70+
stop.as_secs_f64(),
71+
stop.as_nanos() / (config.iterations as u128 * 2)
72+
);
73+
});
74+
}
75+
76+
fn main() {
77+
perform_benchmark(Config {
78+
capacity: 256,
79+
iterations: 1000000,
80+
})
81+
}

0 commit comments

Comments
 (0)