|
|
@ -1,27 +1,28 @@
|
|
|
|
#![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]
|
|
|
|
#[bench]
|
|
|
|
fn contention(b: &mut Bencher) {
|
|
|
|
fn contention(b: &mut Bencher) {
|
|
|
|
b.iter(|| task::block_on(run(10, 1000)));
|
|
|
|
b.iter(|| task::block_on(run(10, 1000)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
#[bench]
|
|
|
|
fn no_contention(b: &mut Bencher) {
|
|
|
|
fn no_contention(b: &mut Bencher) {
|
|
|
|
b.iter(|| task::block_on(run(1, 10000)));
|
|
|
|
b.iter(|| task::block_on(run(1, 10000)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async fn run(task: usize, iter: usize) {
|
|
|
|
async fn run(task: usize, iter: usize) {
|
|
|
|
let m = Arc::new(Mutex::new(()));
|
|
|
|
let m = Arc::new(Mutex::new(()));
|
|
|
|
let mut tasks = Vec::new();
|
|
|
|
let mut tasks = Vec::new();
|
|
|
|
|
|
|
|
|
|
|
@ -37,4 +38,88 @@ async fn run(task: usize, iter: usize) {
|
|
|
|
for t in tasks {
|
|
|
|
for t in tasks {
|
|
|
|
t.await;
|
|
|
|
t.await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mod std {
|
|
|
|
|
|
|
|
extern crate test;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|