| 1 | #![feature (test)] |
| 2 | #![cfg (feature = "bilock" )] |
| 3 | |
| 4 | extern crate test; |
| 5 | |
| 6 | use futures::task::Poll; |
| 7 | use futures_test::task::noop_context; |
| 8 | use futures_util::lock::BiLock; |
| 9 | |
| 10 | use crate::test::Bencher; |
| 11 | |
| 12 | #[bench] |
| 13 | fn contended(b: &mut Bencher) { |
| 14 | let mut context = noop_context(); |
| 15 | |
| 16 | b.iter(|| { |
| 17 | let (x, y) = BiLock::new(1); |
| 18 | |
| 19 | for _ in 0..1000 { |
| 20 | let x_guard = match x.poll_lock(&mut context) { |
| 21 | Poll::Ready(guard) => guard, |
| 22 | _ => panic!(), |
| 23 | }; |
| 24 | |
| 25 | // Try poll second lock while first lock still holds the lock |
| 26 | match y.poll_lock(&mut context) { |
| 27 | Poll::Pending => (), |
| 28 | _ => panic!(), |
| 29 | }; |
| 30 | |
| 31 | drop(x_guard); |
| 32 | |
| 33 | let y_guard = match y.poll_lock(&mut context) { |
| 34 | Poll::Ready(guard) => guard, |
| 35 | _ => panic!(), |
| 36 | }; |
| 37 | |
| 38 | drop(y_guard); |
| 39 | } |
| 40 | (x, y) |
| 41 | }); |
| 42 | } |
| 43 | |
| 44 | #[bench] |
| 45 | fn lock_unlock(b: &mut Bencher) { |
| 46 | let mut context = noop_context(); |
| 47 | |
| 48 | b.iter(|| { |
| 49 | let (x, y) = BiLock::new(1); |
| 50 | |
| 51 | for _ in 0..1000 { |
| 52 | let x_guard = match x.poll_lock(&mut context) { |
| 53 | Poll::Ready(guard) => guard, |
| 54 | _ => panic!(), |
| 55 | }; |
| 56 | |
| 57 | drop(x_guard); |
| 58 | |
| 59 | let y_guard = match y.poll_lock(&mut context) { |
| 60 | Poll::Ready(guard) => guard, |
| 61 | _ => panic!(), |
| 62 | }; |
| 63 | |
| 64 | drop(y_guard); |
| 65 | } |
| 66 | (x, y) |
| 67 | }) |
| 68 | } |
| 69 | |