Compare commits

...

2 Commits

Author SHA1 Message Date
Yoshua Wuyts 2378a68b50
cargo fmt
Signed-off-by: Yoshua Wuyts <yoshuawuyts@gmail.com>
5 years ago
Yoshua Wuyts b1d2b8ac86
add mutex benches vs std + parking_lot
Signed-off-by: Yoshua Wuyts <yoshuawuyts@gmail.com>
5 years ago

@ -59,7 +59,7 @@ crossbeam-deque = { version = "0.7.1", optional = true }
crossbeam-utils = { version = "0.6.6", optional = true } crossbeam-utils = { version = "0.6.6", optional = true }
futures-core = { version = "0.3.0", optional = true } futures-core = { version = "0.3.0", optional = true }
futures-io = { 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 } kv-log-macro = { version = "1.0.4", optional = true }
log = { version = "0.4.8", features = ["kv_unstable"], optional = true } log = { version = "0.4.8", features = ["kv_unstable"], optional = true }
memchr = { version = "2.2.1", optional = true } memchr = { version = "2.2.1", optional = true }
@ -73,10 +73,11 @@ slab = { version = "0.4.2", optional = true }
[dev-dependencies] [dev-dependencies]
femme = "1.2.0" femme = "1.2.0"
futures = "0.3.0"
parking_lot = "0.9.0"
rand = "0.7.2" rand = "0.7.2"
surf = "1.0.3" surf = "1.0.3"
tempdir = "0.3.7" tempdir = "0.3.7"
futures = "0.3.0"
[[test]] [[test]]
name = "stream" name = "stream"

@ -1,40 +1,125 @@
#![feature(test)] #![feature(test)]
extern crate test; mod async_std {
extern crate test;
use async_std::sync::{Arc, Mutex}; use async_std::sync::{Arc, Mutex};
use async_std::task; use async_std::task;
use test::Bencher; use test::Bencher;
#[bench] #[bench]
fn create(b: &mut Bencher) { fn create(b: &mut Bencher) {
b.iter(|| Mutex::new(())); b.iter(|| Mutex::new(()));
} }
#[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();
#[bench] for _ in 0..task {
fn contention(b: &mut Bencher) { let m = m.clone();
b.iter(|| task::block_on(run(10, 1000))); tasks.push(task::spawn(async move {
for _ in 0..iter {
let _ = m.lock().await;
}
}));
}
for t in tasks {
t.await;
}
}
} }
#[bench] mod std {
fn no_contention(b: &mut Bencher) { extern crate test;
b.iter(|| task::block_on(run(1, 10000)));
use std::sync::{Arc, 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().unwrap();
}
}));
}
for t in threads {
t.join().unwrap();
}
}
} }
async fn run(task: usize, iter: usize) { mod parking_lot {
let m = Arc::new(Mutex::new(())); extern crate test;
let mut tasks = Vec::new();
use parking_lot::Mutex;
use std::sync::Arc;
use std::thread;
use test::Bencher;
for _ in 0..task { #[bench]
let m = m.clone(); fn create(b: &mut Bencher) {
tasks.push(task::spawn(async move { b.iter(|| Mutex::new(()));
for _ in 0..iter {
let _ = m.lock().await;
}
}));
} }
for t in tasks { #[bench]
t.await; 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();
}
} }
} }

Loading…
Cancel
Save