-
-
Save fantix/647ca9b9e002cbd89c1af4525d8923ee to your computer and use it in GitHub Desktop.
Test Rust shared resource guards
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[macro_use] | |
extern crate lazy_static; | |
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; | |
use std::thread::sleep; | |
use std::time::Duration; | |
static READERS: u64 = 100; | |
static WRITERS: u64 = 2; | |
static RUNNING: AtomicBool = AtomicBool::new(true); | |
static READ_COUNT: AtomicU64 = AtomicU64::new(0); | |
const PROGRESS_EVERY: u64 = 50000; | |
static SLEEP: Duration = Duration::from_secs(16); | |
lazy_static! { | |
static ref S_MUTEX: std::sync::Mutex<u64> = std::sync::Mutex::new(0); | |
static ref S_RWLOCK: std::sync::RwLock<u64> = std::sync::RwLock::new(0); | |
static ref P_MUTEX: parking_lot::Mutex<u64> = parking_lot::Mutex::new(0); | |
static ref P_RWLOCK: parking_lot::RwLock<u64> = parking_lot::RwLock::new(0); | |
} | |
macro_rules! print_stat { | |
($title: expr, $read_count: expr, $write_count: expr) => { | |
println!( | |
"{} spent: {} reads, {} writes", | |
$title, $read_count, $write_count, | |
); | |
}; | |
} | |
fn bench_parking_lot_rwlock() -> (u64, u64) { | |
let writers = (0..WRITERS) | |
.map(|i| { | |
std::thread::spawn(move || { | |
while RUNNING.load(Ordering::Relaxed) { | |
let mut a = P_RWLOCK.write(); | |
*a += 1; | |
*a += 2; | |
*a += 3; | |
*a += 4; | |
for _ in 0..100 { | |
let _a = 123; | |
} | |
} | |
println!("write {} done", i); | |
}) | |
}) | |
.collect::<Vec<_>>(); | |
let readers = (0..READERS) | |
.map(|n| { | |
std::thread::spawn(move || { | |
while RUNNING.load(Ordering::Relaxed) { | |
let a = *P_RWLOCK.read(); | |
let i = READ_COUNT.fetch_add(1, Ordering::Relaxed); | |
if i % PROGRESS_EVERY == 0 { | |
println!("read {} @ {} = {}", n, i, a); | |
} | |
} | |
}) | |
}) | |
.collect::<Vec<_>>(); | |
sleep(SLEEP); | |
RUNNING.store(false, Ordering::SeqCst); | |
let rv = (READ_COUNT.swap(0, Ordering::SeqCst), *P_RWLOCK.read()); | |
readers | |
.into_iter() | |
.chain(writers.into_iter()) | |
.map(|h| h.join().unwrap()) | |
.for_each(drop); | |
rv | |
} | |
fn bench_parking_lot_mutex() -> (u64, u64) { | |
let writers = (0..WRITERS) | |
.map(|i| { | |
std::thread::spawn(move || { | |
while RUNNING.load(Ordering::Relaxed) { | |
let mut a = P_MUTEX.lock(); | |
*a += 1; | |
*a += 2; | |
*a += 3; | |
*a += 4; | |
for _ in 0..100 { | |
let _a = 123; | |
} | |
} | |
println!("write {} done", i); | |
}) | |
}) | |
.collect::<Vec<_>>(); | |
let readers = (0..READERS) | |
.map(|n| { | |
std::thread::spawn(move || { | |
while RUNNING.load(Ordering::Relaxed) { | |
let a = *P_MUTEX.lock(); | |
let i = READ_COUNT.fetch_add(1, Ordering::Relaxed); | |
if i % PROGRESS_EVERY == 0 { | |
println!("read {} @ {} = {}", n, i, a); | |
} | |
} | |
}) | |
}) | |
.collect::<Vec<_>>(); | |
sleep(SLEEP); | |
RUNNING.store(false, Ordering::SeqCst); | |
let rv = (READ_COUNT.swap(0, Ordering::SeqCst), *P_MUTEX.lock()); | |
readers | |
.into_iter() | |
.chain(writers.into_iter()) | |
.map(|h| h.join().unwrap()) | |
.for_each(drop); | |
rv | |
} | |
fn bench_sync_rwlock() -> (u64, u64) { | |
let writers = (0..WRITERS) | |
.map(|i| { | |
std::thread::spawn(move || { | |
while RUNNING.load(Ordering::Relaxed) { | |
let mut a = S_RWLOCK.write().unwrap(); | |
*a += 1; | |
*a += 2; | |
*a += 3; | |
*a += 4; | |
for _ in 0..100 { | |
let _a = 123; | |
} | |
} | |
println!("write {} done", i); | |
}) | |
}) | |
.collect::<Vec<_>>(); | |
let readers = (0..READERS) | |
.map(|n| { | |
std::thread::spawn(move || { | |
while RUNNING.load(Ordering::Relaxed) { | |
let a = *S_RWLOCK.read().unwrap(); | |
let i = READ_COUNT.fetch_add(1, Ordering::Relaxed); | |
if i % PROGRESS_EVERY == 0 { | |
println!("read {} @ {} = {}", n, i, a); | |
} | |
} | |
}) | |
}) | |
.collect::<Vec<_>>(); | |
sleep(SLEEP); | |
RUNNING.store(false, Ordering::SeqCst); | |
let rv = ( | |
READ_COUNT.swap(0, Ordering::SeqCst), | |
*S_RWLOCK.read().unwrap(), | |
); | |
readers | |
.into_iter() | |
.chain(writers.into_iter()) | |
.map(|h| h.join().unwrap()) | |
.for_each(drop); | |
rv | |
} | |
fn bench_sync_mutex() -> (u64, u64) { | |
let writers = (0..WRITERS) | |
.map(|i| { | |
std::thread::spawn(move || { | |
while RUNNING.load(Ordering::Relaxed) { | |
let mut a = S_MUTEX.lock().unwrap(); | |
*a += 1; | |
*a += 2; | |
*a += 3; | |
*a += 4; | |
for _ in 0..100 { | |
let _a = 123; | |
} | |
} | |
println!("write {} done", i); | |
}) | |
}) | |
.collect::<Vec<_>>(); | |
let readers = (0..READERS) | |
.map(|n| { | |
std::thread::spawn(move || { | |
while RUNNING.load(Ordering::Relaxed) { | |
let a = *S_MUTEX.lock().unwrap(); | |
let i = READ_COUNT.fetch_add(1, Ordering::Relaxed); | |
if i % PROGRESS_EVERY == 0 { | |
println!("read {} @ {} = {}", n, i, a); | |
} | |
} | |
}) | |
}) | |
.collect::<Vec<_>>(); | |
sleep(SLEEP); | |
RUNNING.store(false, Ordering::SeqCst); | |
let rv = ( | |
READ_COUNT.swap(0, Ordering::SeqCst), | |
*S_MUTEX.lock().unwrap(), | |
); | |
readers | |
.into_iter() | |
.chain(writers.into_iter()) | |
.map(|h| h.join().unwrap()) | |
.for_each(drop); | |
rv | |
} | |
fn main() -> Result<(), Box<dyn std::error::Error>> { | |
println!("PARKING_LOT RWLOCK ==============================="); | |
let (p_rwlock_r, p_rwlock_w) = bench_parking_lot_rwlock(); | |
RUNNING.store(true, Ordering::SeqCst); | |
println!("PARKING_LOT MUTEX ================================"); | |
let (p_mutex_r, p_mutex_w) = bench_parking_lot_mutex(); | |
RUNNING.store(true, Ordering::SeqCst); | |
println!("SYNC RWLOCK ======================================"); | |
let (s_rwlock_r, s_rwlock_w) = bench_sync_rwlock(); | |
RUNNING.store(true, Ordering::SeqCst); | |
println!("SYNC MUTEX ======================================="); | |
let (s_mutex_r, s_mutex_w) = bench_sync_mutex(); | |
print_stat!("PARKING_LOT RWLOCK", p_rwlock_r, p_rwlock_w); | |
print_stat!("PARKING_LOT MUTEX", p_mutex_r, p_mutex_w); | |
print_stat!("SYNC RWLOCK", s_rwlock_r, s_rwlock_w); | |
print_stat!("SYNC MUTEX", s_mutex_r, s_mutex_w); | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
16s, 100 free readers + 2 free writers
Result on 2020 MBP with 2.3GHz 4-core i7:
Result on 12-core Ryzen 9 5900X:
Findings:
parking_lot
doesn't seem to scale well