diff --git a/Cargo.toml b/Cargo.toml index e920739..26e8421 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -59,7 +59,7 @@ crossbeam-deque = { version = "0.7.1", optional = true } crossbeam-utils = { version = "0.6.6", optional = true } futures-core = { version = "0.3.0", optional = true } futures-io = { version = "0.3.0", optional = true } -futures-timer = { version = "1.0.2", optional = true } +futures-timer = { version = "2.0.0", optional = true } kv-log-macro = { version = "1.0.4", optional = true } log = { version = "0.4.8", features = ["kv_unstable"], optional = true } memchr = { version = "2.2.1", optional = true } @@ -73,10 +73,11 @@ slab = { version = "0.4.2", optional = true } [dev-dependencies] femme = "1.2.0" +futures = "0.3.0" +parking_lot = "0.9.0" rand = "0.7.2" surf = "1.0.3" tempdir = "0.3.7" -futures = "0.3.0" [[test]] name = "stream" diff --git a/benches/mutex.rs b/benches/mutex.rs index 4f1910a..8ef66a6 100644 --- a/benches/mutex.rs +++ b/benches/mutex.rs @@ -1,40 +1,125 @@ #![feature(test)] -extern crate test; +mod async_std { + extern crate test; -use async_std::sync::{Arc, Mutex}; -use async_std::task; -use test::Bencher; + use async_std::sync::{Arc, Mutex}; + use async_std::task; + use test::Bencher; -#[bench] -fn create(b: &mut Bencher) { - b.iter(|| Mutex::new(())); -} + #[bench] + fn create(b: &mut Bencher) { + b.iter(|| Mutex::new(())); + } -#[bench] -fn contention(b: &mut Bencher) { - b.iter(|| task::block_on(run(10, 1000))); -} + #[bench] + fn contention(b: &mut Bencher) { + b.iter(|| task::block_on(run(10, 1000))); + } + + #[bench] + fn no_contention(b: &mut Bencher) { + b.iter(|| task::block_on(run(1, 10000))); + } + + async fn run(task: usize, iter: usize) { + let m = Arc::new(Mutex::new(())); + let mut tasks = Vec::new(); + + for _ in 0..task { + let m = m.clone(); + tasks.push(task::spawn(async move { + for _ in 0..iter { + let _ = m.lock().await; + } + })); + } -#[bench] -fn no_contention(b: &mut Bencher) { - b.iter(|| task::block_on(run(1, 10000))); + for t in tasks { + t.await; + } + } } -async fn run(task: usize, iter: usize) { - let m = Arc::new(Mutex::new(())); - let mut tasks = Vec::new(); + mod std { + extern crate test; + + use std::sync::{Arc, Mutex}; + use std::thread; + use test::Bencher; - for _ in 0..task { - let m = m.clone(); - tasks.push(task::spawn(async move { - for _ in 0..iter { - let _ = m.lock().await; - } - })); + #[bench] + fn create(b: &mut Bencher) { + b.iter(|| Mutex::new(())); } - for t in tasks { - t.await; + #[bench] + fn contention(b: &mut Bencher) { + b.iter(|| run(10, 1000)); } -} + + #[bench] + fn no_contention(b: &mut Bencher) { + b.iter(|| run(1, 10000)); + } + + fn run(thread: usize, iter: usize) { + let m = Arc::new(Mutex::new(())); + let mut threads = Vec::new(); + + for _ in 0..thread { + let m = m.clone(); + threads.push(thread::spawn(move || { + for _ in 0..iter { + let _ = m.lock().unwrap(); + } + })); + } + + for t in threads { + t.join().unwrap(); + } + } + } + + mod parking_lot { + extern crate test; + + use std::sync::Arc; + use parking_lot::Mutex; + use std::thread; + use test::Bencher; + + #[bench] + fn create(b: &mut Bencher) { + b.iter(|| Mutex::new(())); + } + + #[bench] + fn contention(b: &mut Bencher) { + b.iter(|| run(10, 1000)); + } + + #[bench] + fn no_contention(b: &mut Bencher) { + b.iter(|| run(1, 10000)); + } + + fn run(thread: usize, iter: usize) { + let m = Arc::new(Mutex::new(())); + let mut threads = Vec::new(); + + for _ in 0..thread { + let m = m.clone(); + threads.push(thread::spawn(move || { + for _ in 0..iter { + let _ = m.lock(); + } + })); + } + + for t in threads { + t.join().unwrap(); + } + } + }