| 1 |
#[macro_use] |
| 2 |
extern crate criterion; |
| 3 |
|
| 4 |
use criterion::{black_box, Bencher, Criterion}; |
| 5 |
use std::{ops::DerefMut, sync::Arc}; |
| 6 |
|
| 7 |
trait Mutex<T>: Send + Sync + 'static { |
| 8 |
type Guard<'a>: DerefMut<Target = T> |
| 9 |
where |
| 10 |
Self: 'a; |
| 11 |
fn new(x: T) -> Self; |
| 12 |
fn lock(&self) -> Self::Guard<'_>; |
| 13 |
} |
| 14 |
|
| 15 |
impl<T: Send + 'static> Mutex<T> for spin::mutex::SpinMutex<T> { |
| 16 |
type Guard<'a> = spin::mutex::SpinMutexGuard<'a, T> where Self: 'a; |
| 17 |
fn new(x: T) -> Self { |
| 18 |
spin::mutex::SpinMutex::new(x) |
| 19 |
} |
| 20 |
fn lock(&self) -> Self::Guard<'_> { |
| 21 |
self.lock() |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
impl<T: Send + 'static> Mutex<T> for spin::mutex::TicketMutex<T> { |
| 26 |
type Guard<'a> = spin::mutex::TicketMutexGuard<'a, T> where Self: 'a; |
| 27 |
fn new(x: T) -> Self { |
| 28 |
spin::mutex::TicketMutex::new(x) |
| 29 |
} |
| 30 |
fn lock(&self) -> Self::Guard<'_> { |
| 31 |
self.lock() |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
impl<T: Send + 'static> Mutex<T> for std::sync::Mutex<T> { |
| 36 |
type Guard<'a> = std::sync::MutexGuard<'a, T> where Self: 'a; |
| 37 |
fn new(x: T) -> Self { |
| 38 |
std::sync::Mutex::new(x) |
| 39 |
} |
| 40 |
fn lock(&self) -> Self::Guard<'_> { |
| 41 |
self.lock().unwrap() |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
fn gen_create<M: Mutex<u32>>(b: &mut Bencher) { |
| 46 |
b.iter(|| { |
| 47 |
let n = black_box(42); |
| 48 |
M::new(n) |
| 49 |
}); |
| 50 |
} |
| 51 |
|
| 52 |
fn gen_lock_unlock<M: Mutex<u32>>(b: &mut Bencher) { |
| 53 |
let m = M::new(0); |
| 54 |
b.iter(|| { |
| 55 |
let mut m = m.lock(); |
| 56 |
*m = m.wrapping_add(1); |
| 57 |
drop(m); |
| 58 |
}); |
| 59 |
} |
| 60 |
|
| 61 |
fn gen_lock_unlock_read_contention<M: Mutex<u32>>(b: &mut Bencher) { |
| 62 |
let m = Arc::new(M::new(0)); |
| 63 |
let thread = std::thread::spawn({ |
| 64 |
let m = m.clone(); |
| 65 |
move || { |
| 66 |
while Arc::strong_count(&m) > 1 { |
| 67 |
for _ in 0..1000 { |
| 68 |
black_box(*m.lock()); |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
}); |
| 73 |
b.iter(|| { |
| 74 |
let mut m = m.lock(); |
| 75 |
*m = m.wrapping_add(1); |
| 76 |
drop(m); |
| 77 |
}); |
| 78 |
drop(m); |
| 79 |
thread.join().unwrap(); |
| 80 |
} |
| 81 |
|
| 82 |
fn gen_lock_unlock_write_contention<M: Mutex<u32>>(b: &mut Bencher) { |
| 83 |
let m = Arc::new(M::new(0)); |
| 84 |
let thread = std::thread::spawn({ |
| 85 |
let m = m.clone(); |
| 86 |
move || { |
| 87 |
while Arc::strong_count(&m) > 1 { |
| 88 |
for _ in 0..1000 { |
| 89 |
let mut m = m.lock(); |
| 90 |
*m = m.wrapping_add(1); |
| 91 |
drop(m); |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
}); |
| 96 |
b.iter(|| { |
| 97 |
let mut m = m.lock(); |
| 98 |
*m = m.wrapping_add(1); |
| 99 |
drop(m); |
| 100 |
}); |
| 101 |
drop(m); |
| 102 |
thread.join().unwrap(); |
| 103 |
} |
| 104 |
|
| 105 |
fn create(b: &mut Criterion) { |
| 106 |
b.bench_function("create-spin-spinmutex", |b| { |
| 107 |
gen_create::<spin::mutex::SpinMutex<u32>>(b) |
| 108 |
}); |
| 109 |
b.bench_function("create-spin-ticketmutex", |b| { |
| 110 |
gen_create::<spin::mutex::TicketMutex<u32>>(b) |
| 111 |
}); |
| 112 |
b.bench_function("create-std", |b| gen_create::<std::sync::Mutex<u32>>(b)); |
| 113 |
} |
| 114 |
|
| 115 |
fn lock_unlock(b: &mut Criterion) { |
| 116 |
b.bench_function("lock_unlock-spin-spinmutex", |b| { |
| 117 |
gen_lock_unlock::<spin::mutex::SpinMutex<u32>>(b) |
| 118 |
}); |
| 119 |
b.bench_function("lock_unlock-spin-ticketmutex", |b| { |
| 120 |
gen_lock_unlock::<spin::mutex::TicketMutex<u32>>(b) |
| 121 |
}); |
| 122 |
b.bench_function("lock_unlock-std", |b| { |
| 123 |
gen_lock_unlock::<std::sync::Mutex<u32>>(b) |
| 124 |
}); |
| 125 |
} |
| 126 |
|
| 127 |
fn lock_unlock_read_contention(b: &mut Criterion) { |
| 128 |
b.bench_function("lock_unlock_read_contention-spin-spinmutex", |b| { |
| 129 |
gen_lock_unlock_read_contention::<spin::mutex::SpinMutex<u32>>(b) |
| 130 |
}); |
| 131 |
b.bench_function("lock_unlock_read_contention-spin-ticketmutex", |b| { |
| 132 |
gen_lock_unlock_read_contention::<spin::mutex::TicketMutex<u32>>(b) |
| 133 |
}); |
| 134 |
b.bench_function("lock_unlock_read_contention-std", |b| { |
| 135 |
gen_lock_unlock_read_contention::<std::sync::Mutex<u32>>(b) |
| 136 |
}); |
| 137 |
} |
| 138 |
|
| 139 |
fn lock_unlock_write_contention(b: &mut Criterion) { |
| 140 |
b.bench_function("lock_unlock_write_contention-spin-spinmutex", |b| { |
| 141 |
gen_lock_unlock_write_contention::<spin::mutex::SpinMutex<u32>>(b) |
| 142 |
}); |
| 143 |
b.bench_function("lock_unlock_write_contention-spin-ticketmutex", |b| { |
| 144 |
gen_lock_unlock_write_contention::<spin::mutex::TicketMutex<u32>>(b) |
| 145 |
}); |
| 146 |
b.bench_function("lock_unlock_write_contention-std", |b| { |
| 147 |
gen_lock_unlock_write_contention::<std::sync::Mutex<u32>>(b) |
| 148 |
}); |
| 149 |
} |
| 150 |
|
| 151 |
criterion_group!( |
| 152 |
mutex, |
| 153 |
create, |
| 154 |
lock_unlock, |
| 155 |
lock_unlock_read_contention, |
| 156 |
lock_unlock_write_contention, |
| 157 |
); |
| 158 |
|
| 159 |
criterion_main!(mutex); |