|
| 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